From 87443c44a6efa3010e31dbd50347efab8170d6f6 Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Thu, 23 Dec 2010 10:19:08 -0500 Subject: [PATCH] Tweaked admin and models. Added filters to manage navigation display. --- contrib/navigation/admin.py | 1 + contrib/navigation/models.py | 11 +++--- contrib/navigation/templatetags/__init__.py | 0 contrib/navigation/templatetags/navigation.py | 36 +++++++++++++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 contrib/navigation/templatetags/__init__.py create mode 100644 contrib/navigation/templatetags/navigation.py diff --git a/contrib/navigation/admin.py b/contrib/navigation/admin.py index 7a43d45..ec073c6 100644 --- a/contrib/navigation/admin.py +++ b/contrib/navigation/admin.py @@ -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',) diff --git a/contrib/navigation/models.py b/contrib/navigation/models.py index f7f0f56..47e5357 100644 --- a/contrib/navigation/models.py +++ b/contrib/navigation/models.py @@ -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 index 0000000..e69de29 diff --git a/contrib/navigation/templatetags/navigation.py b/contrib/navigation/templatetags/navigation.py new file mode 100644 index 0000000..9cd9df5 --- /dev/null +++ b/contrib/navigation/templatetags/navigation.py @@ -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 -- 2.20.1