diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 7945fa53a..a0bae2c99 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -556,15 +556,21 @@ class Page(MP_Node, ClusterableModel, Indexed): new_self.save() new_self._update_descendant_url_paths(old_url_path, new_url_path) - def copy(self, recursive=False, **update_fields): + def copy(self, recursive=False, to=None, **update_fields): page_copy = Page.objects.get(id=self.id).specific page_copy.pk = None page_copy.id = None + page_copy.depth = None + page_copy.numchild = 0 + page_copy.path = None for field, value in update_fields.items(): setattr(page_copy, field, value) - page_copy = self.add_sibling(instance=page_copy) + if to: + page_copy = to.add_child(instance=page_copy) + else: + page_copy = self.add_sibling(instance=page_copy) # Copy child objects if hasattr(self._meta, 'child_relations'): @@ -573,7 +579,8 @@ class Page(MP_Node, ClusterableModel, Indexed): # Copy child pages if recursive: - pass + for child_page in self.get_children(): + child_page.copy(recursive=True, to=page_copy) return page_copy diff --git a/wagtail/wagtailcore/tests/test_page_model.py b/wagtail/wagtailcore/tests/test_page_model.py index 86df5bf7c..fd45ca260 100644 --- a/wagtail/wagtailcore/tests/test_page_model.py +++ b/wagtail/wagtailcore/tests/test_page_model.py @@ -243,6 +243,9 @@ class TestCopyPage(TestCase): # Check that new_about_us is a different page self.assertNotEqual(about_us.id, new_about_us.id) + # Check that the url path was updated + self.assertEqual(new_about_us.url_path, '/home/new-about-us/') + def test_copy_page_copies_child_objects(self): christmas_event = EventPage.objects.get(url_path='/home/events/christmas/') @@ -268,3 +271,6 @@ class TestCopyPage(TestCase): # Check that the event exists in both places self.assertNotEqual(new_christmas_event, None, "Child pages weren't copied") self.assertNotEqual(old_christmas_event, None, "Child pages were removed from original page") + + # Check that the url path was updated + self.assertEqual(new_christmas_event.url_path, '/home/new-events-index/christmas/')