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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the latest draft for translation (#7754) #7755

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions wagtail/actions/copy_for_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ def check(self, skip_permission_checks=False):
def walk(self, current_page):
for child_page in current_page.get_children():
self._copy_for_translation(
child_page,
child_page.live
and child_page
or child_page.get_latest_revision_as_page(),
self.locale,
self.copy_parents,
self.alias,
Expand Down Expand Up @@ -151,7 +153,11 @@ def execute(self, skip_permission_checks=False):
self.check(skip_permission_checks=skip_permission_checks)

translated_page = self._copy_for_translation(
self.page, self.locale, self.copy_parents, self.alias, self.exclude_fields
self.page.live and self.page or self.page.get_latest_revision_as_page(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.page if self.page.live else self.page.get_latest_revision_as_page() is the more familiar syntax I think, but happy with this logic.

self.locale,
self.copy_parents,
self.alias,
self.exclude_fields,
)

if self.include_subtree:
Expand Down
71 changes: 71 additions & 0 deletions wagtail/admin/tests/api/test_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from wagtail.models import GroupPagePermission, Locale, Page, PageLogEntry
from wagtail.test.demosite import models
from wagtail.test.i18n.models import TestPage
from wagtail.test.testapp.models import (
EventIndex,
EventPage,
Expand Down Expand Up @@ -1775,6 +1776,76 @@ def test_copy_for_translation_unknown_locale(self):
content = json.loads(response.content.decode("utf-8"))
self.assertEqual(content, {"message": "No Locale matches the given query."})

def test_translating_latest_non_draft_page_revision(self):
old_index_title = self.en_eventindex.title
old_post_title = self.en_eventpage.title
new_index_title = old_index_title + "-77777"
new_post_title = old_post_title + "-77777"
self.en_eventindex.title = new_index_title
self.en_eventindex.save_revision(log_action=True)
self.en_eventpage.title = new_post_title
self.en_eventpage.save_revision(log_action=True)

response = self.get_response(
self.en_eventindex.id,
{"locale": "fr", "copy_parents": True, "recursive": True},
)

assert response.status_code == 201

new_index_page = [
trans_page
for trans_page in self.en_eventindex.get_translations()
if trans_page.locale.language_code == "fr"
][0]
assert new_index_page.title == old_index_title
new_post_page = [
trans_page
for trans_page in self.en_eventpage.get_translations()
if trans_page.locale.language_code == "fr"
][0]
assert new_post_page.title == old_post_title

def test_translating_latest_draft_page_revision(self):
"""In case when Page have only draft revisions"""

draft_index_page = TestPage(title="Draft Blog", slug="draft_blog", live=False)
self.en_homepage.add_child(instance=draft_index_page)
draft_blog_post = TestPage(
title="Draft Blog post", slug="draft_blog-post", live=False
)
draft_index_page.add_child(instance=draft_blog_post)

old_index_title = draft_index_page.title
new_index_title = old_index_title + "-77777"
draft_index_page.title = new_index_title
draft_index_page.save_revision(log_action=True)

old_page_title = draft_blog_post.title
new_page_title = old_page_title + "-77777"
draft_blog_post.title = new_page_title
draft_blog_post.save_revision(log_action=True)

response = self.get_response(
draft_index_page.id,
{"locale": "fr", "copy_parents": True, "recursive": True},
)

assert response.status_code == 201

new_index_page = [
trans_page
for trans_page in draft_index_page.get_translations()
if trans_page.locale.language_code == "fr"
][0]
assert new_index_page.title == new_index_title
new_post_page = [
trans_page
for trans_page in draft_blog_post.get_translations()
if trans_page.locale.language_code == "fr"
][0]
assert new_post_page.title == new_page_title


class TestCreatePageAliasAction(AdminAPITestCase, TestCase):
fixtures = ["test.json"]
Expand Down