Skip to content

Commit

Permalink
Handle nested ImageBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Jan 31, 2025
1 parent b619a32 commit 4a90395
Show file tree
Hide file tree
Showing 5 changed files with 444 additions and 809 deletions.
13 changes: 12 additions & 1 deletion wagtail_localize/segments/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,26 @@ def handle_image_block(self, block, image_block_value, raw_value=None):
segments = []

for field_name, block_type in block.child_blocks.items():
if raw_value.get("type") and raw_value.get("value"):
# for top-level ImageBlock, raw_value has a
# {"type": "field_name", "value": {"image": X, "alt_text": "", "caption": ""}} format.
# whereas if the ImageBlock is part of a StructBlock, ListBlock or StreamBlock, we
# only get the "value" part.
raw_value = raw_value.get("value")

try:
block_raw_value = raw_value["value"].get(field_name)
block_raw_value = raw_value.get(field_name)
block_value = (
image_block_value if field_name == "image" else block_raw_value
)
except (KeyError, TypeError):
# e.g. raw_value is None, or is that from chooser
block_raw_value = None
block_value = None

if isinstance(block_type, blocks.CharBlock) and block_value is None:
block_value = ""

segments.extend(
segment.wrap(field_name)
for segment in self.handle_block(
Expand Down
108 changes: 106 additions & 2 deletions wagtail_localize/segments/tests/test_segment_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def test_imageblock(self):
{
"image": test_image.pk,
"decorative": False,
"alt_text": "Test alt content",
"alt_text": "The Alt text",
},
)

Expand All @@ -372,7 +372,111 @@ def test_imageblock(self):
f"test_streamfield.{block_id}.image", test_image.pk
),
StringSegmentValue(
f"test_streamfield.{block_id}.alt_text", "Test alt content"
f"test_streamfield.{block_id}.alt_text", "The Alt text"
),
],
)

@unittest.skipUnless(
WAGTAIL_VERSION >= (6, 3), "ImageBlock was added in Wagtail 6.3"
)
def test_imageblock_in_structblock(self):
block_id = uuid.uuid4()
test_image = get_image_model().objects.create(
title="Test image", file=get_test_image_file()
)
page = make_test_page_with_streamfield_block(
str(block_id),
"test_imageblock_in_structblock",
{
"the_image": {
"image": test_image.pk,
"decorative": False,
"alt_text": "The Alt text",
}
},
)

segments = extract_segments(page)
self.assertEqual(
segments,
[
OverridableSegmentValue(
f"test_streamfield.{block_id}.the_image.image", test_image.pk
),
StringSegmentValue(
f"test_streamfield.{block_id}.the_image.alt_text", "The Alt text"
),
],
)

@unittest.skipUnless(
WAGTAIL_VERSION >= (6, 3), "ImageBlock was added in Wagtail 6.3"
)
def test_imageblock_in_listblock(self):
block_id = uuid.uuid4()
test_image = get_image_model().objects.create(
title="Test image", file=get_test_image_file()
)
item_id = "11111111-1111-1111-1111-111111111111"
page = make_test_page_with_streamfield_block(
str(block_id),
"test_imageblock_in_listblock",
[
{
"type": "item",
"id": item_id,
"value": {
"image": test_image.pk,
"decorative": False,
"alt_text": "",
},
},
],
)

segments = extract_segments(page)
self.assertEqual(
segments,
[
OverridableSegmentValue(
f"test_streamfield.{block_id}.{item_id}.image", test_image.pk
),
],
)

@unittest.skipUnless(
WAGTAIL_VERSION >= (6, 3), "ImageBlock was added in Wagtail 6.3"
)
def test_imageblock_in_streamblock(self):
block_id = uuid.uuid4()
nested_block_id = uuid.uuid4()
test_image = get_image_model().objects.create(
title="Test image", file=get_test_image_file()
)
page = make_test_page_with_streamfield_block(
str(block_id),
"test_imageblock_in_streamblock",
[
{
"id": str(nested_block_id),
"type": "the_image",
"value": {
"image": test_image.pk,
"decorative": True,
"alt_text": "",
},
},
],
)

segments = extract_segments(page)
self.assertEqual(
segments,
[
OverridableSegmentValue(
f"test_streamfield.{block_id}.{nested_block_id}.image",
test_image.pk,
),
],
)
Expand Down
Loading

0 comments on commit 4a90395

Please sign in to comment.