Added catch to NavigationManager.get_for_node for cases where items do not have a...
[philo.git] / philo / contrib / shipherd / models.py
index c299a14..95be501 100644 (file)
@@ -27,13 +27,12 @@ class NavigationMapper(object, DictMixin):
                self._cache = {}
        
        def __getitem__(self, key):
                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):
 
 
 def navigation(self):
@@ -62,7 +61,7 @@ class NavigationManager(models.Manager):
                                "node__%s__gte" % opts.right_attr: right,
                                "node__%s" % opts.tree_id_attr: tree_id
                        }
                                "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().select_related('target_node').order_by('order')
                        item_opts = NavigationItem._mptt_meta
                        nav = navs[0]
                        roots = nav.roots.all().select_related('target_node').order_by('order')
                        item_opts = NavigationItem._mptt_meta
@@ -75,7 +74,8 @@ class NavigationManager(models.Manager):
                                by_pk[root.pk] = root
                                tree_ids.append(getattr(root, item_opts.tree_id_attr))
                                root._cached_children = []
                                by_pk[root.pk] = root
                                tree_ids.append(getattr(root, item_opts.tree_id_attr))
                                root._cached_children = []
-                               root.target_node.get_path(root=site_root_node)
+                               if root.target_node:
+                                       root.target_node.get_path(root=site_root_node)
                                root.navigation = nav
                        
                        kwargs = {
                                root.navigation = nav
                        
                        kwargs = {
@@ -90,7 +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)
                                parent_pk = getattr(item, '%s_id' % item_opts.parent_attr)
                                item.parent = by_pk[parent_pk]
                                item.parent._cached_children.append(item)
-                               item.target_node.get_path(root=site_root_node)
+                               if item.target_node:
+                                       item.target_node.get_path(root=site_root_node)
                        
                        cached = roots
                        cache.set(cache_key, cached)
                        
                        cached = roots
                        cache.set(cache_key, cached)