Permalink
Browse files

testsuite working again

  • Loading branch information...
1 parent 231b8ea commit 38225bbc491b72c9fe52f2b99776d14a5e69105e @digi604 digi604 committed Feb 16, 2010
Showing with 182 additions and 73 deletions.
  1. +1 −1 cms/admin/forms.py
  2. +86 −23 cms/menu.py
  3. +1 −0 cms/menu_bases.py
  4. +42 −4 cms/tests/apphooks.py
  5. +8 −1 cms/tests/menu.py
  6. +2 −0 example/manage.py
  7. +1 −12 menus/base.py
  8. +14 −7 menus/menu_pool.py
  9. +10 −8 menus/modifiers.py
  10. +17 −17 menus/templatetags/menu_tags.py
View
@@ -107,7 +107,7 @@ class PageForm(PageAddForm):
def __init__(self, *args, **kwargs):
super(PageForm, self).__init__(*args, **kwargs)
# set the nav extenders for the form
- self.fields['navigation_extenders'].choices = [('', "---------")] + menu_pool.get_cms_enabled_menus()
+ self.fields['navigation_extenders'].choices = [('', "---------")] + menu_pool.get_menus_by_attribute("cms_enabled", True)
def clean(self):
cleaned_data = super(PageForm, self).clean()
View
@@ -7,6 +7,22 @@
from cms.utils.i18n import get_fallback_languages
from cms.exceptions import NoHomeFound
+def page_to_node(page, home, cut):
+ parent_id = page.parent_id
+ if home and page.parent_id == home.pk and cut:
+ parent_id = None
+ attr = {'navigation_extenders':page.navigation_extenders,
+ 'soft_root':page.soft_root,
+ 'auth_required':page.login_required,
+ 'reverse_id':page.reverse_id,}
+ n = NavigationNode(page.get_menu_title(),
+ page.get_absolute_url(),
+ page.pk,
+ parent_id,
+ attr=attr,
+ )
+ return n
+
class CMSMenu(Menu):
def get_nodes(self, request):
@@ -48,7 +64,7 @@ def get_nodes(self, request):
if not hasattr(page, "title_cache"):
page.title_cache = {}
page.title_cache[title.language] = title
- nodes.append(self.page_to_node(page, home, home_cut))
+ nodes.append(page_to_node(page, home, home_cut))
ids.remove(page.pk)
if ids: # get fallback languages
fallbacks = get_fallback_languages(lang)
@@ -60,31 +76,14 @@ def get_nodes(self, request):
if not hasattr(page, "title_cache"):
page.title_cache = {}
page.title_cache[title.language] = title
- nodes.append(self.page_to_node(page, home, home_cut))
+ nodes.append(page_to_node(page, home, home_cut))
ids.remove(page.pk)
break
if not ids:
break
- return nodes
-
- def page_to_node(self, page, home, cut):
- parent_id = page.parent_id
- if home and page.parent_id == home.pk and cut:
- parent_id = None
- attr = {'navigation_extenders':page.navigation_extenders}
- n = NavigationNode(page.get_menu_title(),
- page.get_absolute_url(),
- page.pk,
- parent_id,
- attr=attr,
- softroot=page.soft_root,
- auth_required=page.login_required,
- reverse_id=page.reverse_id)
- return n
-
+ return nodes
menu_pool.register_menu(CMSMenu)
-
class NavExtender(Modifier):
def modify(self, request, nodes, namespace, id, post_cut, breadcrumb):
if post_cut:
@@ -105,11 +104,11 @@ def modify(self, request, nodes, namespace, id, post_cut, breadcrumb):
removed = []
# find all not assigned nodes
for menu in menu_pool.menus.items():
- if menu[1].cms_enabled and not menu[0] in exts:
+ if hasattr(menu[1], 'cms_enabled') and menu[1].cms_enabled and not menu[0] in exts:
for node in nodes:
if node.namespace == menu[0]:
removed.append(node)
- if removed:
+ if removed or breadcrumb:
# has home a nav extender and is home not in navigation?
page_queryset = get_page_queryset(request)
try:
@@ -122,9 +121,73 @@ def modify(self, request, nodes, namespace, id, post_cut, breadcrumb):
for node in n_removed:
if node.namespace != home.navigation_extenders:
removed.append(node)
+ # if breadcrumb and home not in navigation add node
+ if breadcrumb and home and not home.in_navigation:
+ home = page_to_node(home, home, False)
+ nodes.append(home)
+ if request.path == home.get_absolute_url():
+ home.selected = True
+ else:
+ home.selected = False
# remove all nodes that are nav_extenders and not assigned
for node in removed:
nodes.remove(node)
+ return nodes
+menu_pool.register_modifier(NavExtender)
+
+
+class SoftRootCutter(Modifier):
+ def modify(self, request, nodes, namespace, id, post_cut, breadcrumb):
+ return nodes
+ if post_cut:
+ return nodes
+ selected = None
+ for node in nodes:
+ if node.selected:
+ selected = node
+ nodes = []
+ if selected:
+ if selected.attr.get("soft_root", False):
+ nodes = selected.children
+ else:
+ if selected.parent:
+ self.find_ancestors(selected.parent, selected, nodes)
+ self.find_children(selected, nodes)
+ return nodes
+
+ def find_children(self, node, nodes):
+ for n in node.children:
+ if n.attr.get("soft_root", False):
+ self.remove_children(n, nodes)
+ return nodes
+
+ def remove_children(self, node, nodes):
+ for n in node.children:
+ nodes.remove(n)
+ self.remove_children(n, nodes)
+ node.children = []
+ return nodes
+
+ def find_ancestors(self, node, child, 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)
+ for n in node.children:
+ if n != child:
+ 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(NavExtender)
+menu_pool.register_modifier(SoftRootCutter)
View
@@ -3,6 +3,7 @@
class CMSAttachMenu(Menu):
cms_enabled = True
+ name = None
def __init__(self, *args, **kwargs):
super(CMSAttachMenu, self).__init__(*args, **kwargs)
View
@@ -1,5 +1,43 @@
-'''
-Created on Jan 22, 2010
+# -*- coding: utf-8 -*-
+from django.conf import settings
+from django.contrib.auth.models import User
+from cms.tests.base import CMSTestCase
+from cms.models import Page
+from cms.menu import CMSMenu
+from menus.templatetags.menu_tags import show_menu, show_sub_menu,\
+ show_breadcrumb, language_chooser, page_language_url, show_menu_below_id
+from menus.menu_pool import menu_pool
+from menus.base import NavigationNode
-@author: patricklauber
-'''
+
+class ApphookTestCase(CMSTestCase):
+
+ def setUp(self):
+ settings.CMS_MODERATOR = False
+ u = User(username="test", is_staff = True, is_active = True, is_superuser = True)
+ u.set_password("test")
+ u.save()
+ self.login_user(u)
+ if not menu_pool.discovered:
+ menu_pool.discover_menus()
+ self.old_menu = menu_pool.menus
+ menu_pool.menus = {'CMSMenu':self.old_menu['CMSMenu']}
+ menu_pool.clear()
+ self.create_some_nodes()
+
+ def tearDown(self):
+ menu_pool.menus = self.old_menu
+
+ def create_some_nodes(self):
+ self.page1 = self.create_page(parent_page=None, published=True, in_navigation=True)
+ self.page2 = self.create_page(parent_page=self.page1, published=True, in_navigation=True)
+ self.page3 = self.create_page(parent_page=self.page2, published=True, in_navigation=True)
+ self.page4 = self.create_page(parent_page=None, published=True, in_navigation=True)
+ self.page5 = self.create_page(parent_page=self.page4, published=True, in_navigation=True)
+
+ def test_01_basic_apphooks(self):
+ self.assertEqual(2, 1)
+
+ def test_02_two_apphooks_in_different_languages(self):
+ self.assertEqual(1, 2)
+
View
@@ -7,6 +7,7 @@
from menus.templatetags.menu_tags import show_menu, show_sub_menu,\
show_breadcrumb, language_chooser, page_language_url, show_menu_below_id
from menus.menu_pool import menu_pool
+from menus.base import NavigationNode
class MenusTestCase(CMSTestCase):
@@ -145,8 +146,8 @@ def test_10_show_breadcrumb(self):
context = self.get_context(path=self.page2.get_absolute_url())
nodes = show_breadcrumb(context)['ancestors']
self.assertEqual(len(nodes), 2)
- print nodes
self.assertEqual(nodes[0].get_absolute_url(), "/")
+ self.assertEqual(isinstance(nodes[0], NavigationNode), True)
self.assertEqual(nodes[1].get_absolute_url(), page2.get_absolute_url())
def test_11_language_chooser(self):
@@ -210,4 +211,10 @@ def test_15_empty_menu(self):
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)
View
@@ -11,3 +11,5 @@
if __name__ == "__main__":
execute_manager(settings)
+
+
View
@@ -22,31 +22,20 @@ def modify(self, request, nodes, namespace, id, post_cut):
class NavigationNode(object):
title = None
url = None
- auth_required = False
- required_group_id = None
attr = {}
namespace = None
id = None
- softroot = False
parent_id = None
parent_namespace = None
parent = None # do not touch
- reverse_id = None
- longer_url_match = False
-
- selected = False
- def __init__(self, title, url, id, parent_id=None, parent_namespace=None, attr=None, softroot=False, auth_required=False, required_group_id=None, reverse_id=None):
+ def __init__(self, title, url, id, parent_id=None, parent_namespace=None, attr=None):
self.children = [] # do not touch
self.title = title
self.url = url
self.id = id
- self.softroot = softroot
self.parent_id = parent_id
self.parent_namespace = parent_namespace
- self.auth_required = auth_required
- self.required_group_id = required_group_id
- self.reverse_id = reverse_id
if attr:
self.attr = attr
View
@@ -49,7 +49,7 @@ def _build_nodes(self, request, site_id):
try:
nodes = self.menus[ns].get_nodes(request)
except:
- pass
+ raise
last = None
for node in nodes:
if not node.namespace:
@@ -97,7 +97,7 @@ def get_nodes(self, request, namespace=None, root_id=None, site_id=None, breadcr
site_id = Site.objects.get_current().pk
nodes = self._build_nodes(request, site_id)
nodes = copy.deepcopy(nodes)
- nodes = self.apply_modifiers(nodes, request, namespace, root_id, post_cut=False, breadcrumb)
+ nodes = self.apply_modifiers(nodes, request, namespace, root_id, post_cut=False, breadcrumb=breadcrumb)
return nodes
def _mark_selected(self, request, nodes):
@@ -111,11 +111,18 @@ def _mark_selected(self, request, nodes):
node.selected = False
return nodes
- def get_cms_enabled_menus(self):
- enabled = []
+ def get_menus_by_attribute(self, name, value):
+ found = []
for menu in self.menus.items():
- if menu[1].cms_enabled:
- enabled.append((menu[0], menu[1].name))
- return enabled
+ if hasattr(menu[1], name) and getattr(menu[1], name, None) == value:
+ found.append((menu[0], menu[1].name))
+ return found
+
+ def get_nodes_by_attribute(self, nodes, name, value):
+ found = []
+ for node in nodes:
+ if node.attr.get(name, None) == value:
+ found.append(node)
+ return found
menu_pool = MenuPool()
View
@@ -9,8 +9,8 @@ class Marker(Modifier):
descendants: descendant = True
ancestors: ancestor = True
"""
- def modify(self, request, nodes, namespace, root_id, post_cut):
- if post_cut:
+ def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
+ if post_cut or breadcrumb:
return nodes
selected = None
root_nodes = []
@@ -58,7 +58,9 @@ class Level(Modifier):
"""
post_cut = True
- def modify(self, request, nodes, namespace, root_id, post_cut):
+ def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
+ if breadcrumb:
+ return nodes
for node in nodes:
if not node.parent:
@@ -85,23 +87,23 @@ class LoginRequired(Modifier):
"""
Remove nodes that are login required or require a group
"""
- def modify(self, request, nodes, namespace, root_id, post_cut):
- if post_cut:
+ def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
+ if post_cut or breadcrumb:
return nodes
final = []
for node in nodes:
good = False
- if node.auth_required and request.user.is_authenticated():
+ if node.attr.get('auth_required', False) and request.user.is_authenticated():
good = True
- if node.required_group_id and request.user.is_authenticated():
+ if node.attr.get('required_group_id', False) and request.user.is_authenticated():
if not hasattr(request.user, "group_cache"):
request.user.group_cache = request.user.groups.all()
good = False
for group in request.user.group_cache:
if group.pk == node.required_group_id:
good = True
break
- if good or (not node.auth_required and not node.required_group_id):
+ if good or (not node.attr.get('auth_required', False) and not node.attr.get('required_group_id', False)):
final.append(node)
return final
Oops, something went wrong.

0 comments on commit 38225bb

Please sign in to comment.