X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/cac73c3fcf9abaaf497875fae5457793cc4cf0ee..ae602182bcbeb943d031fbe3c16f2fe464e8e862:/contrib/navigation/models.py?ds=inline diff --git a/contrib/navigation/models.py b/contrib/navigation/models.py index b37f614..e43e8d9 100644 --- a/contrib/navigation/models.py +++ b/contrib/navigation/models.py @@ -6,18 +6,35 @@ from django.forms.models import model_to_dict from philo.models import TreeEntity, JSONField, Node, TreeManager from philo.validators import RedirectValidator -#from mptt.templatetags.mptt_tags import cache_tree_children - DEFAULT_NAVIGATION_DEPTH = 3 +class NavigationQuerySet(models.query.QuerySet): + """ + This subclass is necessary to trigger cache clearing for Navigation when a mass update + or deletion is performed. For now, either action will trigger a clearing of the entire + navigation cache, since there's no convenient way to iterate over the changed or + deleted instances. + """ + def update(self, *args, **kwargs): + super(NavigationQuerySet, self).update(*args, **kwargs) + Navigation.objects.clear_cache() + + def delete(self, *args, **kwargs): + super(NavigationQuerySet, self).delete(*args, **kwargs) + Navigation.objects.clear_cache() + + class NavigationManager(TreeManager): # Analagous to contenttypes, cache Navigation to avoid repeated lookups all over the place. # Navigation will probably be used frequently. _cache = {} + def get_queryset(self): + return NavigationQuerySet(self.model, using=self._db) + def closest_navigation(self, node): """ Returns the set of Navigation objects for a given node's navigation. This @@ -91,10 +108,6 @@ class NavigationManager(TreeManager): """ Clear out the navigation cache. This needs to happen during database flushes or if a navigation entry is changed to prevent caching of outdated navigation information. - - TODO: call this method from update() and delete()! - But how? Those aren't methods available - from the manager. The only solution would be to make a special QuerySet subclass that calls - this method for each instance. """ if navigation is None: self.__class__._cache.clear() @@ -158,6 +171,33 @@ class Navigation(TreeEntity): return self.url_or_subpath target_url = property(get_target_url) + def is_active(self, request): + # First check if this particular navigation is active. It is considered active if: + # - the requested node is this instance's target node and its subpath matches the requested path. + # - the requested node is a descendant of this instance's target node and this instance's target + # node is not the hosting node of this navigation structure. + # - this instance has no target node and the url matches either the request path or the full url. + # - any of this instance's children are active. + node = request.node + + if self.target_node == node: + if self.target_url == request.path: + return True + elif self.target_node is None: + if self.url_or_subpath == request.path or self.url_or_subpath == "http%s://%s%s" % (request.is_secure() and 's' or '', request.get_host(), request.path): + return True + elif self.target_node.is_ancestor_of(node) and self.target_node != self.hosting_node: + return True + + # Always fall back to whether the node has active children. + return self.has_active_children(request) + + def has_active_children(self, request): + for child in self.get_children(): + if child.is_active(request): + return True + return False + def _has_changed(self): if model_to_dict(self) == self._initial_data: return False