Move the snippet chooser HTML markup from snippet_chooser_panel.html entirely within the AdminSnippetChooser widget's render() method

This commit is contained in:
Matt Westcott 2015-02-09 19:18:58 +00:00
parent 435e4571b3
commit 63dafbe4b7
4 changed files with 24 additions and 9 deletions

View file

@ -10,7 +10,6 @@ from .widgets import AdminSnippetChooser
class BaseSnippetChooserPanel(BaseChooserPanel):
field_template = "wagtailsnippets/edit_handlers/snippet_chooser_panel.html"
object_type_name = 'item'
_content_type = None

View file

@ -1,8 +1,2 @@
{% extends "wagtailadmin/edit_handlers/chooser_panel.html" %}
{% load i18n %}
{% block chooser_class %}snippet-chooser{% endblock %}
{% block chosen_state_view %}
<span class="title">{{ item }}</span>
{% endblock %}
{# Snippet chooser is now implemented as an entirely standard form widget - snippet_chooser_panel.html is redundant #}
{% include "wagtailadmin/shared/field.html" %}

View file

@ -0,0 +1,7 @@
{% extends "wagtailadmin/widgets/chooser.html" %}
{% block chooser_class %}snippet-chooser{% endblock %}
{% block chosen_state_view %}
<span class="title">{{ item }}</span>
{% endblock %}

View file

@ -2,6 +2,7 @@ from __future__ import absolute_import, unicode_literals
import json
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
from wagtail.wagtailadmin.widgets import AdminChooser
@ -20,6 +21,20 @@ class AdminSnippetChooser(AdminChooser):
if content_type is not None:
self.target_content_type = content_type
def render_html(self, name, value, attrs):
original_field_html = super(AdminSnippetChooser, self).render_html(name, value, attrs)
model_class = self.target_content_type.model_class()
instance = self.get_instance(model_class, value)
return render_to_string("wagtailsnippets/widgets/snippet_chooser.html", {
'widget': self,
'original_field_html': original_field_html,
'attrs': attrs,
'value': value,
'item': instance,
})
def render_js_init(self, id_, name, value):
content_type = self.target_content_type