mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Update ListBlock.value_from_datadict to pick up block IDs
This commit is contained in:
parent
4faa209986
commit
4c73ce70b8
@ -121,19 +121,23 @@ class ListBlock(Block):
|
|||||||
|
|
||||||
def value_from_datadict(self, data, files, prefix):
|
def value_from_datadict(self, data, files, prefix):
|
||||||
count = int(data['%s-count' % prefix])
|
count = int(data['%s-count' % prefix])
|
||||||
values_with_indexes = []
|
child_blocks_with_indexes = []
|
||||||
for i in range(0, count):
|
for i in range(0, count):
|
||||||
if data['%s-%d-deleted' % (prefix, i)]:
|
if data['%s-%d-deleted' % (prefix, i)]:
|
||||||
continue
|
continue
|
||||||
values_with_indexes.append(
|
child_blocks_with_indexes.append(
|
||||||
(
|
(
|
||||||
int(data['%s-%d-order' % (prefix, i)]),
|
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()
|
child_blocks_with_indexes.sort()
|
||||||
return ListValue(self, values=[v for (i, v) in values_with_indexes])
|
return ListValue(self, bound_blocks=[b for (i, b) in child_blocks_with_indexes])
|
||||||
|
|
||||||
def value_omitted_from_data(self, data, files, prefix):
|
def value_omitted_from_data(self, data, files, prefix):
|
||||||
return ('%s-count' % prefix) not in data
|
return ('%s-count' % prefix) not in data
|
||||||
|
@ -2239,6 +2239,22 @@ class TestListBlock(WagtailTestUtils, SimpleTestCase):
|
|||||||
}, {}, 'mylist'))
|
}, {}, 'mylist'))
|
||||||
self.assertTrue(block.value_omitted_from_data({'nothing-here': 'nope'}, {}, '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):
|
def test_ordering_in_form_submission_uses_order_field(self):
|
||||||
block = blocks.ListBlock(blocks.CharBlock())
|
block = blocks.ListBlock(blocks.CharBlock())
|
||||||
|
|
||||||
@ -2248,7 +2264,8 @@ class TestListBlock(WagtailTestUtils, SimpleTestCase):
|
|||||||
post_data.update({
|
post_data.update({
|
||||||
'shoppinglist-%d-deleted' % i: '',
|
'shoppinglist-%d-deleted' % i: '',
|
||||||
'shoppinglist-%d-order' % i: str(2 - 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')
|
block_value = block.value_from_datadict(post_data, {}, 'shoppinglist')
|
||||||
@ -2263,7 +2280,8 @@ class TestListBlock(WagtailTestUtils, SimpleTestCase):
|
|||||||
post_data.update({
|
post_data.update({
|
||||||
'shoppinglist-%d-deleted' % i: '',
|
'shoppinglist-%d-deleted' % i: '',
|
||||||
'shoppinglist-%d-order' % i: str(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')
|
block_value = block.value_from_datadict(post_data, {}, 'shoppinglist')
|
||||||
|
Loading…
Reference in New Issue
Block a user