From 3ff98a14c03bbc35cacaf5a42c3e17bee410ca97 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 25 Mar 2015 11:47:27 +0000 Subject: [PATCH] StructBlock.value_from_datadict should return a StructValue, not dict - fixes #1100 --- wagtail/wagtailcore/blocks/struct_block.py | 2 +- wagtail/wagtailcore/tests/test_blocks.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/wagtail/wagtailcore/blocks/struct_block.py b/wagtail/wagtailcore/blocks/struct_block.py index 1203c537ef..2373ef75dc 100644 --- a/wagtail/wagtailcore/blocks/struct_block.py +++ b/wagtail/wagtailcore/blocks/struct_block.py @@ -80,7 +80,7 @@ class BaseStructBlock(Block): return format_html('
', list_items) def value_from_datadict(self, data, files, prefix): - return dict([ + return StructValue(self, [ (name, block.value_from_datadict(data, files, '%s-%s' % (prefix, name))) for name, block in self.child_blocks.items() ]) diff --git a/wagtail/wagtailcore/tests/test_blocks.py b/wagtail/wagtailcore/tests/test_blocks.py index 3262d56748..458fbb341b 100644 --- a/wagtail/wagtailcore/tests/test_blocks.py +++ b/wagtail/wagtailcore/tests/test_blocks.py @@ -438,6 +438,23 @@ class TestStructBlock(unittest.TestCase): self.assertEqual(content, ["Wagtail site"]) + def test_value_from_datadict(self): + block = blocks.StructBlock([ + ('title', blocks.CharBlock()), + ('link', blocks.URLBlock()), + ]) + + struct_val = block.value_from_datadict({ + 'mylink-title': "Torchbox", + 'mylink-link': "http://www.torchbox.com" + }, {}, 'mylink') + + self.assertEqual(struct_val['title'], "Torchbox") + self.assertEqual(struct_val['link'], "http://www.torchbox.com") + self.assertTrue(isinstance(struct_val, blocks.StructValue)) + self.assertTrue(isinstance(struct_val.bound_blocks['link'].block, blocks.URLBlock)) + + class TestListBlock(unittest.TestCase): def test_initialise_with_class(self):