Merge pull request #568 from kaedroho/page-copy-revisions

Make Page.copy also copy revisions
This commit is contained in:
Matt Westcott 2014-09-16 10:18:18 +01:00
commit 99761cdcdc
4 changed files with 71 additions and 1 deletions

View file

@ -3,3 +3,4 @@ coverage==3.7.1
flake8==2.2.1
mock==1.0.1
python-dateutil==2.2
pytz==2014.7

View file

@ -15,6 +15,7 @@ base =
elasticsearch==1.1.0
mock==1.0.1
python-dateutil==2.2
pytz==2014.7
Embedly
coverage

View file

@ -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,15 @@ 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.submitted_for_moderation = False
revision.approved_go_live_at = None
revision.page = page_copy
revision.save()
# Copy child pages
if recursive:
for child_page in self.get_children():

View file

@ -1,4 +1,7 @@
import warnings
import datetime
import pytz
from django.test import TestCase, Client
from django.test.utils import override_settings
@ -413,6 +416,45 @@ 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_revisions_and_doesnt_submit_for_moderation(self):
christmas_event = EventPage.objects.get(url_path='/home/events/christmas/')
christmas_event.save_revision(submitted_for_moderation=True)
# Copy it
new_christmas_event = christmas_event.copy(update_attrs={'title': "New christmas event", 'slug': 'new-christmas-event'})
# Check that the old revision is still submitted for moderation
self.assertTrue(christmas_event.revisions.first().submitted_for_moderation)
# Check that the new revision is not submitted for moderation
self.assertFalse(new_christmas_event.revisions.first().submitted_for_moderation)
def test_copy_page_copies_revisions_and_doesnt_schedule(self):
christmas_event = EventPage.objects.get(url_path='/home/events/christmas/')
christmas_event.save_revision(approved_go_live_at=datetime.datetime(2014, 9, 16, 9, 12, 00, tzinfo=pytz.utc))
# Copy it
new_christmas_event = christmas_event.copy(update_attrs={'title': "New christmas event", 'slug': 'new-christmas-event'})
# Check that the old revision is still scheduled
self.assertEqual(christmas_event.revisions.first().approved_go_live_at, datetime.datetime(2014, 9, 16, 9, 12, 00, tzinfo=pytz.utc))
# Check that the new revision is not scheduled
self.assertEqual(new_christmas_event.revisions.first().approved_go_live_at, None)
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 +500,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")