From d503b0fb390b02d21f93d5eb00fa880c781ca432 Mon Sep 17 00:00:00 2001 From: Paulo Alvarado Date: Tue, 21 Nov 2017 18:43:30 -0500 Subject: [PATCH] Fixed #6151 -- Pages can be copied as the first child of a parent page (#6152) --- cms/models/pagemodel.py | 6 ++++ cms/test_utils/testcases.py | 2 +- cms/tests/test_page_admin.py | 55 +++++++++++++++++++++++++----------- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/cms/models/pagemodel.py b/cms/models/pagemodel.py index cfdb7de36d5..4413c582c3a 100644 --- a/cms/models/pagemodel.py +++ b/cms/models/pagemodel.py @@ -711,6 +711,12 @@ def copy_with_descendants(self, site, target_node=None, position=None, new_root_page = self.copy(target_site, parent_node=parent_node) new_root_node = new_root_page.get_node_object(target_site) + if target_node and position in ('first-child'): + # target node is a parent and user has requested to + # insert the new page as its first child + new_root_node.move(target_node, position) + new_root_node.refresh_from_db(fields=('path', 'depth')) + if target_node and position in ('left', 'last-child'): # target node is a sibling new_root_node.move(target_node, position) diff --git a/cms/test_utils/testcases.py b/cms/test_utils/testcases.py index bd7ccb5976b..4e198582d7e 100644 --- a/cms/test_utils/testcases.py +++ b/cms/test_utils/testcases.py @@ -357,7 +357,7 @@ def copy_page(self, page, target_page, position=0): Page.objects.all(), pk=response_data['id'], ) - self.assertObjectExist(page.title_set.filter(language=language), slug=new_page_slug) + self.assertObjectExist(copied_page.title_set.filter(language=language), slug=new_page_slug) page._clear_node_cache(page.site) target_page._clear_node_cache(target_page.site) return copied_page diff --git a/cms/tests/test_page_admin.py b/cms/tests/test_page_admin.py index 54ce9ee121c..fc7c4c33bbf 100644 --- a/cms/tests/test_page_admin.py +++ b/cms/tests/test_page_admin.py @@ -614,13 +614,18 @@ def test_copy_page_to_explicit_position(self): """ superuser = self.get_superuser() parent = create_page("parent", "nav_playground.html", "en", published=True) - child_0001 = create_page("child-0001", "nav_playground.html", "en", published=True, parent=parent) child_0002 = create_page("child-0002", "nav_playground.html", "en", published=True, parent=parent) - child_0004 = create_page("child-0004", "nav_playground.html", "en", published=True, parent=parent) - child_0003 = create_page("child-0003", "nav_playground.html", "en", published=True) + child_0003 = create_page("child-0003", "nav_playground.html", "en", published=True, parent=parent) + child_0005 = create_page("child-0005", "nav_playground.html", "en", published=True, parent=parent) + child_0004 = create_page("child-0004", "nav_playground.html", "en", published=True) with self.login_user_context(superuser): - child_0003 = self.copy_page(child_0003, parent, position=2) + # Copy the 0005 page and insert it as first child of parent + child_0001 = self.copy_page(child_0005, parent, position=0) + + with self.login_user_context(superuser): + # Copy the 0004 page and insert it as fourth child of parent + child_0004 = self.copy_page(child_0004, parent, position=3) tree = ( (parent, '0001'), @@ -628,6 +633,7 @@ def test_copy_page_to_explicit_position(self): (child_0002, '00010002'), (child_0003, '00010003'), (child_0004, '00010004'), + (child_0005, '00010005'), ) for page, path in tree: @@ -640,29 +646,44 @@ def test_copy_page_tree_to_explicit_position(self): """ superuser = self.get_superuser() parent = create_page("parent", "nav_playground.html", "en", published=True) - child_0001 = create_page("child-0001", "nav_playground.html", "en", published=True, parent=parent) child_0002 = create_page("child-0002", "nav_playground.html", "en", published=True, parent=parent) - child_0004 = create_page("child-0004", "nav_playground.html", "en", published=True, parent=parent) - child_0003 = create_page("child-0003", "nav_playground.html", "en", published=True) - create_page("child-00030001", "nav_playground.html", "en", published=True, parent=child_0003) - create_page("child-00030002", "nav_playground.html", "en", published=True, parent=child_0003) - create_page("child-00030003", "nav_playground.html", "en", published=True, parent=child_0003) + child_0003 = create_page("child-0003", "nav_playground.html", "en", published=True, parent=parent) + child_0005 = create_page("child-0005", "nav_playground.html", "en", published=True, parent=parent) + create_page("child-00050001", "nav_playground.html", "en", published=True, parent=child_0005) + create_page("child-00050002", "nav_playground.html", "en", published=True, parent=child_0005) + create_page("child-00050003", "nav_playground.html", "en", published=True, parent=child_0005) + child_0004 = create_page("child-0004", "nav_playground.html", "en", published=True) + create_page("child-00040001", "nav_playground.html", "en", published=True, parent=child_0004) + create_page("child-00040002", "nav_playground.html", "en", published=True, parent=child_0004) + create_page("child-00040003", "nav_playground.html", "en", published=True, parent=child_0004) + + with self.login_user_context(superuser): + # Copy the 0005 page and insert it as first child of parent + child_0001 = self.copy_page(child_0005, parent, position=0) + child_00010001 = child_0001.node.get_children()[0].page + child_00010002 = child_0001.node.get_children()[1].page + child_00010003 = child_0001.node.get_children()[2].page with self.login_user_context(superuser): - child_0003 = self.copy_page(child_0003, parent, position=2) - child_00030001 = child_0003.node.get_children()[0].page - child_00030002 = child_0003.node.get_children()[1].page - child_00030003 = child_0003.node.get_children()[2].page + # Copy the 0004 page and insert it as fourth child of parent + child_0004 = self.copy_page(child_0004, parent, position=3) + child_00040001 = child_0004.node.get_children()[0].page + child_00040002 = child_0004.node.get_children()[1].page + child_00040003 = child_0004.node.get_children()[2].page tree = ( (parent, '0001'), (child_0001, '00010001'), + (child_00010001, '000100010001'), + (child_00010002, '000100010002'), + (child_00010003, '000100010003'), (child_0002, '00010002'), (child_0003, '00010003'), - (child_00030001, '000100030001'), - (child_00030002, '000100030002'), - (child_00030003, '000100030003'), (child_0004, '00010004'), + (child_00040001, '000100040001'), + (child_00040002, '000100040002'), + (child_00040003, '000100040003'), + (child_0005, '00010005'), ) for page, path in tree: