Skip to content

Commit

Permalink
Merge pull request #3829 from yakky/feature/fix_copy_page
Browse files Browse the repository at this point in the history
Fix Page.copy_page by filtering pages on site in Page.get_descendants() ...
  • Loading branch information
yakky committed Feb 11, 2015
2 parents f0f6420 + 63d66f3 commit f4d1974
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
18 changes: 9 additions & 9 deletions cms/models/pagemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ class Meta:
('publish_page', 'Can publish page'),
('edit_static_placeholder', 'Can edit static placeholders'),
)
unique_together = (("publisher_is_draft", "application_namespace"), ("reverse_id", "site", "publisher_is_draft"))
unique_together = (("publisher_is_draft", "application_namespace"),
("reverse_id", "site", "publisher_is_draft"))
verbose_name = _('page')
verbose_name_plural = _('pages')
ordering = ('path',)
Expand Down Expand Up @@ -334,7 +335,7 @@ def copy_page(self, target, site, position='first-child',
placeholders = list(page.get_placeholders())
origin_id = page.id
# create a copy of this page by setting pk = None (=new instance)
page.old_pk = page.pk
page.old_pk = old_pk = page.pk
page.pk = None
page.path = None
page.depth = None
Expand All @@ -352,12 +353,14 @@ def copy_page(self, target, site, position='first-child',
else:
page.parent = None
page.save()
page.move(target, pos=position)
if target:
page = page.move(target, pos=position)
page.old_pk = old_pk
else:
count = 1
found = False
for prnt in tree:
if tree[0].pk == self.pk and page.parent_id == self.pk and count==1:
if tree[0].pk == self.pk and page.parent_id == self.pk and count == 1:
count += 1
continue
elif prnt.old_pk == page.parent_id:
Expand All @@ -372,9 +375,6 @@ def copy_page(self, target, site, position='first-child',
page.save()
tree.append(page)




# copy permissions if necessary
if get_cms_setting('PERMISSION') and copy_permissions:
from cms.models.permissionmodels import PagePermission
Expand Down Expand Up @@ -734,9 +734,9 @@ def get_descendants(self, include_self=False):
include the node itself
"""
if include_self:
return self.__class__.get_tree(self)
return self.__class__.get_tree(self).filter(site_id=self.site_id)
else:
return self.__class__.get_tree(self).exclude(pk=self.pk)
return self.__class__.get_tree(self).exclude(pk=self.pk).filter(site_id=self.site_id)

def get_cached_ancestors(self):
if not hasattr(self, "ancestors_ascending"):
Expand Down
25 changes: 25 additions & 0 deletions cms/tests/page.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement
import datetime
from django.db import transaction
from cms.exceptions import PublicIsUnmodifiable, PublicVersionNeeded
from cms.utils.i18n import force_language
import os.path
Expand Down Expand Up @@ -393,6 +394,30 @@ def test_copy_page(self):

self.assertEqual(Page.objects.drafts().count() - count, 3)

def test_copy_page_method(self):
"""
Test that a page can be copied via the admin
"""
page_a = create_page("page_a", "nav_playground.html", "en", published=False)
page_a_a = create_page("page_a_a", "nav_playground.html", "en",
parent=page_a, published=False, reverse_id="hello")
create_page("page_a_a_a", "nav_playground.html", "en", parent=page_a_a, published=False)
site = Site.objects.create(domain='whatever.com', name='whatever')

pages = Page.objects.drafts().filter(site_id=1, depth=1)
with transaction.atomic():
for page in pages:
page.copy_page(None, site)

with transaction.atomic():
for page in pages:
page.copy_page(None, site)

self.assertEqual(Page.objects.filter(site_id=1, depth=1).count(), 1)
self.assertEqual(Page.objects.filter(site_id=1).count(), 3)
self.assertEqual(Page.objects.filter(site_id=site.pk, depth=1).count(), 2)
self.assertEqual(Page.objects.filter(site_id=site.pk).count(), 6)

def test_copy_self_page(self):
"""
Test that a page can be copied via the admin
Expand Down

0 comments on commit f4d1974

Please sign in to comment.