kopia lustrzana https://github.com/wagtail/wagtail
make WidgetWithScript explicitly fail in the absence of an ID attribute
rodzic
f56d927be0
commit
8f655cd74d
|
@ -10,12 +10,14 @@ class WidgetWithScript(Widget):
|
|||
return super(WidgetWithScript, self).render(name, value, attrs)
|
||||
|
||||
def render(self, name, value, attrs=None):
|
||||
widget_html = self.render_html(name, value, attrs)
|
||||
# no point trying to come up with sensible semantics for when 'id' is missing from attrs,
|
||||
# so let's make sure it fails early in the process
|
||||
try:
|
||||
id_ = attrs['id']
|
||||
except KeyError, TypeError:
|
||||
raise TypeError("WidgetWithScript cannot be rendered without an 'id' attribute")
|
||||
|
||||
final_attrs = self.build_attrs(attrs, name=name)
|
||||
id_ = final_attrs.get('id', None)
|
||||
if id_ is None:
|
||||
return widget_html
|
||||
widget_html = self.render_html(name, value, attrs)
|
||||
|
||||
js = self.render_js_init(id_, name, value)
|
||||
out = '{0}<script>{1}</script>'.format(widget_html, js)
|
||||
|
|
|
@ -6,6 +6,7 @@ from django.core.urlresolvers import reverse
|
|||
from django.forms import widgets
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
from wagtail.utils.widgets import WidgetWithScript
|
||||
from wagtail.wagtailcore.models import Page
|
||||
|
@ -61,6 +62,14 @@ class AdminPageChooser(AdminChooser):
|
|||
super(AdminPageChooser, self).__init__(**kwargs)
|
||||
self.target_content_type = content_type or ContentType.objects.get_for_model(Page)
|
||||
|
||||
def render_html(self, name, value, attrs):
|
||||
original_field_html = super(AdminPageChooser, self).render_html(name, value, attrs)
|
||||
|
||||
return render_to_string("wagtailadmin/widgets/page_chooser.html", {
|
||||
'original_field_html': original_field_html,
|
||||
'widget': self,
|
||||
})
|
||||
|
||||
def render_js_init(self, id_, name, value):
|
||||
page = Page.objects.get(pk=value) if value else None
|
||||
parent = page.get_parent() if page else None
|
||||
|
|
Ładowanie…
Reference in New Issue