Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining translatable_fields/override_translatable_fields on StructBlock #752

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions wagtail_localize/segments/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,17 @@ def handle_related_object_block(self, related_object):
def handle_struct_block(self, struct_block, raw_value=None):
segments = []

override_translatable_blocks = getattr(
struct_block.block, "override_translatable_blocks", None
freddiemixell marked this conversation as resolved.
Show resolved Hide resolved
)

for field_name, block_value in struct_block.items():
if (
override_translatable_blocks is not None
and field_name not in override_translatable_blocks
):
continue

block_type = struct_block.block.child_blocks[field_name]
try:
block_raw_value = raw_value["value"].get(field_name)
Expand Down
34 changes: 34 additions & 0 deletions wagtail_localize/segments/tests/test_segment_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,40 @@ def test_customstructblock(self):
],
)

def test_structblockwithoverrides(self):
block_id = uuid.uuid4()
page = make_test_page_with_streamfield_block(
str(block_id),
"test_structblockwithoverrides",
{"field_a": "Test content", "field_b": "Non-translatable content"},
)

segments = extract_segments(page)

self.assertEqual(
segments,
[
StringSegmentValue(
f"test_streamfield.{block_id}.field_a",
"Test content",
)
],
)

def test_structblockignoreall(self):
block_id = uuid.uuid4()
page = make_test_page_with_streamfield_block(
str(block_id),
"test_structblockignoreall",
{
"field_a": "Non-translatable content",
"field_b": "Non-translatable content",
},
)

segments = extract_segments(page)
self.assertEqual(segments, [])

def test_customblockwithoutextractmethod(self):
block_id = uuid.uuid4()
page = make_test_page_with_streamfield_block(
Expand Down
16 changes: 16 additions & 0 deletions wagtail_localize/test/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ class TestNestedStreamBlock(blocks.StreamBlock):
chooser_in_list = blocks.ListBlock(blocks.PageChooserBlock())


class TestStructBlockOverride(blocks.StructBlock):
field_a = blocks.TextBlock()
field_b = blocks.TextBlock()

override_translatable_blocks = ["field_a"]


class TestStructBlockIgnoreAll(blocks.StructBlock):
field_a = blocks.TextBlock()
field_b = blocks.TextBlock()

override_translatable_blocks = []


class TestNestedChooserStructBlock(blocks.StructBlock):
nested_page = TestChooserStructBlock()

Expand Down Expand Up @@ -196,6 +210,8 @@ class TestStreamBlock(blocks.StreamBlock):
test_nestedstreamblock = TestNestedStreamBlock()
test_streamblock_in_structblock = TestStreamBlockInStructBlock()
test_customstructblock = CustomStructBlock()
test_structblockwithoverrides = TestStructBlockOverride()
test_structblockignoreall = TestStructBlockIgnoreAll()
test_customblockwithoutextractmethod = CustomBlockWithoutExtractMethod()
test_pagechooserblock = blocks.PageChooserBlock()
test_pagechooserblock_with_restricted_types = blocks.PageChooserBlock(
Expand Down