Skip to content

Commit

Permalink
Fix non-page model creation with `WAGTAILLOCALIZE_SYNC_LIVE_STATUS_ON…
Browse files Browse the repository at this point in the history
…_TRANSLATE = False` (#726)
  • Loading branch information
zerolab authored Oct 6, 2023
1 parent 088a6bc commit 6f4d8ab
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions wagtail_localize/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ def create_or_update_translation(
created = False

# Only pages can be saved as draft
# To-Do: add support for models using DraftStateMixin
if not publish and not isinstance(original, Page):
raise CannotSaveDraftError

Expand Down
6 changes: 5 additions & 1 deletion wagtail_localize/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ def create_translations(self, instance, include_related_objects=True):
# Determine whether to publish the translation.
if getattr(settings, "WAGTAILLOCALIZE_SYNC_LIVE_STATUS_ON_TRANSLATE", True):
publish = getattr(instance, "live", True)
else:
elif isinstance(instance, Page):
publish = False
else:
# we cannot save drafts for non-Page models, so set this to True
# To-Do: add support for models using DraftStateMixin
publish = True

try:
translation.save_target(user=self.user, publish=publish)
Expand Down
27 changes: 27 additions & 0 deletions wagtail_localize/tests/test_submit_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,33 @@ def test_post_submit_snippet_translation(self):
),
)

@override_settings(WAGTAILLOCALIZE_SYNC_LIVE_STATUS_ON_TRANSLATE=False)
def test_post_submit_snippet_translation_draft(self):
response = self.client.post(
reverse(
"wagtail_localize:submit_snippet_translation",
args=["wagtail_localize_test", "testsnippet", self.en_snippet.id],
),
{"locales": [self.fr_locale.id]},
)

translation = Translation.objects.get()
self.assertEqual(translation.source.locale, self.en_locale)
self.assertEqual(translation.target_locale, self.fr_locale)
self.assertTrue(translation.created_at)

# The translated snippet should've been created
translated_snippet = self.en_snippet.get_translation(self.fr_locale)
self.assertEqual(translated_snippet.field, "Test snippet")

self.assertRedirects(
response,
reverse(
f"wagtailsnippets_{translated_snippet._meta.app_label}_{translated_snippet._meta.model_name}:edit",
args=[quote(translated_snippet.pk)],
),
)

def test_post_submit_snippet_translation_into_multiple_locales(self):
response = self.client.post(
reverse(
Expand Down

0 comments on commit 6f4d8ab

Please sign in to comment.