From 7ebd19fad0a3feffc6ed3c3917799c26aaa223d3 Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Wed, 22 Jun 2011 11:48:31 -0400 Subject: [PATCH] Eliminated (Generic)ForeignKey evaluations in shipherd's NavigationManager.update_cache_for, Node.accepts_subpath, and templatetages.nodes.NodeURLNode.render. --- philo/contrib/shipherd/models.py | 4 ++-- philo/models/nodes.py | 6 +++--- philo/templatetags/nodes.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/philo/contrib/shipherd/models.py b/philo/contrib/shipherd/models.py index 429faaa..de9a869 100644 --- a/philo/contrib/shipherd/models.py +++ b/philo/contrib/shipherd/models.py @@ -163,12 +163,12 @@ class NavigationManager(models.Manager): # A distinct query is not strictly necessary. TODO: benchmark the efficiency # with/without distinct. - targets = list(Node.objects.filter(pk__in=target_pks).distinct()) + targets = dict([(n.pk, n) for n in Node.objects.filter(pk__in=target_pks).distinct()]) for cache in caches: for item in cache['items']: if item.target_node_id: - item.target_node = targets[targets.index(item.target_node)] + item.target_node = targets[item.target_node_id] def clear_cache(self): """Clears the manager's entire navigation cache.""" diff --git a/philo/models/nodes.py b/philo/models/nodes.py index 5b8b8ed..9309a42 100644 --- a/philo/models/nodes.py +++ b/philo/models/nodes.py @@ -39,8 +39,8 @@ class Node(SlugTreeEntity): @property def accepts_subpath(self): """A property shortcut for :attr:`self.view.accepts_subpath `""" - if self.view: - return self.view.accepts_subpath + if self.view_object_id and self.view_content_type_id: + return ContentType.objects.get_for_id(self.view_content_type_id).model_class().accepts_subpath return False def handles_subpath(self, subpath): @@ -126,7 +126,7 @@ class View(Entity): #: A generic relation back to nodes. nodes = generic.GenericRelation(Node, content_type_field='view_content_type', object_id_field='view_object_id') - #: Property or attribute which defines whether this :class:`View` can handle subpaths. Default: ``False`` + #: An attribute on the class which defines whether this :class:`View` can handle subpaths. Default: ``False`` accepts_subpath = False def handles_subpath(self, subpath): diff --git a/philo/templatetags/nodes.py b/philo/templatetags/nodes.py index 189fdd5..52da236 100644 --- a/philo/templatetags/nodes.py +++ b/philo/templatetags/nodes.py @@ -39,7 +39,7 @@ class NodeURLNode(template.Node): if self.with_obj is None and self.view_name is None: url = node.get_absolute_url() else: - if not node.view.accepts_subpath: + if not node.accepts_subpath: return settings.TEMPLATE_STRING_IF_INVALID if self.with_obj is not None: -- 2.20.1