kopia lustrzana https://github.com/wagtail/wagtail
Compare StreamField blocks by serialised value when checking has_changed for a form, preventing form.has_changed from always returning True for pages with StreamFields
rodzic
f3f932d2e5
commit
f915a69a93
|
@ -542,6 +542,9 @@ class BlockField(forms.Field):
|
|||
def clean(self, value):
|
||||
return self.block.clean(value)
|
||||
|
||||
def has_changed(self, initial_value, data_value):
|
||||
return self.block.get_prep_value(initial_value) != self.block.get_prep_value(data_value)
|
||||
|
||||
|
||||
DECONSTRUCT_ALIASES = {
|
||||
Block: 'wagtail.core.blocks.Block',
|
||||
|
|
|
@ -2417,6 +2417,23 @@ class TestStreamBlock(WagtailTestUtils, SimpleTestCase):
|
|||
self.assertEqual(list(block.child_blocks.keys()),
|
||||
['heading', 'paragraph', 'intro', 'by_line'])
|
||||
|
||||
def test_field_has_changed(self):
|
||||
block = blocks.StreamBlock([('paragraph', blocks.CharBlock())])
|
||||
initial_value = blocks.StreamValue(block, [('paragraph', 'test')])
|
||||
initial_value[0].id = 'a'
|
||||
|
||||
data_value = blocks.StreamValue(block, [('paragraph', 'test')])
|
||||
data_value[0].id = 'a'
|
||||
|
||||
# identical ids and content, so has_changed should return False
|
||||
self.assertFalse(blocks.BlockField(block).has_changed(initial_value, data_value))
|
||||
|
||||
changed_data_value = blocks.StreamValue(block, [('paragraph', 'not a test')])
|
||||
changed_data_value[0].id = 'a'
|
||||
|
||||
# identical ids but changed content, so has_changed should return True
|
||||
self.assertTrue(blocks.BlockField(block).has_changed(initial_value, changed_data_value))
|
||||
|
||||
def test_required_raises_an_exception_if_empty(self):
|
||||
block = blocks.StreamBlock([('paragraph', blocks.CharBlock())], required=True)
|
||||
value = blocks.StreamValue(block, [])
|
||||
|
|
Ładowanie…
Reference in New Issue