Permalink
Browse files

When using the menu tag wrong (eg give a page as 'namespace') the men…

…u tag should fail fast (or ignore the value). This fix prevents some infinite loops in some cases.
  • Loading branch information...
1 parent bb9dee6 commit 33a759d45f9da49c44f6b7ecfc68d4bd940e23e9 Jonas Obrist committed Aug 10, 2011
Showing with 29 additions and 4 deletions.
  1. +9 −1 cms/tests/menu.py
  2. +20 −3 menus/templatetags/menu_tags.py
View
@@ -13,7 +13,7 @@
from django.contrib.auth.models import AnonymousUser, User, Permission, Group
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
-from django.template import Template
+from django.template import Template, TemplateSyntaxError
from menus.base import NavigationNode
from menus.menu_pool import menu_pool, _build_nodes_inner_for_one_menu
from menus.utils import mark_descendants, find_selected, cut_levels
@@ -71,6 +71,14 @@ def get_level(self, num):
def get_all_pages(self):
return Page.objects.all()
+ def test_menu_failfast_on_invalid_usage(self):
+ context = self.get_context()
+ context['child'] = self.get_page(1)
+ # test standard show_menu
+ with SettingsOverride(DEBUG=True, TEMPLATE_DEBUG=True):
+ tpl = Template("{% load menu_tags %}{% show_menu 0 0 0 0 'menu/menu.html' child %}")
+ self.assertRaises(TemplateSyntaxError, tpl.render, context)
+
def test_basic_cms_menu(self):
self.assertEqual(len(menu_pool.menus), 1)
response = self.client.get(self.get_pages_root()) # path = '/'
@@ -2,6 +2,7 @@
from classytags.arguments import IntegerArgument, Argument
from classytags.core import Options
from classytags.helpers import InclusionTag
+from classytags.values import StringValue
from django import template
from django.conf import settings
from django.contrib.sites.models import Site
@@ -17,6 +18,22 @@
class NOT_PROVIDED: pass
+class StrictStringValue(StringValue):
+ errors = {
+ "clean": "%(value)s is not a string",
+ }
+ value_on_error = ""
+
+ def clean(self, value):
+ if not isinstance(value, basestring):
+ return self.error(value, 'type')
+ return value
+
+
+class StrictStringArgument(Argument):
+ value_class = StrictStringValue
+
+
def cut_after(node, levels, removed):
"""
given a tree of nodes cuts after N levels
@@ -104,9 +121,9 @@ class ShowMenu(InclusionTag):
IntegerArgument('to_level', default=100, required=False),
IntegerArgument('extra_inactive', default=0, required=False),
IntegerArgument('extra_active', default=1000, required=False),
- Argument('template', default='menu/menu.html', required=False),
- Argument('namespace', default=None, required=False),
- Argument('root_id', default=None, required=False),
+ StrictStringArgument('template', default='menu/menu.html', required=False),
+ StrictStringArgument('namespace', default=None, required=False),
+ StrictStringArgument('root_id', default=None, required=False),
Argument('next_page', default=None, required=False),
)

0 comments on commit 33a759d

Please sign in to comment.