Skip to content

Commit

Permalink
Handle text field clearing when syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Dec 13, 2021
1 parent 297e5ed commit eb950c9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
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

0 comments on commit eb950c9

Please sign in to comment.