Make Page.copy also copy revisions

pull/568/head
Karl Hobley 2014-08-22 15:34:17 +01:00
rodzic 9f8a5f2379
commit 3857576f90
2 zmienionych plików z 38 dodań i 1 usunięć
wagtail/wagtailcore

Wyświetl plik

@ -601,7 +601,7 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed
new_self.save()
new_self._update_descendant_url_paths(old_url_path, new_url_path)
def copy(self, recursive=False, to=None, update_attrs=None):
def copy(self, recursive=False, to=None, update_attrs=None, copy_revisions=True):
# Make a copy
page_copy = Page.objects.get(id=self.id).specific
page_copy.pk = None
@ -631,6 +631,13 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed
setattr(child_object, parental_key_name, page_copy.id)
child_object.save()
# Copy revisions
if copy_revisions:
for revision in self.revisions.all():
revision.pk = None
revision.page = page_copy
revision.save()
# Copy child pages
if recursive:
for child_page in self.get_children():

Wyświetl plik

@ -413,6 +413,19 @@ class TestCopyPage(TestCase):
self.assertEqual(new_christmas_event.advert_placements.count(), 1, "Child objects defined on the superclass weren't copied")
self.assertEqual(christmas_event.advert_placements.count(), 1, "Child objects defined on the superclass were removed from the original page")
def test_copy_page_copies_revisions(self):
christmas_event = EventPage.objects.get(url_path='/home/events/christmas/')
christmas_event.save_revision()
# Copy it
new_christmas_event = christmas_event.copy(update_attrs={'title': "New christmas event", 'slug': 'new-christmas-event'})
# Check that the revisions were copied
self.assertEqual(new_christmas_event.revisions.count(), 1, "Revisions weren't copied")
# Check that the revisions weren't removed from old page
self.assertEqual(christmas_event.revisions.count(), 1, "Revisions were removed from the original page")
def test_copy_page_copies_child_objects_with_nonspecific_class(self):
# Get chrismas page as Page instead of EventPage
christmas_event = Page.objects.get(url_path='/home/events/christmas/')
@ -458,3 +471,20 @@ class TestCopyPage(TestCase):
# Check that the speakers weren't removed from old page
self.assertEqual(old_christmas_event.specific.speakers.count(), 1, "Child objects were removed from the original page")
def test_copy_page_copies_recursively_with_revisions(self):
events_index = EventIndex.objects.get(url_path='/home/events/')
old_christmas_event = events_index.get_children().filter(slug='christmas').first()
old_christmas_event.save_revision()
# Copy it
new_events_index = events_index.copy(recursive=True, update_attrs={'title': "New events index", 'slug': 'new-events-index'})
# Get christmas event
new_christmas_event = new_events_index.get_children().filter(slug='christmas').first()
# Check that the revisions were copied
self.assertEqual(new_christmas_event.specific.revisions.count(), 1, "Revisions weren't copied")
# Check that the revisions weren't removed from old page
self.assertEqual(old_christmas_event.specific.revisions.count(), 1, "Revisions were removed from the original page")