Added user attribute to Page.copy

This is used for setting the owner of the new pages and the user for the
new revision that gets created
pull/842/head
Karl Hobley 2014-12-02 14:03:09 +00:00
rodzic 7235c61fb7
commit ca7e285353
3 zmienionych plików z 27 dodań i 7 usunięć

Wyświetl plik

@ -665,11 +665,9 @@ def copy(request, page_id):
'slug': form.cleaned_data['new_slug'],
},
keep_live=(can_publish and form.cleaned_data.get('publish_copies')),
user=request.user,
)
# Assign user of this request as the owner of all the new pages
new_page.get_descendants(inclusive=True).update(owner=request.user)
# Give a success message back to the user
if form.cleaned_data.get('copy_subpages'):
messages.success(request, _("Page '{0}' and {1} subpages copied.").format(page.title, new_page.get_descendants().count()))

Wyświetl plik

@ -730,7 +730,7 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed
# Log
logger.info("Page moved: \"%s\" id=%d path=%s", self.title, self.id, new_url_path)
def copy(self, recursive=False, to=None, update_attrs=None, copy_revisions=True, keep_live=True):
def copy(self, recursive=False, to=None, update_attrs=None, copy_revisions=True, keep_live=True, user=None):
# Make a copy
page_copy = Page.objects.get(id=self.id).specific
page_copy.pk = None
@ -743,6 +743,9 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed
page_copy.live = False
page_copy.has_unpublished_changes = True
if user:
page_copy.owner = user
if update_attrs:
for field, value in update_attrs.items():
setattr(page_copy, field, value)
@ -793,16 +796,17 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed
revision.save()
# Create a new revision
# This code serves two purposes:
# This code serves a few purposes:
# * It makes sure update_attrs gets applied to the latest revision so the changes are reflected in the editor
# * It bumps the last_revision_created_at value so the new page gets ordered as if it was just created
# * It sets the user of the new revision so it's possible to see who copied the page by looking at its history
latest_revision = page_copy.get_latest_revision_as_page()
if update_attrs:
for field, value in update_attrs.items():
setattr(latest_revision, field, value)
latest_revision.save_revision()
latest_revision.save_revision(user=user)
# Log
logger.info("Page copied: \"%s\" id=%d from=%d", page_copy.title, page_copy.id, self.id)
@ -810,7 +814,7 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed
# Copy child pages
if recursive:
for child_page in self.get_children():
child_page.specific.copy(recursive=True, to=page_copy, copy_revisions=copy_revisions, keep_live=keep_live)
child_page.specific.copy(recursive=True, to=page_copy, copy_revisions=copy_revisions, keep_live=keep_live, user=user)
return page_copy

Wyświetl plik

@ -8,6 +8,7 @@ from django.test import TestCase, Client
from django.test.utils import override_settings
from django.http import HttpRequest, Http404
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth import get_user_model
from wagtail.wagtailcore.models import Page, Site
from wagtail.tests.models import EventPage, EventIndex, SimplePage, PageWithOldStyleRouteMethod, BusinessIndex, BusinessSubIndex, BusinessChild, StandardIndex
@ -582,6 +583,23 @@ class TestCopyPage(TestCase):
# 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")
def test_copy_page_updates_user(self):
event_editor = get_user_model().objects.get(username='eventeditor')
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'},
user=event_editor,
)
# Check that the owner has been updated
self.assertEqual(new_christmas_event.owner, event_editor)
# Check that the user on the last revision is correct
self.assertEqual(new_christmas_event.get_latest_revision().user, event_editor)
class TestSubpageTypeBusinessRules(TestCase):
def test_allowed_subpage_types(self):