Tweaked admin and models. Added filters to manage navigation display.
authorStephen Burrows <stephen.r.burrows@gmail.com>
Thu, 23 Dec 2010 15:19:08 +0000 (10:19 -0500)
committerStephen Burrows <stephen.r.burrows@gmail.com>
Thu, 23 Dec 2010 15:19:08 +0000 (10:19 -0500)
contrib/navigation/admin.py
contrib/navigation/models.py
contrib/navigation/templatetags/__init__.py [new file with mode: 0644]
contrib/navigation/templatetags/navigation.py [new file with mode: 0644]

index 7a43d45..ec073c6 100644 (file)
@@ -39,6 +39,7 @@ class NodeNavigationInline(NavigationInline):
 
 
 class NavigationAdmin(TreeEntityAdmin):
+       list_display = ('__unicode__', 'target_node', 'url_or_subpath', 'reversing_parameters')
        fieldsets = (
                (None, {
                        'fields': ('text', 'hosting_node',)
index f7f0f56..47e5357 100644 (file)
@@ -31,7 +31,7 @@ class NavigationManager(models.Manager):
                        hosted_navigation = self.__class__._cache[self.db][key]
                except KeyError:
                        # Find the most recent host!
-                       ancestors = node.get_ancestors(ascending=True, include_self=True).annotate(num_navigation=models.Count("hosted_navigation_set"))
+                       ancestors = node.get_ancestors(ascending=True, include_self=True).annotate(num_navigation=models.Count("hosted_navigation"))
                        
                        # Iterate down the ancestors until you find one that:
                        # a) is cached, or
@@ -49,7 +49,7 @@ class NavigationManager(models.Manager):
                                return self.none()
                        
                        if ancestor.pk not in self.__class__._cache[self.db]:
-                               self.__class__._cache[self.db][ancestor.pk] = host_node.hosted_navigation_set.select_related('target_node')
+                               self.__class__._cache[self.db][ancestor.pk] = host_node.hosted_navigation.select_related('target_node')
                        
                        hosted_navigation = self.__class__._cache[self.db][ancestor.pk]
                        
@@ -88,9 +88,9 @@ class NavigationManager(models.Manager):
 class Navigation(TreeEntity):
        text = models.CharField(max_length=50)
        
-       hosting_node = models.ForeignKey(Node, blank=True, null=True, related_name='hosted_navigation_set', help_text="Be part of this node's root navigation.")
+       hosting_node = models.ForeignKey(Node, blank=True, null=True, related_name='hosted_navigation', help_text="Be part of this node's root navigation.")
        
-       target_node = models.ForeignKey(Node, blank=True, null=True, related_name='targeting_navigation_set', help_text="Point to this node's url.")
+       target_node = models.ForeignKey(Node, blank=True, null=True, related_name='targeting_navigation', help_text="Point to this node's url.")
        url_or_subpath = models.CharField(max_length=200, validators=[RedirectValidator()], blank=True, help_text="Point to this url or, if a node is defined and accepts subpaths, this subpath of the node.")
        reversing_parameters = JSONField(blank=True, help_text="If reversing parameters are defined, url_or_subpath will instead be interpreted as the view name to be reversed.")
        
@@ -113,7 +113,7 @@ class Navigation(TreeEntity):
        def get_target_url(self):
                node = self.target_node
                if node is not None and node.accepts_subpath and self.url_or_subpath:
-                       if self.reversing_parameters:
+                       if self.reversing_parameters is not None:
                                view_name = self.url_or_subpath
                                params = self.reversing_parameters
                                args = isinstance(params, list) and params or None
@@ -128,6 +128,7 @@ class Navigation(TreeEntity):
                        return node.get_absolute_url()
                else:
                        return self.url_or_subpath
+       target_url = property(get_target_url)
        
        def __unicode__(self):
                return self.get_path(field='text', pathsep=u' › ')
diff --git a/contrib/navigation/templatetags/__init__.py b/contrib/navigation/templatetags/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/contrib/navigation/templatetags/navigation.py b/contrib/navigation/templatetags/navigation.py
new file mode 100644 (file)
index 0000000..9cd9df5
--- /dev/null
@@ -0,0 +1,36 @@
+from django import template
+from django.conf import settings
+from philo.contrib.navigation.models import Navigation
+
+
+register = template.Library()
+
+
+@register.filter
+def get_descendants(roots):
+       qs = None
+       for root in roots:
+               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)
+               if qs is None:
+                       qs = root_qs
+               else:
+                       qs |= root_qs
+       return qs
+
+@register.filter
+def is_active(navigation, request):
+       """
+       Returns true if the navigation is considered `active`.
+       
+       But what does "active" mean? Should this be defined on the model instead, perhaps?
+       """
+       try:
+               if navigation.target_node == request.node:
+                       if request.path == navigation.target_url:
+                               return True
+                       return False
+               if navigation.target_url in request.path:
+                       return True
+       except:
+               pass
+       return False
\ No newline at end of file