X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/bc19cb3b97a92e3b42b6640e1c154e761cfb2f56..refs/heads/develop:/philo/contrib/shipherd/models.py?ds=sidebyside diff --git a/philo/contrib/shipherd/models.py b/philo/contrib/shipherd/models.py index 9bba816..95be501 100644 --- a/philo/contrib/shipherd/models.py +++ b/philo/contrib/shipherd/models.py @@ -2,6 +2,7 @@ from UserDict import DictMixin from hashlib import sha1 +from django.contrib.sites.models import Site from django.core.cache import cache from django.core.exceptions import ValidationError from django.core.urlresolvers import NoReverseMatch @@ -26,13 +27,12 @@ class NavigationMapper(object, DictMixin): self._cache = {} def __getitem__(self, key): - return Navigation.objects.get_for_node(self.node, key) - #if key not in self._cache: - # try: - # self._cache[key] = Navigation.objects.get_for_node(self.node, key) - # except Navigation.DoesNotExist: - # self._cache[key] = None - #return self._cache[key] + if key not in self._cache: + try: + self._cache[key] = Navigation.objects.get_for_node(self.node, key) + except Navigation.DoesNotExist: + self._cache[key] = None + return self._cache[key] def navigation(self): @@ -63,15 +63,20 @@ class NavigationManager(models.Manager): } navs = self.filter(key=key, **kwargs).select_related('node').order_by('-node__%s' % opts.level_attr) nav = navs[0] - roots = nav.roots.all().select_related('target_node') + roots = nav.roots.all().select_related('target_node').order_by('order') item_opts = NavigationItem._mptt_meta by_pk = {} tree_ids = [] + site_root_node = Site.objects.get_current().root_node + for root in roots: by_pk[root.pk] = root tree_ids.append(getattr(root, item_opts.tree_id_attr)) root._cached_children = [] + if root.target_node: + root.target_node.get_path(root=site_root_node) + root.navigation = nav kwargs = { '%s__in' % item_opts.tree_id_attr: tree_ids, @@ -85,6 +90,8 @@ class NavigationManager(models.Manager): parent_pk = getattr(item, '%s_id' % item_opts.parent_attr) item.parent = by_pk[parent_pk] item.parent._cached_children.append(item) + if item.target_node: + item.target_node.get_path(root=site_root_node) cached = roots cache.set(cache_key, cached) @@ -167,13 +174,15 @@ class NavigationItem(TreeEntity, TargetURLModel): # the same as the request path, check whether the target node is an ancestor # of the requested node. If so, this is active unless the target node # is the same as the ``host node`` for this navigation structure. - try: - host_node = self.get_root().navigation.node - except AttributeError: - pass - else: - if self.target_node != host_node and self.target_node.is_ancestor_of(request.node): - return True + root = self + + # The common case will be cached items, whose parents are cached with them. + while root.parent is not None: + root = root.parent + + host_node_id = root.navigation.node_id + if self.target_node.pk != host_node_id and self.target_node.is_ancestor_of(request.node): + return True return False