Skip to content

Commit

Permalink
Merge pull request #97 from AccentDesign/tidy_streamfield
Browse files Browse the repository at this point in the history
better streamfield init of registered fields
  • Loading branch information
stuartaccent authored Jun 18, 2018
2 parents 046a49f + fadb66d commit adefed0
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


install_requires = [
'Django>=2,<2.1',
'wagtail>=2,<2.2'
]

Expand Down
56 changes: 56 additions & 0 deletions tests/fields/test_streamfield.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from django import forms
from django.core.exceptions import ImproperlyConfigured

from wagtailstreamforms.streamfield import FormFieldsStreamField
from wagtailstreamforms import fields

from ..test_case import AppTestCase


class GoodField(fields.BaseField):
field_class = forms.CharField


class TestCorrectTypeRegistering(AppTestCase):

@classmethod
def setUpClass(cls):
fields.register('good', GoodField)

@classmethod
def tearDownClass(cls):
del fields._fields['good']

def test_child_blocks(self):
field = FormFieldsStreamField([])
self.assertIn('good', field.stream_block.child_blocks)

def test_dependencies(self):
field = FormFieldsStreamField([])
self.assertListEqual(
[b.__class__ for b in field.stream_block.dependencies],
[b.__class__ for b in field.stream_block.child_blocks.values()]
)


class BadField:
field_class = forms.CharField


class TestIncorrectTypeRegistering(AppTestCase):

@classmethod
def setUpClass(cls):
fields.register('bad', BadField)

@classmethod
def tearDownClass(cls):
del fields._fields['bad']

def test_is_invalid_class(self):
expected_error = "'%s' must be a subclass of '%s'" % (BadField, fields.BaseField)

with self.assertRaises(ImproperlyConfigured) as e:
FormFieldsStreamField([])

self.assertEqual(e.exception.args[0], expected_error)
42 changes: 39 additions & 3 deletions wagtailstreamforms/streamfield.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
from django.core.exceptions import ImproperlyConfigured

from wagtail.core import blocks
from wagtail.core.fields import StreamField
from wagtailstreamforms.fields import get_fields, BaseField

from wagtailstreamforms.fields import get_fields

class FormFieldStreamBlock(blocks.StreamBlock):
""" Add all registered instances of BaseField's get_form_block method to the streamfield. """

class FormFieldsStreamField(StreamField):
def __init__(self, local_blocks=None, **kwargs):
self._constructor_kwargs = kwargs

# Note, this is calling BaseStreamBlock's super __init__, not FormFieldStreamBlock's.
# We don't want BaseStreamBlock.__init__() to run, because it tries to assign to self.child_blocks,
# which we've overridden with a @property. But we DO want Block.__init__() to run.
super(blocks.BaseStreamBlock, self).__init__()

self._child_blocks = self.base_blocks.copy()

for name, field_class in get_fields().items():

# ensure the field is a subclass of BaseField.
if not issubclass(field_class, BaseField):
raise ImproperlyConfigured("'%s' must be a subclass of '%s'" % (field_class, BaseField))

# assign the block
block = field_class().get_form_block()
block.set_name(name)
self._child_blocks[name] = block

self._dependencies = self._child_blocks.values()

@property
def child_blocks(self):
return self._child_blocks

@property
def dependencies(self):
return self._dependencies


class FormFieldsStreamField(StreamField):
def __init__(self, block_types, **kwargs):
block_types = [(key, item().get_form_block()) for key, item in get_fields().items()]
super().__init__(block_types, **kwargs)
self.stream_block = FormFieldStreamBlock(block_types, required=not self.blank)

0 comments on commit adefed0

Please sign in to comment.