mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-04-24 08:34:49 +00:00
Merge branch 'kaedroho-page-duplication'
This commit is contained in:
commit
e509871624
3 changed files with 127 additions and 0 deletions
11
wagtail/tests/fixtures/test.json
vendored
11
wagtail/tests/fixtures/test.json
vendored
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -622,6 +622,43 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, 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):
|
||||
# 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
|
||||
|
|
|
|||
|
|
@ -307,3 +307,82 @@ class TestPrevNextSiblings(TestCase):
|
|||
|
||||
# First element must always be the current page
|
||||
self.assertEqual(final_event.get_prev_siblings(inclusive=True).first(), final_event)
|
||||
|
||||
|
||||
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")
|
||||
|
|
|
|||
Loading…
Reference in a new issue