Switched nav caching to cache the entire set of nodes for the navigation, not just...
[philo.git] / contrib / navigation / models.py
index f7f0f56..b37f614 100644 (file)
@@ -2,7 +2,8 @@
 from django.core.exceptions import ValidationError
 from django.core.urlresolvers import NoReverseMatch
 from django.db import models
 from django.core.exceptions import ValidationError
 from django.core.urlresolvers import NoReverseMatch
 from django.db import models
-from philo.models import TreeEntity, JSONField, Node
+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
 from philo.validators import RedirectValidator
 
 #from mptt.templatetags.mptt_tags import cache_tree_children
@@ -11,13 +12,13 @@ from philo.validators import RedirectValidator
 DEFAULT_NAVIGATION_DEPTH = 3
 
 
 DEFAULT_NAVIGATION_DEPTH = 3
 
 
-class NavigationManager(models.Manager):
+class NavigationManager(TreeManager):
        
        # Analagous to contenttypes, cache Navigation to avoid repeated lookups all over the place.
        # Navigation will probably be used frequently.
        _cache = {}
        
        
        # Analagous to contenttypes, cache Navigation to avoid repeated lookups all over the place.
        # Navigation will probably be used frequently.
        _cache = {}
        
-       def for_node(self, node):
+       def closest_navigation(self, node):
                """
                Returns the set of Navigation objects for a given node's navigation. This
                will be the most recent set of defined hosted navigation among the node's
                """
                Returns the set of Navigation objects for a given node's navigation. This
                will be the most recent set of defined hosted navigation among the node's
@@ -26,77 +27,104 @@ class NavigationManager(models.Manager):
                
                TODO: Should this create the auto-generated navigation in "physical" form?
                """
                
                TODO: Should this create the auto-generated navigation in "physical" form?
                """
-               key = node.pk
                try:
                try:
-                       hosted_navigation = self.__class__._cache[self.db][key]
+                       return self._get_from_cache(self.db, node)
                except KeyError:
                        # Find the most recent host!
                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
                        # b) has hosted navigation.
                        
                        # Iterate down the ancestors until you find one that:
                        # a) is cached, or
                        # b) has hosted navigation.
-                       pks_to_cache = []
+                       nodes_to_cache = []
                        host_node = None
                        for ancestor in ancestors:
                        host_node = None
                        for ancestor in ancestors:
-                               if ancestor.pk in self.__class__._cache[self.db] or ancestor.num_navigation > 0:
+                               if self._is_cached(self.db, ancestor) or ancestor.num_navigation > 0:
                                        host_node = ancestor
                                        break
                                else:
                                        host_node = ancestor
                                        break
                                else:
-                                       pks_to_cache.append(ancestor.pk)
+                                       nodes_to_cache.append(ancestor)
                        
                        
-                       if host_node is None:
-                               return self.none()
+                       if not self._is_cached(self.db, host_node):
+                               self._add_to_cache(self.db, host_node)
                        
                        
-                       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')
-                       
-                       hosted_navigation = self.__class__._cache[self.db][ancestor.pk]
-                       
-                       # Cache the queryset instance for every pk that was passed over, as well.
-                       for pk in pks_to_cache:
-                               self.__class__._cache[self.db][pk] = hosted_navigation
+                       # Cache the queryset instance for every node that was passed over, as well.
+                       hosted_navigation = self._get_from_cache(self.db, host_node)
+                       for node in nodes_to_cache:
+                               self._add_to_cache(self.db, node, hosted_navigation)
                
                return hosted_navigation
        
                
                return hosted_navigation
        
+       def _add_to_cache(self, using, node, qs=None):
+               key = getattr(node, 'pk', None)
+               
+               if qs is None:
+                       if key is None:
+                               roots = self.none()
+                       else:
+                               roots = node.hosted_navigation.select_related('target_node')
+                       
+                       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
+               
+                       if qs is None:
+                               qs = self.none()
+               
+               self.__class__._cache.setdefault(using, {})[key] = qs
+       
+       def _get_from_cache(self, using, node):
+               key = getattr(node, 'pk', None)
+               return self.__class__._cache[self.db][key]
+       
+       def _is_cached(self, using, node):
+               try:
+                       self._get_from_cache(using, node)
+               except KeyError:
+                       return False
+               return True
+       
        def clear_cache(self, navigation=None):
                """
                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.
                
        def clear_cache(self, navigation=None):
                """
                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()!
+               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()
                else:
                        cache = self.__class__._cache[self.db]
                """
                if navigation is None:
                        self.__class__._cache.clear()
                else:
                        cache = self.__class__._cache[self.db]
-                       for pk in cache.keys():
-                               for qs in cache[pk]:
-                                       if navigation in qs:
-                                               cache.pop(pk)
-                                               break
-                                       else:
-                                               for instance in qs:
-                                                       if navigation.is_descendant(instance):
-                                                               cache.pop(pk)
-                                                               break
-                                               # necessary?
-                                               if pk not in cache:
-                                                       break
+                       for pk, qs in cache.items():
+                               if navigation in qs:
+                                       cache.pop(pk)
 
 
 class Navigation(TreeEntity):
 
 
 class Navigation(TreeEntity):
+       objects = NavigationManager()
        text = models.CharField(max_length=50)
        
        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.")
        
        order = models.PositiveSmallIntegerField(blank=True, null=True)
        depth = models.PositiveSmallIntegerField(blank=True, null=True, default=DEFAULT_NAVIGATION_DEPTH, help_text="For the root of a hosted tree, defines the depth of the tree. A blank depth will hide this section of navigation. Otherwise, depth is ignored.")
        
        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.")
        
        order = models.PositiveSmallIntegerField(blank=True, null=True)
        depth = models.PositiveSmallIntegerField(blank=True, null=True, default=DEFAULT_NAVIGATION_DEPTH, help_text="For the root of a hosted tree, defines the depth of the tree. A blank depth will hide this section of navigation. Otherwise, depth is ignored.")
        
+       def __init__(self, *args, **kwargs):
+               super(Navigation, self).__init__(*args, **kwargs)
+               self._initial_data = model_to_dict(self)
+       
+       def __unicode__(self):
+               return self.get_path(field='text', pathsep=u' › ')
+       
        def clean(self):
                # Should this be enforced? Not enforcing it would allow creation of "headers" in the navbar.
                if not self.target_node and not self.url_or_subpath:
        def clean(self):
                # Should this be enforced? Not enforcing it would allow creation of "headers" in the navbar.
                if not self.target_node and not self.url_or_subpath:
@@ -113,7 +141,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:
        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
                                view_name = self.url_or_subpath
                                params = self.reversing_parameters
                                args = isinstance(params, list) and params or None
@@ -128,11 +156,22 @@ class Navigation(TreeEntity):
                        return node.get_absolute_url()
                else:
                        return self.url_or_subpath
                        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' › ')
+       def _has_changed(self):
+               if model_to_dict(self) == self._initial_data:
+                       return False
+               return True
+       
+       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)
        
        
-       # TODO: Add delete and save methods to handle cache clearing.
+       def delete(self, *args, **kwargs):
+               super(Navigation, self).delete(*args, **kwargs)
+               Navigation.objects.clear_cache(self)
        
        class Meta:
                ordering = ['order']
        
        class Meta:
                ordering = ['order']