Make StructBlock.clean return a StructValue, not a dict - fixes #1355

pull/1360/head
Matt Westcott 2015-05-29 21:12:12 +01:00
rodzic 3188fc031a
commit 1167cd0484
2 zmienionych plików z 18 dodań i 3 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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):