From 7022d0e37ad16c913d87d877f5b720b36a4d9472 Mon Sep 17 00:00:00 2001 From: samgans <63362199+samgans@users.noreply.github.com> Date: Thu, 1 Oct 2020 19:14:12 +0300 Subject: [PATCH] Fixed an issue with copying unsaved Pages (#6420) --- wagtail/core/models.py | 4 ++++ wagtail/core/tests/test_page_model.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/wagtail/core/models.py b/wagtail/core/models.py index 2d2670d62e..1017418597 100644 --- a/wagtail/core/models.py +++ b/wagtail/core/models.py @@ -1756,6 +1756,10 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase): :param log_action flag for logging the action. Pass None to skip logging. Can be passed an action string. Defaults to 'wagtail.copy' """ + + if self._state.adding: + raise RuntimeError('Page.copy() called on an unsaved page') + exclude_fields = self.default_exclude_fields_in_copy + self.exclude_fields_in_copy + (exclude_fields or []) specific_self = self.specific if keep_live: diff --git a/wagtail/core/tests/test_page_model.py b/wagtail/core/tests/test_page_model.py index b51862a60c..ff78b5d142 100644 --- a/wagtail/core/tests/test_page_model.py +++ b/wagtail/core/tests/test_page_model.py @@ -1484,6 +1484,12 @@ class TestCopyPage(TestCase): # reset excluded fields for future tests EventPage.exclude_fields_in_copy = [] + def test_copy_unsaved_page(self): + """Test that unsaved page will not be copied.""" + new_page = SimplePage(slug='testpurp', title='testpurpose') + with self.assertRaises(RuntimeError): + new_page.copy() + class TestCopyForTranslation(TestCase): fixtures = ['test.json']