0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Update ListBlock.value_from_datadict to pick up block IDs

This commit is contained in:
Matt Westcott 2021-11-30 15:24:21 +00:00 committed by Matt Westcott
parent 4faa209986
commit 4c73ce70b8
2 changed files with 29 additions and 7 deletions

View File

@ -121,19 +121,23 @@ class ListBlock(Block):
def value_from_datadict(self, data, files, prefix):
count = int(data['%s-count' % prefix])
values_with_indexes = []
child_blocks_with_indexes = []
for i in range(0, count):
if data['%s-%d-deleted' % (prefix, i)]:
continue
values_with_indexes.append(
child_blocks_with_indexes.append(
(
int(data['%s-%d-order' % (prefix, i)]),
self.child_block.value_from_datadict(data, files, '%s-%d-value' % (prefix, i))
ListValue.ListChild(
self.child_block,
self.child_block.value_from_datadict(data, files, '%s-%d-value' % (prefix, i)),
id=data.get('%s-%d-id' % (prefix, i)),
)
)
)
values_with_indexes.sort()
return ListValue(self, values=[v for (i, v) in values_with_indexes])
child_blocks_with_indexes.sort()
return ListValue(self, bound_blocks=[b for (i, b) in child_blocks_with_indexes])
def value_omitted_from_data(self, data, files, prefix):
return ('%s-count' % prefix) not in data

View File

@ -2239,6 +2239,22 @@ class TestListBlock(WagtailTestUtils, SimpleTestCase):
}, {}, 'mylist'))
self.assertTrue(block.value_omitted_from_data({'nothing-here': 'nope'}, {}, 'mylist'))
def test_id_from_form_submission_is_preserved(self):
block = blocks.ListBlock(blocks.CharBlock())
post_data = {'shoppinglist-count': '3'}
for i in range(0, 3):
post_data.update({
'shoppinglist-%d-deleted' % i: '',
'shoppinglist-%d-order' % i: str(i),
'shoppinglist-%d-value' % i: "item %d" % i,
'shoppinglist-%d-id' % i: "0000000%d" % i,
})
block_value = block.value_from_datadict(post_data, {}, 'shoppinglist')
self.assertEqual(block_value.bound_blocks[1].value, "item 1")
self.assertEqual(block_value.bound_blocks[1].id, "00000001")
def test_ordering_in_form_submission_uses_order_field(self):
block = blocks.ListBlock(blocks.CharBlock())
@ -2248,7 +2264,8 @@ class TestListBlock(WagtailTestUtils, SimpleTestCase):
post_data.update({
'shoppinglist-%d-deleted' % i: '',
'shoppinglist-%d-order' % i: str(2 - i),
'shoppinglist-%d-value' % i: "item %d" % i
'shoppinglist-%d-value' % i: "item %d" % i,
'shoppinglist-%d-id' % i: "0000000%d" % i,
})
block_value = block.value_from_datadict(post_data, {}, 'shoppinglist')
@ -2263,7 +2280,8 @@ class TestListBlock(WagtailTestUtils, SimpleTestCase):
post_data.update({
'shoppinglist-%d-deleted' % i: '',
'shoppinglist-%d-order' % i: str(i),
'shoppinglist-%d-value' % i: "item %d" % i
'shoppinglist-%d-value' % i: "item %d" % i,
'shoppinglist-%d-id' % i: "0000000%d" % i,
})
block_value = block.value_from_datadict(post_data, {}, 'shoppinglist')