From c7161f34d92a2df6ed37b2ed061e64a47dbfb003 Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Tue, 11 Jan 2011 15:26:39 -0500 Subject: [PATCH] Added shortcut is_cached method to Navigation instances and Navigation.objects; updated other methods to use these shortcuts. Added cache clearing for new and changed Navigation instances that have not been cached but are within the range of a hosted navigation instance. --- contrib/shipherd/models.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/contrib/shipherd/models.py b/contrib/shipherd/models.py index 72bd775..a95546d 100644 --- a/contrib/shipherd/models.py +++ b/contrib/shipherd/models.py @@ -56,13 +56,13 @@ class NavigationManager(TreeManager): nodes_to_cache = [] host_node = None for ancestor in ancestors: - if self._is_cached(self.db, ancestor) or ancestor.num_navigation > 0: + if self.is_cached(ancestor) or ancestor.num_navigation > 0: host_node = ancestor break else: nodes_to_cache.append(ancestor) - if not self._is_cached(self.db, host_node): + if not self.is_cached(host_node): self._add_to_cache(self.db, host_node) # Cache the queryset instance for every node that was passed over, as well. @@ -72,6 +72,9 @@ class NavigationManager(TreeManager): return hosted_navigation + def is_cached(self, node): + return self._is_cached(self.db, node) + def _add_to_cache(self, using, node, qs=None): key = getattr(node, 'pk', None) @@ -192,6 +195,10 @@ class Navigation(TreeEntity): # Always fall back to whether the node has active children. return self.has_active_children(request) + def is_cached(self): + """Shortcut method for Navigation.objects.is_cached""" + return Navigation.objects.is_cached(self) + def has_active_children(self, request): for child in self.get_children(): if child.is_active(request): @@ -205,9 +212,15 @@ class Navigation(TreeEntity): def save(self, *args, **kwargs): super(Navigation, self).save(*args, **kwargs) + if self._has_changed(): self._initial_data = model_to_dict(self) - Navigation.objects.clear_cache(self) + if self.is_cached(): + Navigation.objects.clear_cache(self) + else: + for navigation in self.get_ancestors(): + if navigation.hosting_node and navigation.is_cached() and self.get_level() <= (navigation.get_level() + navigation.depth): + Navigation.objects.clear_cache(navigation) def delete(self, *args, **kwargs): super(Navigation, self).delete(*args, **kwargs) -- 2.20.1