Moved NavigationManager cache handling into custom methods for better readability...
[philo.git] / contrib / navigation / templatetags / navigation.py
1 from django import template
2 from django.conf import settings
3 from philo.contrib.navigation.models import Navigation
4
5
6 register = template.Library()
7
8
9 @register.filter
10 def get_navigation(node):
11         roots = Navigation.objects.for_node(node)
12         qs = None
13         for root in roots:
14                 root_qs = root.get_descendants(include_self=True).complex_filter({'%s__lte' % root._mptt_meta.level_attr: root.get_level() + root.depth}).exclude(depth__isnull=True)
15                 if qs is None:
16                         qs = root_qs
17                 else:
18                         qs |= root_qs
19         return qs
20
21 @register.filter
22 def is_active(navigation, request):
23         """
24         Returns true if the navigation is considered `active`.
25         
26         But what does "active" mean? Should this be defined on the model instead, perhaps?
27         """
28         try:
29                 if navigation.target_node == request.node:
30                         if request.path == navigation.target_url:
31                                 return True
32                         return False
33                 if navigation.target_url in request.path:
34                         return True
35         except:
36                 pass
37         return False