move wagtailadmin.widgets.StreamWidget to wagtailadmin.blocks.BlockWidget

This keeps all block-API related code in one module, and reflects the fact that BlockWidget is not inherently tied to StreamBlock in particular.
This commit is contained in:
Matt Westcott 2015-02-11 16:04:14 +00:00
parent 194535120a
commit f7e6fcc7b5
3 changed files with 37 additions and 31 deletions

View file

@ -14,7 +14,7 @@ from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.deconstruct import deconstructible
from django.utils.functional import cached_property
from django.template.loader import render_to_string
from django.forms import Media, CharField, ModelChoiceField
from django.forms import Media, CharField, ModelChoiceField, Widget
from django.forms.utils import ErrorList
import six
@ -869,3 +869,37 @@ class StreamValue(collections.Sequence):
def __str__(self):
return self.stream_block.render(self)
# ========================
# django.forms integration
# ========================
class BlockWidget(Widget):
"""Wraps a block object as a widget so that it can be incorporated into a Django form"""
def __init__(self, block_def, attrs=None):
super(BlockWidget, self).__init__(attrs=attrs)
self.block_def = block_def
def render(self, name, value, attrs=None):
bound_block = self.block_def.bind(value, prefix=name)
js_initializer = self.block_def.js_initializer()
if js_initializer:
js_snippet = """
<script>
$(function() {
var initializer = %s;
initializer('%s');
})
</script>
""" % (js_initializer, name)
else:
js_snippet = ''
return mark_safe(bound_block.render_form() + js_snippet)
@property
def media(self):
return self.block_def.all_media()
def value_from_datadict(self, data, files, name):
return self.block_def.value_from_datadict(data, files, name)

View file

@ -20,6 +20,7 @@ from django.utils.translation import ugettext_lazy
from taggit.managers import TaggableManager
from wagtail.wagtailadmin import widgets
from wagtail.wagtailadmin.blocks import BlockWidget
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.utils import camelcase_to_underscore, resolve_model_string
from wagtail.utils.deprecation import RemovedInWagtail11Warning
@ -723,7 +724,7 @@ class BaseStreamFieldPanel(BaseFieldPanel):
@classmethod
def widget_overrides(cls):
return {cls.field_name: widgets.StreamWidget(block_def=cls.block_def)}
return {cls.field_name: BlockWidget(block_def=cls.block_def)}
@classmethod
def html_declarations(cls):

View file

@ -121,32 +121,3 @@ class AdminPageChooser(AdminChooser):
app=content_type.app_label,
model=content_type.model)),
parent=json.dumps(parent.id if parent else None))
class StreamWidget(widgets.Widget):
def __init__(self, block_def, attrs=None):
super(StreamWidget, self).__init__(attrs=attrs)
self.block_def = block_def
def render(self, name, value, attrs=None):
bound_block = self.block_def.bind(value, prefix=name)
js_initializer = self.block_def.js_initializer()
if js_initializer:
js_snippet = """
<script>
$(function() {
var initializer = %s;
initializer('%s');
})
</script>
""" % (js_initializer, name)
else:
js_snippet = ''
return mark_safe(bound_block.render_form() + js_snippet)
@property
def media(self):
return self.block_def.all_media()
def value_from_datadict(self, data, files, name):
return self.block_def.value_from_datadict(data, files, name)