Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page copy #379

Merged
merged 8 commits into from Jul 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions wagtail/tests/fixtures/test.json
Expand Up @@ -85,6 +85,17 @@
}
},

{
"pk": 1,
"model": "tests.eventpagespeaker",
"fields": {
"page": 4,
"first_name": "Santa",
"last_name": "Claus",
"sort_order": 0
}
},

{
"pk": 5,
"model": "wagtailcore.page",
Expand Down
37 changes: 37 additions & 0 deletions wagtail/wagtailcore/models.py
Expand Up @@ -556,6 +556,43 @@ def move(self, target, pos=None):
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):
# Make a copy
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

if update_attrs:
for field, value in update_attrs.items():
setattr(page_copy, field, value)

if to:
page_copy = to.add_child(instance=page_copy)
else:
page_copy = self.add_sibling(instance=page_copy)

# Copy child objects
specific_self = self.specific
for child_relation in getattr(specific_self._meta, 'child_relations', []):
parental_key_name = child_relation.field.attname
child_objects = getattr(specific_self, child_relation.get_accessor_name(), None)

if child_objects:
for child_object in child_objects.all():
child_object.pk = None
setattr(child_object, parental_key_name, page_copy.id)
child_object.save()

# Copy child pages
if recursive:
for child_page in self.get_children():
child_page.specific.copy(recursive=True, to=page_copy)

return page_copy

def permissions_for_user(self, user):
"""
Return a PagePermissionsTester object defining what actions the user can perform on this page
Expand Down
79 changes: 79 additions & 0 deletions wagtail/wagtailcore/tests/test_page_model.py
Expand Up @@ -224,3 +224,82 @@ def test_move_page(self):
christmas = events_index.get_children().get(slug='christmas')
self.assertEqual(christmas.depth, 5)
self.assertEqual(christmas.url_path, '/home/about-us/events/christmas/')


class TestCopyPage(TestCase):
fixtures = ['test.json']

def test_copy_page_copies(self):
about_us = SimplePage.objects.get(url_path='/home/about-us/')

# Copy it
new_about_us = about_us.copy(update_attrs={'title': "New about us", 'slug': 'new-about-us'})

# Check that new_about_us is correct
self.assertIsInstance(new_about_us, SimplePage)
self.assertEqual(new_about_us.title, "New about us")
self.assertEqual(new_about_us.slug, 'new-about-us')

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

# Copy it
new_christmas_event = christmas_event.copy(update_attrs={'title': "New christmas event", 'slug': 'new-christmas-event'})

# Check that the speakers were copied
self.assertEqual(new_christmas_event.speakers.count(), 1, "Child objects weren't copied")

# Check that the speakers weren't removed from old page
self.assertEqual(christmas_event.speakers.count(), 1, "Child objects 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/')

# Copy it
new_christmas_event = christmas_event.copy(update_attrs={'title': "New christmas event", 'slug': 'new-christmas-event'})

# Check that the type of the new page is correct
self.assertIsInstance(new_christmas_event, EventPage)

# Check that the speakers were copied
self.assertEqual(new_christmas_event.speakers.count(), 1, "Child objects weren't copied")

def test_copy_page_copies_recursively(self):
events_index = EventIndex.objects.get(url_path='/home/events/')

# Copy it
new_events_index = events_index.copy(recursive=True, update_attrs={'title': "New events index", 'slug': 'new-events-index'})

# Get christmas event
old_christmas_event = events_index.get_children().filter(slug='christmas').first()
new_christmas_event = new_events_index.get_children().filter(slug='christmas').first()

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

def test_copy_page_copies_recursively_with_child_objects(self):
events_index = EventIndex.objects.get(url_path='/home/events/')

# Copy it
new_events_index = events_index.copy(recursive=True, update_attrs={'title': "New events index", 'slug': 'new-events-index'})

# Get christmas event
old_christmas_event = events_index.get_children().filter(slug='christmas').first()
new_christmas_event = new_events_index.get_children().filter(slug='christmas').first()

# Check that the speakers were copied
self.assertEqual(new_christmas_event.specific.speakers.count(), 1, "Child objects weren't copied")

# 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")