Skip to content

Commit

Permalink
Merge pull request #73 from wagtail/fix/no-stripping-falsy-values
Browse files Browse the repository at this point in the history
Only delete required blocks if value is None
  • Loading branch information
jacobtoppm committed Mar 4, 2021
2 parents 88f02e7 + 644ed9a commit 3e6572a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion tests/blocks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from wagtail.core.blocks import (
CharBlock, RichTextBlock, StreamBlock, StructBlock, ListBlock, PageChooserBlock
CharBlock, IntegerBlock, RichTextBlock, StreamBlock, StructBlock, ListBlock, PageChooserBlock
)


Expand All @@ -18,6 +18,7 @@ class BaseStreamBlock(StreamBlock):
Define the custom blocks that `StreamField` will utilize
"""
link_block = CaptionedPageLink()
integer = IntegerBlock(required=True)
page = PageChooserBlock()
stream = AnotherStreamBlock()
rich_text = RichTextBlock()
Expand Down
5 changes: 4 additions & 1 deletion tests/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ def test_import_page_with_streamfield_page_links_where_linked_pages_not_imported
"seo_title": "",
"show_in_menus": false,
"search_description": "",
"body": "[{\\"type\\": \\"link_block\\", \\"value\\": {\\"page\\": 100, \\"text\\": \\"Test\\"}, \\"id\\": \\"fc3b0d3d-d316-4271-9e31-84919558188a\\"}, {\\"type\\": \\"page\\", \\"value\\": 200, \\"id\\": \\"c6d07d3a-72d4-445e-8fa5-b34107291176\\"}, {\\"type\\": \\"stream\\", \\"value\\": [{\\"type\\": \\"page\\", \\"value\\": 300, \\"id\\": \\"8c0d7de7-4f77-4477-be67-7d990d0bfb82\\"}], \\"id\\": \\"21ffe52a-c0fc-4ecc-92f1-17b356c9cc94\\"}, {\\"type\\": \\"list_of_pages\\", \\"value\\": [500], \\"id\\": \\"17b972cb-a952-4940-87e2-e4eb00703997\\"}]"},
"body": "[{\\"type\\": \\"integer\\", \\"value\\": 0, \\"id\\": \\"aad07d3a-72d4-445e-8fa5-b34107291199\\"}, {\\"type\\": \\"link_block\\", \\"value\\": {\\"page\\": 100, \\"text\\": \\"Test\\"}, \\"id\\": \\"fc3b0d3d-d316-4271-9e31-84919558188a\\"}, {\\"type\\": \\"page\\", \\"value\\": 200, \\"id\\": \\"c6d07d3a-72d4-445e-8fa5-b34107291176\\"}, {\\"type\\": \\"stream\\", \\"value\\": [{\\"type\\": \\"page\\", \\"value\\": 300, \\"id\\": \\"8c0d7de7-4f77-4477-be67-7d990d0bfb82\\"}], \\"id\\": \\"21ffe52a-c0fc-4ecc-92f1-17b356c9cc94\\"}, {\\"type\\": \\"list_of_pages\\", \\"value\\": [500], \\"id\\": \\"17b972cb-a952-4940-87e2-e4eb00703997\\"}]"},
"parent_id": 300
}
]
Expand All @@ -605,6 +605,9 @@ def test_import_page_with_streamfield_page_links_where_linked_pages_not_imported
# The PageChooserBlock has required=True, so when its value is removed, the block should also be removed
self.assertNotIn({'type': 'page', 'value': None, 'id': 'c6d07d3a-72d4-445e-8fa5-b34107291176'}, imported_streamfield)

# Test that 0 values are not removed, only None
self.assertIn({'type': 'integer', 'value': 0, 'id': 'aad07d3a-72d4-445e-8fa5-b34107291199'}, imported_streamfield)

# By contrast, the PageChooserBlock in the link_block has required=False, so just the block's value should be removed instead
self.assertIn({'type': 'link_block', 'value': {'page': None, 'text': 'Test'}, 'id': 'fc3b0d3d-d316-4271-9e31-84919558188a'}, imported_streamfield)

Expand Down
2 changes: 1 addition & 1 deletion wagtail_transfer/streamfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def map_over_json(self, stream, func):
list of dicts (imported json) format and return a copy of the rewritten streamfield.
"""
value = func(self.block, stream)
if self.block.required and not value:
if self.block.required and value is None:
raise ValidationError('This block requires a value')
return value

Expand Down

0 comments on commit 3e6572a

Please sign in to comment.