Permalink
Browse files

softroots working

  • Loading branch information...
1 parent 55e99a9 commit 65331d81c589dc1fa8d96271942100c8cae400f8 @digi604 digi604 committed Mar 16, 2010
@@ -1,7 +1,8 @@
-#Thu Jan 14 16:52:32 CET 2010
+#Tue Mar 16 10:48:24 CET 2010
eclipse.preferences.version=1
encoding//cms/docs/conf.py=utf-8
encoding//cms/tests/__init__.py=utf-8
+encoding//cms/tests/apphooks.py=utf-8
encoding//cms/tests/menu.py=utf-8
encoding//cms/tests/navextender.py=utf-8
encoding//cms/tests/page.py=utf-8
View
@@ -138,21 +138,27 @@ def modify(self, request, nodes, namespace, id, post_cut, breadcrumb):
class SoftRootCutter(Modifier):
def modify(self, request, nodes, namespace, id, post_cut, breadcrumb):
- return nodes
- if post_cut:
+ if post_cut or not settings.CMS_SOFTROOT:
return nodes
selected = None
+ root_nodes = []
for node in nodes:
if node.selected:
selected = node
- nodes = []
+ if not node.parent:
+ root_nodes.append(node)
+
if selected:
if selected.attr.get("soft_root", False):
- nodes = selected.children
+ nodes = selected.get_descendants()
+ selected.parent = None
+ nodes = [selected] + nodes
else:
- if selected.parent:
- self.find_ancestors(selected.parent, selected, nodes)
- self.find_children(selected, nodes)
+ nodes = self.find_ancestors(selected, nodes)
+ nodes = self.find_children(selected, nodes)
+ else:
+ for node in root_nodes:
+ self.find_children(node, nodes)
return nodes
def find_children(self, node, nodes):
@@ -166,28 +172,26 @@ def remove_children(self, node, nodes):
nodes.remove(n)
self.remove_children(n, nodes)
node.children = []
- return nodes
- def find_ancestors(self, node, child, nodes):
+ def find_ancestors(self, node, nodes):
is_root = False
- if node.parent and node.parent.attr.get("soft_root", False):
- is_root = True
- nodes.remove(node)
- nodes = self.remove_ancestors(node.parent, node, nodes)
+ if node.parent:
+ if node.parent.attr.get("soft_root", False):
+ is_root = True
+ nodes = node.parent.get_descendants()
+ node.parent.parent = None
+ nodes = [node.parent] + nodes
+ else:
+ nodes = self.find_ancestors(node.parent, nodes)
+ else:
+ for n in nodes:
+ if n != node and not n.parent:
+ self.find_children(n, nodes)
for n in node.children:
- if n != child:
+ if n != node:
self.find_children(n, nodes)
if is_root:
n.parent = None
return nodes
- def remove_ancestors(self, node, child, nodes):
- if node.parent:
- self.remove_ancestors(node.parent, node, nodes)
- nodes.remove(node)
- for n in node.children:
- if n != child:
- nodes = self.remove_children(n, nodes)
- return nodes
-
menu_pool.register_modifier(SoftRootCutter)
@@ -13,6 +13,7 @@
from cms.tests.plugins import PluginsTestCase
from cms.tests.reversion_tests import ReversionTestCase
from cms.tests.menu import MenusTestCase
+from cms.tests.apphooks import ApphookTestCase
settings.CMS_PERMISSION = True
settings.CMS_MODERATOR = True
@@ -41,6 +42,7 @@ def suite():
s.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ReversionTestCase))
s.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(PermissionModeratorTestCase))
s.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(MenusTestCase))
+ s.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ApphookTestCase))
return s
@@ -10,6 +10,7 @@
from menus.base import NavigationNode
+
class ApphookTestCase(CMSTestCase):
def setUp(self):
View
@@ -210,11 +210,36 @@ def test_15_empty_menu(self):
nodes = show_menu(context, 0, 100, 100, 100)['children']
def test_16_softroot(self):
- self.assertEqual(1, 2)
page2 = Page.objects.get(pk=self.page2.pk)
page2.soft_root = True
page2.save()
context = self.get_context(path=page2.get_absolute_url())
nodes = show_menu(context, 0, 100, 100, 100)['children']
self.assertEqual(len(nodes), 1)
+ self.assertEqual(nodes[0].get_absolute_url(), page2.get_absolute_url())
+ page3 = Page.objects.get(pk=self.page3.pk)
+ context = self.get_context(path=page3.get_absolute_url())
+ nodes = show_menu(context, 0, 100, 100, 100)['children']
+ self.assertEqual(len(nodes), 1)
+ self.assertEqual(nodes[0].get_absolute_url(), page2.get_absolute_url())
+
+ page1 = Page.objects.get(pk=self.page1.pk)
+ context = self.get_context(path=page1.get_absolute_url())
+ nodes = show_menu(context, 0, 100, 100, 100)['children']
+ self.assertEqual(len(nodes), 2)
+ self.assertEqual(nodes[0].get_absolute_url(), page1.get_absolute_url())
+ self.assertEqual(len(nodes[0].children[0].children), 0)
+
+ context = self.get_context(path="/no/real/path/")
+ nodes = show_menu(context, 0, 100, 100, 100)['children']
+ self.assertEqual(len(nodes), 2)
+ self.assertEqual(nodes[0].get_absolute_url(), page1.get_absolute_url())
+ self.assertEqual(len(nodes[0].children[0].children), 0)
+
+ page5 = Page.objects.get(pk=self.page5.pk)
+ context = self.get_context(path=page5.get_absolute_url())
+ nodes = show_menu(context, 0, 100, 100, 100)['children']
+ self.assertEqual(len(nodes), 2)
+ self.assertEqual(nodes[0].get_absolute_url(), page1.get_absolute_url())
+ self.assertEqual(len(nodes[0].children[0].children), 0)
@@ -58,7 +58,7 @@ def test_03_extenders_on_root_child(self):
page4.navigation_extenders = "TestMenu"
page4.save()
context = self.get_context()
- nodes = show_menu(context)['children']
+ nodes = show_menu(context, 0, 100, 100, 100)['children']
self.assertEqual(len(nodes), 2)
self.assertEqual(len(nodes[1].children), 4)
View
@@ -50,4 +50,11 @@ def get_absolute_url(self):
def get_attribute(self, name):
return self.attr[name]
+
+ def get_descendants(self):
+ nodes = []
+ for node in self.children:
+ nodes.append(node)
+ nodes += node.get_descendants()
+ return nodes
View
@@ -101,14 +101,22 @@ def get_nodes(self, request, namespace=None, root_id=None, site_id=None, breadcr
return nodes
def _mark_selected(self, request, nodes):
+ sel = None
for node in nodes:
node.sibling = False
node.ancestor = False
node.descendant = False
- if node.get_absolute_url() == request.path:
- node.selected = True
+ node.selected = False
+ if node.get_absolute_url() == request.path[:len(node.get_absolute_url())]:
+ if sel:
+ if len(node.get_absolute_url()) > len(sel.get_absolute_url()):
+ sel = node
+ else:
+ sel = node
else:
node.selected = False
+ if sel:
+ sel.selected = True
return nodes
def get_menus_by_attribute(self, name, value):
@@ -78,7 +78,6 @@ def show_menu(context, from_level=0, to_level=100, extra_inactive=0, extra_activ
else:
new_nodes = []
nodes = new_nodes
-
children = cut_levels(nodes, from_level, to_level, extra_inactive, extra_active)
children = menu_pool.apply_modifiers(children, request, namespace, root_id, post_cut=True)

0 comments on commit 65331d8

Please sign in to comment.