X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/db91ce955e7408382263b8f4ec82915f77d8efb4..64e4cdefe89f642d349b5908a3bbaec76333e3b2:/templatetags/nodes.py diff --git a/templatetags/nodes.py b/templatetags/nodes.py index 8a98630..c2dcd9a 100644 --- a/templatetags/nodes.py +++ b/templatetags/nodes.py @@ -25,7 +25,7 @@ class NodeURLNode(template.Node): if self.node: node = self.node.resolve(context) else: - node = context['node'] + node = context.get('node', None) if not node: return settings.TEMPLATE_STRING_IF_INVALID @@ -51,7 +51,9 @@ class NodeURLNode(template.Node): subpath = reverse(view_name, urlconf=node.view, args=args, kwargs=kwargs) except NoReverseMatch: if self.as_var is None: - raise + if settings.TEMPLATE_DEBUG: + raise + return settings.TEMPLATE_STRING_IF_INVALID else: if subpath[0] == '/': subpath = subpath[1:] @@ -68,7 +70,7 @@ class NodeURLNode(template.Node): @register.tag(name='node_url') def do_node_url(parser, token): """ - {% node_url [for ] [as ] [as ] %} {% node_url with [for ] [as ] %} {% node_url [ [ ...] ] [for ] [as ] %} {% node_url [= [= ...] ] [for ] [as ]%} @@ -112,4 +114,84 @@ def do_node_url(parser, token): args.append(parser.compile_filter(value)) return NodeURLNode(view_name=view_name, args=args, kwargs=kwargs, node=node, as_var=as_var) - return NodeURLNode(node=node, as_var=as_var) \ No newline at end of file + return NodeURLNode(node=node, as_var=as_var) + + +class NavigationNode(template.Node): + def __init__(self, node=None, as_var=None): + self.as_var = as_var + self.node = node + + def render(self, context): + if 'request' not in context: + return settings.TEMPLATE_STRING_IF_INVALID + + if self.node: + node = self.node.resolve(context) + else: + node = context.get('node', None) + + if not node: + return settings.TEMPLATE_STRING_IF_INVALID + + try: + nav_root = node.attributes['navigation_root'] + except KeyError: + if settings.TEMPLATE_DEBUG: + raise + return settings.TEMPLATE_STRING_IF_INVALID + + # Should I get its override and check for a max depth override there? + navigation = nav_root.get_navigation() + + if self.as_var: + context[self.as_var] = navigation + return '' + + return self.compile(navigation, context['request'].path, nav_root.get_absolute_url(), nav_root.get_level(), nav_root.get_level() + 3) + + def compile(self, navigation, active_path, root_url, current_depth, max_depth): + compiled = "" + for item in navigation: + if item['url'] in active_path and (item['url'] != root_url or root_url == active_path): + compiled += "
  • " + else: + compiled += "
  • " + + if item['url']: + compiled += "" % item['url'] + + compiled += item['title'] + + if item['url']: + compiled += "" + + if 'children' in item and current_depth < max_depth: + compiled += "
      %s
    " % self.compile(item['children'], active_path, root_url, current_depth + 1, max_depth) + + compiled += "
  • " + return compiled + + +@register.tag(name='navigation') +def do_navigation(parser, token): + """ + {% navigation [for ] [as ] %} + """ + bits = token.split_contents() + tag = bits[0] + bits = bits[1:] + node = None + as_var = None + + if len(bits) >= 2 and bits[-2] == 'as': + as_var = bits[-1] + bits = bits[:-2] + + if len(bits) >= 2 and bits[-2] == 'for': + node = parser.compile_filter(bits[-1]) + bits = bits[-2] + + if bits: + raise template.TemplateSyntaxError('`%s` template tag expects the syntax {%% %s [for ] [as ] %}' % (tag, tag)) + return NavigationNode(node, as_var)