From bea17ac8b6831cae936c6e97b5c24c9af24304e2 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Tue, 20 Jul 2021 18:14:29 +0100 Subject: [PATCH] Add a log action for copy_for_translation operation (#7352) Currently, when a user copies a page for translation, the 'copy' log action is used. This adds a more specific 'copy_for_translation' operation to be used instead when the user is translating. --- CHANGELOG.txt | 1 + docs/advanced_topics/audit_log.rst | 1 + docs/releases/2.15.rst | 1 + wagtail/core/models/__init__.py | 13 +++++++++++-- wagtail/core/tests/test_page_model.py | 13 +++++++++++++ wagtail/core/wagtail_hooks.py | 11 +++++++++++ 6 files changed, 38 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index f791b9cea4..fc676cc30a 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -5,6 +5,7 @@ Changelog ~~~~~~~~~~~~~~~~~ * Support `min_num` / `max_num` options on ListBlock (Matt Westcott) + * Added a distinct `wagtail.copy_for_translation` log action type (Karl Hobley) * Fix: Delete button is now correct colour on snippets and modeladmin listings (Brandon Murch) diff --git a/docs/advanced_topics/audit_log.rst b/docs/advanced_topics/audit_log.rst index 72e17ba451..358f3e301a 100644 --- a/docs/advanced_topics/audit_log.rst +++ b/docs/advanced_topics/audit_log.rst @@ -71,6 +71,7 @@ Action Notes ``wagtail.rename`` A page was renamed ``wagtail.revert`` The page was reverted to a previous draft ``wagtail.copy`` The page was copied to a new location +``wagtail.copy_for_translation`` The page was copied into a new locale for translation ``wagtail.move`` The page was moved to a new location ``wagtail.reorder`` The order of the page under it's parent was changed ``wagtail.view_restriction.create`` The page was restricted diff --git a/docs/releases/2.15.rst b/docs/releases/2.15.rst index efecceebdf..2a7fdd2962 100644 --- a/docs/releases/2.15.rst +++ b/docs/releases/2.15.rst @@ -16,6 +16,7 @@ Other features ~~~~~~~~~~~~~~ * Support ``min_num`` / ``max_num`` options on ListBlock (Matt Westcott) + * Added a distinct ``wagtail.copy_for_translation`` log action type (Karl Hobley) Bug fixes ~~~~~~~~~ diff --git a/wagtail/core/models/__init__.py b/wagtail/core/models/__init__.py index b10919a936..ead6165fe9 100644 --- a/wagtail/core/models/__init__.py +++ b/wagtail/core/models/__init__.py @@ -1681,11 +1681,19 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase): data={ 'page': { 'id': page_copy.id, - 'title': page_copy.get_admin_display_title() + 'title': page_copy.get_admin_display_title(), + 'locale': { + 'id': page_copy.locale_id, + 'language_code': page_copy.locale.language_code + } }, 'source': {'id': parent.id, 'title': parent.specific_deferred.get_admin_display_title()} if parent else None, 'destination': {'id': to.id, 'title': to.specific_deferred.get_admin_display_title()} if to else None, - 'keep_live': page_copy.live and keep_live + 'keep_live': page_copy.live and keep_live, + 'source_locale': { + 'id': self.locale_id, + 'language_code': self.locale.language_code + } }, ) if page_copy.live and keep_live: @@ -1942,6 +1950,7 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase): reset_translation_key=False, process_child_object=process_child_object, exclude_fields=exclude_fields, + log_action='wagtail.copy_for_translation', ) copy_for_translation.alters_data = True diff --git a/wagtail/core/tests/test_page_model.py b/wagtail/core/tests/test_page_model.py index 0bad2caaf9..f39053365d 100644 --- a/wagtail/core/tests/test_page_model.py +++ b/wagtail/core/tests/test_page_model.py @@ -18,6 +18,7 @@ from django.test.utils import override_settings from django.utils import timezone, translation from freezegun import freeze_time +from wagtail.core.log_actions import page_log_action_registry from wagtail.core.models import ( Comment, Locale, Page, PageLogEntry, PageManager, ParentNotTranslatedError, Site, get_page_models, get_translatable_models) @@ -2225,6 +2226,12 @@ class TestCopyForTranslation(TestCase): self.assertFalse(fr_homepage.live) self.assertTrue(fr_homepage.has_unpublished_changes) + # Check log + log_entry = PageLogEntry.objects.get(action='wagtail.copy_for_translation') + self.assertEqual(log_entry.data['source_locale']['language_code'], 'en') + self.assertEqual(log_entry.data['page']['locale']['language_code'], 'fr') + self.assertEqual(page_log_action_registry.format_message(log_entry), "Copied for translation from Root (English)") + def test_copy_homepage_slug_exists(self): # This test is the same as test_copy_homepage, but we will create another page with # the slug "home-fr" before translating. copy_for_translation should pick a different slug @@ -2254,6 +2261,12 @@ class TestCopyForTranslation(TestCase): # The slug should be the same when copying to another tree self.assertEqual(self.en_eventindex.slug, fr_eventindex.slug) + # Check log + log_entry = PageLogEntry.objects.get(action='wagtail.copy_for_translation') + self.assertEqual(log_entry.data['source_locale']['language_code'], 'en') + self.assertEqual(log_entry.data['page']['locale']['language_code'], 'fr') + self.assertEqual(page_log_action_registry.format_message(log_entry), "Copied for translation from Welcome to the Wagtail test site! (English)") + def test_copy_childpage_without_parent(self): # This test is the same as test_copy_childpage but we won't create the parent page first diff --git a/wagtail/core/wagtail_hooks.py b/wagtail/core/wagtail_hooks.py index 966353bd04..fdb64f5db7 100644 --- a/wagtail/core/wagtail_hooks.py +++ b/wagtail/core/wagtail_hooks.py @@ -9,6 +9,7 @@ from django.utils.translation import ngettext from wagtail.core import hooks from wagtail.core.models import PageViewRestriction from wagtail.core.rich_text.pages import PageLinkHandler +from wagtail.core.utils import get_content_languages def require_wagtail_login(next): @@ -127,6 +128,15 @@ def register_core_log_actions(actions): except KeyError: return _("Copied") + def copy_for_translation_message(data): + try: + return _('Copied for translation from %(title)s (%(locale)s)') % { + 'title': data['source']['title'], + 'locale': get_content_languages().get(data['source_locale']['language_code']) or '', + } + except KeyError: + return _("Copied for translation") + def create_alias_message(data): try: return _('Created an alias of %(title)s') % { @@ -312,6 +322,7 @@ def register_core_log_actions(actions): actions.register_action('wagtail.rename', _('Rename'), rename_message) actions.register_action('wagtail.revert', _('Revert'), revert_message) actions.register_action('wagtail.copy', _('Copy'), copy_message) + actions.register_action('wagtail.copy_for_translation', _('Copy for translation'), copy_for_translation_message) actions.register_action('wagtail.create_alias', _('Create alias'), create_alias_message) actions.register_action('wagtail.convert_alias', _('Convert alias into ordinary page'), convert_alias_message) actions.register_action('wagtail.move', _('Move'), move_message)