Implemented recursive copying

This commit is contained in:
Karl Hobley 2014-06-26 09:45:51 +01:00
parent 39f9b9b0c2
commit 069406f86b
2 changed files with 16 additions and 3 deletions

View file

@ -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

View file

@ -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/')