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

Redirect to newly translate page's edit view when submitting a translation for a single locale #518

Merged
merged 5 commits into from
Feb 10, 2022
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
5 changes: 3 additions & 2 deletions wagtail_localize/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ class TranslationComponentManager(BaseComponentManager):
"""
The translation component manager handles all registered components for translation.

Classes registered as translation components should implement a `get_or_create_from_source_and_translation_data`
method which takes `translation_source`, `translations` and kwargs as parameters.
Classes registered as translation components should implement a
`get_or_create_from_source_and_translation_data` method which takes
`translation_source`, `translations` and kwargs as parameters.
"""

@classmethod
Expand Down
73 changes: 39 additions & 34 deletions wagtail_localize/tests/test_submit_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test(self):
self.assertContains(
response,
(
f'<a href="/admin/localize/submit/page/{self.en_blog_index.id}/?next=%2Fadmin%2Fpages%2F{self.en_homepage.id}%2F" '
f'<a href="/admin/localize/submit/page/{self.en_blog_index.id}/" '
'aria-label="" class="u-link is-live ">'
"\n Translate this page\n </a>"
),
Expand Down Expand Up @@ -229,10 +229,6 @@ def test_post_submit_page_translation(self):
{"locales": [self.fr_locale.id]},
)

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

translation = Translation.objects.get()
self.assertEqual(translation.source.locale, self.en_locale)
self.assertEqual(translation.target_locale, self.fr_locale)
Expand All @@ -242,6 +238,10 @@ def test_post_submit_page_translation(self):
translated_page = self.en_blog_index.get_translation(self.fr_locale)
self.assertTrue(translated_page.live)

self.assertRedirects(
response, reverse("wagtailadmin_pages:edit", args=[translated_page.id])
)

def test_post_submit_page_translation_submits_linked_snippets(self):
self.en_blog_index.test_snippet = TestSnippet.objects.create(
field="My test snippet"
Expand All @@ -256,10 +256,6 @@ def test_post_submit_page_translation_submits_linked_snippets(self):
{"locales": [self.fr_locale.id]},
)

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

page_translation = Translation.objects.get(
source__specific_content_type=ContentType.objects.get_for_model(TestPage)
)
Expand All @@ -271,6 +267,10 @@ def test_post_submit_page_translation_submits_linked_snippets(self):
translated_page = self.en_blog_index.get_translation(self.fr_locale)
self.assertTrue(translated_page.live)

self.assertRedirects(
response, reverse("wagtailadmin_pages:edit", args=[translated_page.id])
)

snippet_translation = Translation.objects.get(
source__specific_content_type=ContentType.objects.get_for_model(TestSnippet)
)
Expand Down Expand Up @@ -313,8 +313,10 @@ def test_post_submit_page_translation_including_subtree(self):
{"locales": [self.fr_locale.id], "include_subtree": "on"},
)

translated_page = self.en_blog_index.get_translation(self.fr_locale)

self.assertRedirects(
response, reverse("wagtailadmin_explore", args=[self.en_homepage.id])
response, reverse("wagtailadmin_pages:edit", args=[translated_page.id])
)

# Check multiple translations were created
Expand All @@ -329,10 +331,6 @@ def test_post_submit_page_translation_with_untranslated_parent(self):
{"locales": [self.fr_locale.id]},
)

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

# One translation should be created
fr_translation = Translation.objects.get()
self.assertEqual(fr_translation.source.locale, self.en_locale)
Expand All @@ -343,6 +341,10 @@ def test_post_submit_page_translation_with_untranslated_parent(self):
translated_page = self.en_blog_post.get_translation(self.fr_locale)
self.assertTrue(translated_page.live)

self.assertRedirects(
response, reverse("wagtailadmin_pages:edit", args=[translated_page.id])
)

# The parent should've been created as an alias page
translated_parent_page = self.en_blog_index.get_translation(self.fr_locale)
self.assertTrue(translated_parent_page.live)
Expand All @@ -364,10 +366,6 @@ def test_post_submit_page_translation_with_untranslated_grandparent(self):
{"locales": [es_locale.id]},
)

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

# One translation should be created
fr_translation = Translation.objects.get()
self.assertEqual(fr_translation.source.locale, self.en_locale)
Expand All @@ -378,6 +376,10 @@ def test_post_submit_page_translation_with_untranslated_grandparent(self):
translated_page = self.en_blog_post.get_translation(es_locale)
self.assertTrue(translated_page.live)

self.assertRedirects(
response, reverse("wagtailadmin_pages:edit", args=[translated_page.id])
)

# The parent should've been created as an alias page
translated_parent_page = self.en_blog_index.get_translation(es_locale)
self.assertTrue(translated_parent_page.live)
Expand Down Expand Up @@ -443,10 +445,6 @@ def test_post_submit_page_translation_reactivates_deleted_translation(self):
{"locales": [self.fr_locale.id]},
)

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

# Check that the translation was reactivated
# Note, .get() here tests that another translation record wasn't created
translation = Translation.objects.get()
Expand All @@ -458,6 +456,10 @@ def test_post_submit_page_translation_reactivates_deleted_translation(self):
translated_page = self.en_blog_index.get_translation(self.fr_locale)
self.assertTrue(translated_page.live)

self.assertRedirects(
response, reverse("wagtailadmin_pages:edit", args=[translated_page.id])
)

def test_post_submit_page_translation_doesnt_reactivate_deactivated_translation(
self,
):
Expand Down Expand Up @@ -504,8 +506,9 @@ def test_post_submit_page_translation_with_global_disabled_mode(self):
{"locales": [self.fr_locale.id]},
)

translated_page = self.en_blog_index.get_translation(self.fr_locale)
self.assertRedirects(
response, reverse("wagtailadmin_explore", args=[self.en_homepage.id])
response, reverse("wagtailadmin_pages:edit", args=[translated_page.id])
)

translation = Translation.objects.get()
Expand All @@ -526,8 +529,9 @@ def test_post_submit_page_translation_with_disabled_mode_per_page_type(self):
{"locales": [self.fr_locale.id]},
)

translated_page = custom_page.get_translation(self.fr_locale)
self.assertRedirects(
response, reverse("wagtailadmin_explore", args=[self.en_homepage.id])
response, reverse("wagtailadmin_pages:edit", args=[translated_page.id])
)

translation = Translation.objects.get()
Expand All @@ -551,8 +555,9 @@ def test_post_submit_page_translation_with_global_mode_disabled_but_enabled_per_
{"locales": [self.fr_locale.id]},
)

translated_page = custom_page.get_translation(self.fr_locale)
self.assertRedirects(
response, reverse("wagtailadmin_explore", args=[self.en_homepage.id])
response, reverse("wagtailadmin_pages:edit", args=[translated_page.id])
)

translation = Translation.objects.get()
Expand Down Expand Up @@ -619,7 +624,7 @@ def test(self):
self.assertContains(
response,
(
f'href="/admin/localize/submit/snippet/wagtail_localize_test/testsnippet/{self.en_snippet.id}/?next=%2Fadmin%2Fsnippets%2Fwagtail_localize_test%2Ftestsnippet%2F"{extra}>Translate</a>'
f'href="/admin/localize/submit/snippet/wagtail_localize_test/testsnippet/{self.en_snippet.id}/"{extra}>Translate</a>'
),
)

Expand Down Expand Up @@ -790,14 +795,6 @@ def test_post_submit_snippet_translation(self):
{"locales": [self.fr_locale.id]},
)

self.assertRedirects(
response,
reverse(
"wagtailsnippets:edit",
args=["wagtail_localize_test", "testsnippet", self.en_snippet.id],
),
)

translation = Translation.objects.get()
self.assertEqual(translation.source.locale, self.en_locale)
self.assertEqual(translation.target_locale, self.fr_locale)
Expand All @@ -807,6 +804,14 @@ def test_post_submit_snippet_translation(self):
translated_snippet = self.en_snippet.get_translation(self.fr_locale)
self.assertEqual(translated_snippet.field, "Test snippet")

self.assertRedirects(
response,
reverse(
"wagtailsnippets:edit",
args=["wagtail_localize_test", "testsnippet", translated_snippet.id],
),
)

mixxorz marked this conversation as resolved.
Show resolved Hide resolved
def test_post_submit_snippet_translation_into_multiple_locales(self):
response = self.client.post(
reverse(
Expand Down
3 changes: 2 additions & 1 deletion wagtail_localize/tests/test_translation_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ def test_post_submit_page_translation(self):
},
)

translated_page = self.en_blog_index.get_translation(self.fr_locale)
self.assertRedirects(
response, reverse("wagtailadmin_explore", args=[self.en_homepage.id])
response, reverse("wagtailadmin_pages:edit", args=[translated_page.id])
)

self.assertEqual(CustomTranslationData.objects.count(), 1)
Expand Down
32 changes: 28 additions & 4 deletions wagtail_localize/views/submit_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def get_form(self):
def get_success_url(self):
return get_valid_next_url_from_request(self.request)

def get_default_success_url(self):
def get_default_success_url(self, translated_object=None):
pass

def get_context_data(self, **kwargs):
Expand Down Expand Up @@ -210,8 +210,12 @@ def _walk(current_page):

_walk(self.object)

single_translated_object = None
if len(form.cleaned_data["locales"]) == 1:
locales = form.cleaned_data["locales"][0].get_display_name()
single_translated_object = self.object.get_translation(
form.cleaned_data["locales"][0]
)

else:
# Note: always plural
Expand All @@ -222,7 +226,10 @@ def _walk(current_page):
# TODO: Button that links to page in translations report when we have it
messages.success(self.request, self.get_success_message(locales))

return redirect(self.get_success_url() or self.get_default_success_url())
return redirect(
self.get_success_url()
or self.get_default_success_url(single_translated_object)
zerolab marked this conversation as resolved.
Show resolved Hide resolved
)

def dispatch(self, request, *args, **kwargs):
if not request.user.has_perms(["wagtail_localize.submit_translation"]):
Expand Down Expand Up @@ -250,7 +257,12 @@ def get_object(self):

return page

def get_default_success_url(self):
def get_default_success_url(self, translated_page=None):
if translated_page:
# If the editor chose a single locale to translate to, redirect to
# the newly translated page's edit view.
return reverse("wagtailadmin_pages:edit", args=[translated_page.id])

return reverse("wagtailadmin_explore", args=[self.get_object().get_parent().id])

def get_success_message(self, locales):
Expand All @@ -275,7 +287,19 @@ def get_object(self):

return get_object_or_404(model, pk=unquote(self.kwargs["pk"]))

def get_default_success_url(self):
def get_default_success_url(self, translated_snippet=None):
if translated_snippet:
# If the editor chose a single locale to translate to, redirect to
# the newly translated snippet's edit view.
return reverse(
"wagtailsnippets:edit",
args=[
self.kwargs["app_label"],
self.kwargs["model_name"],
translated_snippet.pk,
],
)

return reverse(
"wagtailsnippets:edit",
args=[
Expand Down
4 changes: 0 additions & 4 deletions wagtail_localize/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ def page_listing_more_buttons(page, page_perms, is_parent=False, next_url=None):

if has_locale_to_translate_to:
url = reverse("wagtail_localize:submit_page_translation", args=[page.id])
if next_url is not None:
url += "?" + urlencode({"next": next_url})

yield wagtailadmin_widgets.Button(
_("Translate this page"), url, priority=60
Expand Down Expand Up @@ -188,8 +186,6 @@ def register_snippet_listing_buttons(snippet, user, next_url=None):
"wagtail_localize:submit_snippet_translation",
args=[model._meta.app_label, model._meta.model_name, quote(snippet.pk)],
)
if next_url is not None:
url += "?" + urlencode({"next": next_url})

yield SnippetListingButton(
_("Translate"),
Expand Down