From 52d3a0f1ca43820d59447c256126aaba7a726dcd Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Fri, 24 Jun 2011 10:18:19 -0400 Subject: [PATCH] Corrections to has_navigation and navigation_host to use the new NavigationMapper correctly. Also added select_related calls to the NavigationManager.get_for_node method. --- philo/contrib/shipherd/models.py | 18 ++++++++++++------ .../contrib/shipherd/templatetags/shipherd.py | 10 ++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/philo/contrib/shipherd/models.py b/philo/contrib/shipherd/models.py index c8579a2..9888378 100644 --- a/philo/contrib/shipherd/models.py +++ b/philo/contrib/shipherd/models.py @@ -21,9 +21,15 @@ class NavigationMapper(object, DictMixin): """ def __init__(self, node): self.node = node + 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] def navigation(self): @@ -42,21 +48,21 @@ class NavigationManager(models.Manager): opts = Node._mptt_meta left = getattr(node, opts.left_attr) right = getattr(node, opts.right_attr) - tree = getattr(node, opts.tree_id_attr) + tree_id = getattr(node, opts.tree_id_attr) kwargs = { "node__%s__lte" % opts.left_attr: left, "node__%s__gte" % opts.right_attr: right, "node__%s" % opts.tree_id_attr: tree_id } - navs = self.filter(key=key, **kwargs).order_by('-node__%s' % opts.level_attr) + navs = self.filter(key=key, **kwargs).select_related('node').order_by('-node__%s' % opts.level_attr) nav = navs[0] - roots = nav.roots.all() + roots = nav.roots.all().select_related('target_node') item_opts = NavigationItem._mptt_meta by_pk = {} tree_ids = [] for root in roots: - by_pk[root.pk] = pk + by_pk[root.pk] = root tree_ids.append(getattr(root, item_opts.tree_id_attr)) root._cached_children = [] @@ -65,7 +71,7 @@ class NavigationManager(models.Manager): '%s__lt' % item_opts.level_attr: nav.depth, '%s__gt' % item_opts.level_attr: 0 } - items = NavigationItem.objects.filter(**kwargs).order_by('level', 'order') + items = NavigationItem.objects.filter(**kwargs).select_related('target_node').order_by('level', 'order') for item in items: by_pk[item.pk] = item item._cached_children = [] diff --git a/philo/contrib/shipherd/templatetags/shipherd.py b/philo/contrib/shipherd/templatetags/shipherd.py index 1031d73..833a995 100644 --- a/philo/contrib/shipherd/templatetags/shipherd.py +++ b/philo/contrib/shipherd/templatetags/shipherd.py @@ -173,13 +173,7 @@ def recursenavigation(parser, token): def has_navigation(node, key=None): """Returns ``True`` if the node has a :class:`.Navigation` with the given key and ``False`` otherwise. If ``key`` is ``None``, returns whether the node has any :class:`.Navigation`\ s at all.""" try: - nav = node.navigation - if key is not None: - if key in nav and bool(node.navigation[key]): - return True - elif key not in node.navigation: - return False - return bool(node.navigation) + return bool(node.navigation[key]) except: return False @@ -188,6 +182,6 @@ def has_navigation(node, key=None): def navigation_host(node, key): """Returns the :class:`.Node` which hosts the :class:`.Navigation` which ``node`` has inherited for ``key``. Returns ``node`` if any exceptions are encountered.""" try: - return Navigation.objects.filter(node__in=node.get_ancestors(include_self=True), key=key).order_by('-node__level')[0].node + return node.navigation[key].node except: return node \ No newline at end of file -- 2.20.1