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

Fix non-page model creation with WAGTAILLOCALIZE_SYNC_LIVE_STATUS_ON_TRANSLATE=False #726

Merged
merged 1 commit into from
Oct 6, 2023
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
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