-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from AccentDesign/tidy_streamfield
better streamfield init of registered fields
- Loading branch information
Showing
3 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ | |
|
||
|
||
install_requires = [ | ||
'Django>=2,<2.1', | ||
'wagtail>=2,<2.2' | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |