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

Handle text field clearing when syncing #495

Merged
merged 1 commit into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 13 additions & 5 deletions wagtail_localize/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,24 @@ class TranslatableField(BaseTranslatableField):
def is_translated(self, obj):
return True

def is_synchronized(self, obj):
field = self.get_field(obj.__class__)
def is_synchronized(self, source):
field = self.get_field(source.__class__)

# Child relations should all be synchronised before translation
if isinstance(field, (models.ManyToOneRel)) and isinstance(
if isinstance(field, models.ManyToOneRel) and isinstance(
field.remote_field, ParentalKey
):
return True

# Streamfields need to be re-synchronised before translation so the structure and non-translatable content is copied over
# We have a text field that has been cleared so we should mark it as synchronised
if (
isinstance(field, (RichTextField, models.TextField, models.CharField))
and getattr(source, field.attname) == ""
):
return True

# Streamfields need to be re-synchronised before translation so
# the structure and non-translatable content is copied over
return isinstance(field, StreamField)

def __repr__(self):
Expand Down Expand Up @@ -245,7 +253,7 @@ def copy_synchronised_fields(source, target):
if translatable_field.is_synchronized(source):
field = translatable_field.get_field(target.__class__)

if isinstance(field, (models.ManyToOneRel)) and isinstance(
if isinstance(field, models.ManyToOneRel) and isinstance(
field.remote_field, ParentalKey
):
# Use modelcluster's copy_child_relation for child relations
Expand Down
25 changes: 25 additions & 0 deletions wagtail_localize/tests/test_update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def setUp(self):
title="Blog post",
slug="blog-post",
test_charfield="Test content",
test_richtextfield="<p>rich text</p>",
)

self.en_snippet = TestSnippet.objects.create(field="Test snippet")
Expand Down Expand Up @@ -351,6 +352,30 @@ def test_post_update_page_translation_with_publish_translations(self):
self.fr_blog_post.refresh_from_db()
self.assertEqual(self.fr_blog_post.test_charfield, "Edited blog post")

def test_post_update_page_translation_with_publish_translations_and_cleared_text_field(
self,
):
self.en_blog_post.test_charfield = ""
self.en_blog_post.test_richtextfield = ""
self.en_blog_post.save_revision().publish()

response = self.client.post(
reverse(
"wagtail_localize:update_translations",
args=[self.page_source.id],
),
{"publish_translations": "on"},
)

self.assertRedirects(
response, reverse("wagtailadmin_explore", args=[self.en_blog_index.id])
)

# The FR version should be updated
self.fr_blog_post.refresh_from_db()
self.assertEqual(self.fr_blog_post.test_charfield, "")
self.assertEqual(self.fr_blog_post.test_richtextfield, "")

def test_post_update_snippet_translation(self):
self.en_snippet.field = "Edited snippet"
self.en_snippet.save()
Expand Down