X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/26c548c5fe8c7cc7227b5f38cd27e0278442dd76..2f7b3e87ff28392c872a0e828224d0b781297041:/contrib/shipherd/templatetags/shipherd.py diff --git a/contrib/shipherd/templatetags/shipherd.py b/contrib/shipherd/templatetags/shipherd.py index 019d240..fa4ec3e 100644 --- a/contrib/shipherd/templatetags/shipherd.py +++ b/contrib/shipherd/templatetags/shipherd.py @@ -4,25 +4,28 @@ from django.utils.safestring import mark_safe from philo.contrib.shipherd.models import Navigation from philo.models import Node from mptt.templatetags.mptt_tags import RecurseTreeNode, cache_tree_children +from django.utils.translation import ugettext as _ register = template.Library() class RecurseNavigationNode(RecurseTreeNode): - def __init__(self, template_nodes, instance_var): + def __init__(self, template_nodes, instance_var, key): self.template_nodes = template_nodes self.instance_var = instance_var + self.key = key - def _render_node(self, context, node, request): + def _render_node(self, context, item, request): bits = [] context.push() - for child in node.get_children(): - context['navigation'] = child + for child in item.get_children(): + context['item'] = child bits.append(self._render_node(context, child, request)) - context['navigation'] = node + context['item'] = item context['children'] = mark_safe(u''.join(bits)) - context['active'] = node.is_active(request) + context['active'] = item.is_active(request) + context['active_descendants'] = item.has_active_descendants(request) rendered = self.template_nodes.render(context) context.pop() return rendered @@ -35,33 +38,29 @@ class RecurseNavigationNode(RecurseTreeNode): instance = self.instance_var.resolve(context) - if isinstance(instance, Node): - qs = Navigation.objects.closest_navigation(instance) - elif hasattr(instance, '__iter__'): - # Is this the right way to check? - qs = instance - else: + try: + navigation = instance.navigation[self.key] + except: return settings.TEMPLATE_STRING_IF_INVALID - roots = cache_tree_children(qs) - bits = [self._render_node(context, node, request) for node in roots] + bits = [self._render_node(context, item, request) for item in navigation] return ''.join(bits) @register.tag def recursenavigation(parser, token): """ - Based on django-mptt's recursetree templatetag. In addition to {{ navigation }} and {{ children }}, - sets {{ active }} in the context. + Based on django-mptt's recursetree templatetag. In addition to {{ item }} and {{ children }}, + sets {{ active }} and {{ active_descendants }} in the context. Note that the tag takes one variable, which is a Node instance. Usage: