diff --git a/CHANGELOG.txt b/CHANGELOG.txt index e68fda4d52..687917f6a9 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -9,6 +9,7 @@ Changelog * Fix: Allow bulk publishing of pages without revisions (Andy Chosak) * Fix: Ensure that all descendant pages are logged when deleting a page, not just immediate children (Jake Howard) * Fix: Refactor `FormPagesListView` in wagtail.contrib.forms to avoid undefined `locale` variable when subclassing (Dan Braghis) + * Fix: page copy in Wagtail admin ignores `exclude_fields_in_copy` (John-Scott Atlakson) 2.16.1 (11.02.2022) diff --git a/docs/releases/2.16.2.md b/docs/releases/2.16.2.md index 26beb3c68a..63636e7eb7 100644 --- a/docs/releases/2.16.2.md +++ b/docs/releases/2.16.2.md @@ -15,6 +15,7 @@ * Allow bulk publishing of pages without revisions (Andy Chosak) * Ensure that all descendant pages are logged when deleting a page, not just immediate children (Jake Howard) * Refactor `FormPagesListView` in wagtail.contrib.forms to avoid undefined `locale` variable when subclassing (Dan Braghis) + * Ensure page copy in Wagtail admin doesn't ignore `exclude_fields_in_copy` (John-Scott Atlakson) ## Upgrade considerations diff --git a/wagtail/admin/tests/api/test_pages.py b/wagtail/admin/tests/api/test_pages.py index d9e0f8f829..dc1da6b2f6 100644 --- a/wagtail/admin/tests/api/test_pages.py +++ b/wagtail/admin/tests/api/test_pages.py @@ -11,7 +11,8 @@ from wagtail.api.v2.tests.test_pages import TestPageDetail, TestPageListing from wagtail.core import hooks from wagtail.core.models import GroupPagePermission, Locale, Page, PageLogEntry from wagtail.tests.demosite import models -from wagtail.tests.testapp.models import EventIndex, EventPage, SimplePage, StreamPage +from wagtail.tests.testapp.models import ( + EventIndex, EventPage, PageWithExcludedCopyField, SimplePage, StreamPage) from wagtail.users.models import UserProfile from .utils import AdminAPITestCase @@ -880,6 +881,20 @@ class TestCopyPageAction(AdminAPITestCase): new_page = Page.objects.get(id=content['id']) self.assertEqual(new_page.slug, 'new-slug') + def test_copy_page_with_exclude_fields_in_copy(self): + response = self.get_response(21, {}) + + self.assertEqual(response.status_code, 201) + content = json.loads(response.content.decode("utf-8")) + + original_page = PageWithExcludedCopyField.objects.get(pk=21) + new_page = PageWithExcludedCopyField.objects.get(id=content["id"]) + self.assertEqual(new_page.content, original_page.content) + self.assertNotEqual(new_page.special_field, original_page.special_field) + self.assertEqual( + new_page.special_field, new_page._meta.get_field("special_field").default + ) + def test_copy_page_destination(self): response = self.get_response(3, { 'destination_page_id': 3 diff --git a/wagtail/admin/tests/pages/test_copy_page.py b/wagtail/admin/tests/pages/test_copy_page.py index 34cf2c885f..a381b66f8a 100644 --- a/wagtail/admin/tests/pages/test_copy_page.py +++ b/wagtail/admin/tests/pages/test_copy_page.py @@ -4,7 +4,7 @@ from django.test import TestCase from django.urls import reverse from wagtail.core.models import GroupPagePermission, Page -from wagtail.tests.testapp.models import SimplePage +from wagtail.tests.testapp.models import PageWithExcludedCopyField, SimplePage from wagtail.tests.utils import WagtailTestUtils @@ -587,3 +587,33 @@ class TestPageCopy(TestCase, WagtailTestUtils): # We only need to check that it didn't crash self.assertEqual(response.status_code, 302) + + def test_page_with_exclude_fields_in_copy(self): + original_page = self.test_page.add_child( + instance=PageWithExcludedCopyField( + title="Page with exclude_fields_in_copy", + slug="page-with-exclude-fields-in-copy", + content="Copy me", + special_field="Don't copy me", + live=True, + has_unpublished_changes=False, + ) + ) + post_data = { + "new_title": f"{original_page.title} 2", + "new_slug": f"{original_page.slug}-2", + "new_parent_page": str(self.root_page.id), + "copy_subpages": False, + "publish_copies": False, + "alias": False, + } + self.client.post( + reverse("wagtailadmin_pages:copy", args=(original_page.id,)), post_data + ) + # Get copy + page_copy = PageWithExcludedCopyField.objects.get(slug=post_data["new_slug"]) + self.assertEqual(page_copy.content, original_page.content) + self.assertNotEqual(page_copy.special_field, original_page.special_field) + self.assertEqual( + page_copy.special_field, page_copy._meta.get_field("special_field").default + ) diff --git a/wagtail/core/actions/copy_page.py b/wagtail/core/actions/copy_page.py index 8912789439..701591095d 100644 --- a/wagtail/core/actions/copy_page.py +++ b/wagtail/core/actions/copy_page.py @@ -73,8 +73,12 @@ class CopyPageAction: raise CopyPagePermissionError("You do not have permission to publish a page at the destination") def _copy_page(self, page, to=None, update_attrs=None, exclude_fields=None, _mpnode_attrs=None): - exclude_fields = page.default_exclude_fields_in_copy + page.exclude_fields_in_copy + (exclude_fields or []) specific_page = page.specific + exclude_fields = ( + specific_page.default_exclude_fields_in_copy + + specific_page.exclude_fields_in_copy + + (exclude_fields or []) + ) if self.keep_live: base_update_attrs = { 'alias_of': None, diff --git a/wagtail/tests/testapp/fixtures/test.json b/wagtail/tests/testapp/fixtures/test.json index 71226c31d3..edfd08b864 100644 --- a/wagtail/tests/testapp/fixtures/test.json +++ b/wagtail/tests/testapp/fixtures/test.json @@ -25,7 +25,7 @@ "fields": { "title": "Welcome to the Wagtail test site!", "draft_title": "Welcome to the Wagtail test site!", - "numchild": 9, + "numchild": 10, "show_in_menus": false, "live": true, "depth": 2, @@ -616,7 +616,30 @@ "slug": "does-not-exist" } }, - +{ + "pk": 21, + "model": "wagtailcore.page", + "fields": { + "title": "This page has a field that shouldn't be copied", + "draft_title": "This page has a field that shouldn't be copied", + "numchild": 0, + "show_in_menus": true, + "live": true, + "depth": 3, + "content_type": ["tests", "pagewithexcludedcopyfield"], + "path": "000100010010", + "url_path": "/home/page-with-exclude-fields-in-copy/", + "slug": "page-with-exclude-fields-in-copy" + } +}, +{ + "pk": 21, + "model": "tests.pagewithexcludedcopyfield", + "fields": { + "content": "Copy me.", + "special_field": "Don't copy me." + } +}, { "pk": 1, "model": "wagtailcore.site",