From 6f4d8abc2b38ed123ca13b9ad4a231661a02eb01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Braghi=C8=99?= Date: Fri, 6 Oct 2023 16:45:16 +0100 Subject: [PATCH] Fix non-page model creation with `WAGTAILLOCALIZE_SYNC_LIVE_STATUS_ON_TRANSLATE = False` (#726) --- wagtail_localize/models.py | 1 + wagtail_localize/operations.py | 6 ++++- .../tests/test_submit_translations.py | 27 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/wagtail_localize/models.py b/wagtail_localize/models.py index 7d90485e5..07071622c 100644 --- a/wagtail_localize/models.py +++ b/wagtail_localize/models.py @@ -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 diff --git a/wagtail_localize/operations.py b/wagtail_localize/operations.py index 6835f918d..edbf08046 100644 --- a/wagtail_localize/operations.py +++ b/wagtail_localize/operations.py @@ -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) diff --git a/wagtail_localize/tests/test_submit_translations.py b/wagtail_localize/tests/test_submit_translations.py index 7f14451b3..4ae20d9ee 100644 --- a/wagtail_localize/tests/test_submit_translations.py +++ b/wagtail_localize/tests/test_submit_translations.py @@ -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(