Split shipherd NodeNavigationInline into inlines for hosted/targeting inlines. Added...
[philo.git] / contrib / shipherd / templatetags / shipherd.py
index fec700b..019d240 100644 (file)
@@ -2,6 +2,7 @@ from django import template
 from django.conf import settings
 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
 
 
@@ -17,9 +18,9 @@ class RecurseNavigationNode(RecurseTreeNode):
                bits = []
                context.push()
                for child in node.get_children():
-                       context['node'] = child
+                       context['navigation'] = child
                        bits.append(self._render_node(context, child, request))
-               context['node'] = node
+               context['navigation'] = node
                context['children'] = mark_safe(u''.join(bits))
                context['active'] = node.is_active(request)
                rendered = self.template_nodes.render(context)
@@ -33,7 +34,16 @@ class RecurseNavigationNode(RecurseTreeNode):
                        return ''
                
                instance = self.instance_var.resolve(context)
-               roots = cache_tree_children(Navigation.objects.closest_navigation(instance))
+               
+               if isinstance(instance, Node):
+                       qs = Navigation.objects.closest_navigation(instance)
+               elif hasattr(instance, '__iter__'):
+                       # Is this the right way to check?
+                       qs = instance
+               else:
+                       return settings.TEMPLATE_STRING_IF_INVALID
+               
+               roots = cache_tree_children(qs)
                bits = [self._render_node(context, node, request) for node in roots]
                return ''.join(bits)
 
@@ -41,17 +51,17 @@ class RecurseNavigationNode(RecurseTreeNode):
 @register.tag
 def recursenavigation(parser, token):
        """
-       Based on django-mptt's recursetree templatetag. In addition to {{ node }} and {{ children }},
+       Based on django-mptt's recursetree templatetag. In addition to {{ navigation }} and {{ children }},
        sets {{ active }} in the context.
        
-       Note that the tag takes one variable, navigation, which is a Navigation instance.
+       Note that the tag takes one variable, which is a Node instance.
        
        Usage:
                <ul>
-                       {% recursenavigation navigation %}
+                       {% recursenavigation node %}
                                <li{% if active %} class='active'{% endif %}>
-                                       {{ node.name }}
-                                       {% if not node.is_leaf_node %}
+                                       {{ navigation.text }}
+                                       {% if not navigation.is_leaf_node %}
                                                <ul>
                                                        {{ children }}
                                                </ul>
@@ -64,9 +74,27 @@ def recursenavigation(parser, token):
        if len(bits) != 2:
                raise template.TemplateSyntaxError(_('%s tag requires an instance') % bits[0])
        
-       instance_var = template.Variable(bits[1])
+       instance_var = parser.compile_filter(bits[1])
        
        template_nodes = parser.parse(('endrecursenavigation',))
        parser.delete_first_token()
        
-       return RecurseNavigationNode(template_nodes, instance_var)
\ No newline at end of file
+       return RecurseNavigationNode(template_nodes, instance_var)
+
+
+@register.filter
+def has_navigation(node):
+       return bool(Navigation.objects.closest_navigation(node).count())
+
+
+@register.filter
+def navigation_host(node):
+       try:
+               return Navigation.objects.closest_navigation(node)[0].hosting_node
+       except:
+               return node
+
+
+@register.filter
+def targeting_navigation(node):
+       return Navigation.objects.closest_navigation(node).filter(target_node=node).order_by('level', 'lft')
\ No newline at end of file