Skip to content
Open
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
12 changes: 11 additions & 1 deletion wagtail_localize/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,26 @@ def update_or_create_from_instance(cls, instance):
return source, created

@transaction.atomic
def update_from_db(self):
def update_from_db(self, use_draft_content=False):
"""
Retrieves the source instance from the database and updates this TranslationSource
with its current contents.

Args:
use_draft_content: If True and the source instance is a subclass of DraftStateMixin, the content is
retrieved from either the currently scheduled revision, or from the latest revision.

Raises:
Model.DoesNotExist: If the source instance has been deleted.
"""
instance = self.get_source_instance()

if use_draft_content and isinstance(instance, DraftStateMixin):
instance = (
instance.get_scheduled_revision_as_object()
or instance.get_latest_revision_as_object()
)

if isinstance(instance, ClusterableModel):
self.content_json = instance.to_json()
else:
Expand Down
87 changes: 87 additions & 0 deletions wagtail_localize/tests/test_update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,90 @@ def test_post_update_page_translation_with_use_machine_translation(self):
context_id=string_segment.context_id,
)
self.assertEqual(string_translation.data, "post blog Edited")

def test_post_update_draft_page_translation(self):
self.en_blog_post.test_charfield = "Edited blog post"
self.en_blog_post.save_revision()

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

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

# Check that the new string was submitted
string_segment = StringSegment.objects.get(context__path="test_charfield")
self.assertEqual(string_segment.string.data, "Edited blog post")

# The FR version shouldn't be updated yet
self.fr_blog_post.refresh_from_db()
self.assertEqual(self.fr_blog_post.test_charfield, "Test content")

def test_post_update_draft_page_translation_with_publish_translations(self):
self.en_blog_post.test_charfield = "Edited blog post"
self.en_blog_post.save_revision()

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])
)

# Check that the new string hasn't been submitted
string_segment = StringSegment.objects.get(context__path="test_charfield")
self.assertEqual(string_segment.string.data, "Test content")

# The FR version shouldn't be updated yet
self.fr_blog_post.refresh_from_db()
self.assertEqual(self.fr_blog_post.test_charfield, "Test content")

def test_post_update_draft_page_translation_with_use_machine_translation(self):
self.en_blog_post.test_charfield = "Edited blog post"
self.en_blog_post.save_revision()

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

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

self.fr_blog_post.refresh_from_db()
self.assertEqual(self.fr_blog_post.test_charfield, "Test content")

# Check that the translation is done, awaiting publication
fr = Locale.objects.get(language_code="fr")
translation_source = TranslationSource.objects.get_for_instance_or_none(
self.en_blog_post
)
translation = translation_source.translations.get(target_locale=fr)

string_segment = translation.source.stringsegment_set.get(
string__data="Edited blog post"
)
string_translation = StringTranslation.objects.get(
translation_of_id=string_segment.string_id,
locale=fr,
context_id=string_segment.context_id,
)
self.assertEqual(string_translation.data, "post blog Edited")
5 changes: 4 additions & 1 deletion wagtail_localize/views/update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ def post(self, request, **kwargs):

@transaction.atomic
def form_valid(self, form):
self.object.update_from_db()
# To avoid the unintentional publication of draft content from the source, we only use live content if publication is requested.
self.object.update_from_db(
use_draft_content=not form.cleaned_data["publish_translations"]
)

enabled_translations = self.object.translations.filter(enabled=True)
if form.cleaned_data.get("use_machine_translation"):
Expand Down