From 1167cd0484d70ffb2700dc7f8ff21f3730bddd75 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Fri, 29 May 2015 21:12:12 +0100 Subject: [PATCH] Make StructBlock.clean return a StructValue, not a dict - fixes #1355 --- wagtail/wagtailcore/blocks/struct_block.py | 6 +++--- wagtail/wagtailcore/tests/test_blocks.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/wagtail/wagtailcore/blocks/struct_block.py b/wagtail/wagtailcore/blocks/struct_block.py index f7b6eade61..94a73a563e 100644 --- a/wagtail/wagtailcore/blocks/struct_block.py +++ b/wagtail/wagtailcore/blocks/struct_block.py @@ -94,11 +94,11 @@ class BaseStructBlock(Block): ]) def clean(self, value): - result = {} + result = [] # build up a list of (name, value) tuples to be passed to the StructValue constructor errors = {} for name, val in value.items(): try: - result[name] = self.child_blocks[name].clean(val) + result.append((name, self.child_blocks[name].clean(val))) except ValidationError as e: errors[name] = ErrorList([e]) @@ -107,7 +107,7 @@ class BaseStructBlock(Block): # and delegate the errors contained in the 'params' dict to the child blocks instead raise ValidationError('Validation error in StructBlock', params=errors) - return result + return StructValue(self, result) def to_python(self, value): # recursively call to_python on children and return as a StructValue diff --git a/wagtail/wagtailcore/tests/test_blocks.py b/wagtail/wagtailcore/tests/test_blocks.py index 154a642011..f0556e5d28 100644 --- a/wagtail/wagtailcore/tests/test_blocks.py +++ b/wagtail/wagtailcore/tests/test_blocks.py @@ -497,6 +497,21 @@ class TestStructBlock(unittest.TestCase): self.assertEqual(event['guest_speaker']['first_name'], 'Ed') self.assertTrue(isinstance(event['guest_speaker'], blocks.StructValue)) + def test_clean(self): + block = blocks.StructBlock([ + ('title', blocks.CharBlock()), + ('link', blocks.URLBlock()), + ]) + + value = block.to_python({'title': 'Torchbox', 'link': 'http://www.torchbox.com/'}) + clean_value = block.clean(value) + self.assertTrue(isinstance(clean_value, blocks.StructValue)) + self.assertEqual(clean_value['title'], 'Torchbox') + + value = block.to_python({'title': 'Torchbox', 'link': 'not a url'}) + with self.assertRaises(ValidationError): + block.clean(value) + class TestListBlock(unittest.TestCase): def test_initialise_with_class(self):