Skip to content

Commit

Permalink
Fix image translation in ImageBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
Chadys authored and zerolab committed Feb 8, 2025
1 parent 9308650 commit d3f9291
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 9 additions & 0 deletions wagtail_localize/segments/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ def handle_image_block(self, block, image_block_value, segments):
field_name, segment = segment.unwrap()
segments_by_field[field_name].append(segment)

# Handle the image first (either as original, or as an override)
# we need to pop it from the dict as we then follow this up with setting the attributes
# on the image block value (which happens to be the actual image instance
image_segment = segments_by_field.pop("image", None)
if image_segment is not None:
image_block_value = self.handle_related_object_block(
image_block_value, image_segment
)

# ImageBlock field -> Image field.
field_map = {"alt_text": "contextual_alt_text", "decorative": "decorative"}
for field_name, segments in segments_by_field.items():
Expand Down
9 changes: 8 additions & 1 deletion wagtail_localize/segments/tests/test_segment_ingestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,9 @@ def test_imageblock(self):
test_image = get_image_model().objects.create(
title="Test image", file=get_test_image_file()
)
test_image2 = get_image_model().objects.create(
title="Test image2", file=get_test_image_file()
)
page = make_test_page_with_streamfield_block(
str(block_id),
"test_imageblock",
Expand All @@ -586,6 +589,10 @@ def test_imageblock(self):
f"test_streamfield.{block_id}.alt_text",
"Tester le alt_text contenu",
),
OverridableSegmentValue(
f"test_streamfield.{block_id}.image",
test_image2.id,
),
],
)

Expand All @@ -599,7 +606,7 @@ def test_imageblock(self):
"id": str(block_id),
"type": "test_imageblock",
"value": {
"image": 1,
"image": 2,
"alt_text": "Tester le alt_text contenu",
"decorative": False,
},
Expand Down

0 comments on commit d3f9291

Please sign in to comment.