Implements get_absolute_url for nodes (Feature #17). Fixes that mysterious 'name...
authorStephen Burrows <stephen.r.burrows@gmail.com>
Tue, 10 Aug 2010 19:51:39 +0000 (15:51 -0400)
committerStephen Burrows <stephen.r.burrows@gmail.com>
Tue, 10 Aug 2010 23:10:49 +0000 (19:10 -0400)
exceptions.py
models/base.py
models/nodes.py
models/pages.py

index b40f08a..4759bae 100644 (file)
@@ -2,6 +2,12 @@ class ViewDoesNotProvideSubpaths(Exception):
        """ Raised by get_subpath when the View does not provide subpaths (the default). """
        silent_variable_failure = True
 
+
 class ViewCanNotProvideSubpath(Exception):
        """ Raised by get_subpath when the View can not provide a subpath for the supplied object. """
-       silent_variable_failure = True
\ No newline at end of file
+       silent_variable_failure = True
+
+
+class AncestorDoesNotExist(Exception):
+       """ Raised by get_path if the root model is not an ancestor of the current model """
+       pass
\ No newline at end of file
index 7a872ca..9da9230 100644 (file)
@@ -3,6 +3,7 @@ from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes import generic
 from django.utils import simplejson as json
 from django.core.exceptions import ObjectDoesNotExist
+from philo.exceptions import AncestorDoesNotExist
 from philo.utils import ContentTypeRegistryLimiter
 from philo.signals import entity_class_prepared
 from UserDict import DictMixin
@@ -251,7 +252,9 @@ class TreeModel(models.Model):
                return False
        
        def get_path(self, root=None, pathsep='/', field='slug'):
-               if root is not None and self.has_ancestor(root):
+               if root is not None:
+                       if not self.has_ancestor(root):
+                               raise AncestorDoesNotExist(root)
                        path = ''
                        parent = self
                        while parent and parent != root:
index 56cbb12..23be42b 100644 (file)
@@ -10,7 +10,7 @@ from inspect import getargspec
 from philo.models.base import TreeEntity, Entity, QuerySetMapper, register_value_model
 from philo.utils import ContentTypeSubclassLimiter
 from philo.validators import RedirectValidator
-from philo.exceptions import ViewDoesNotProvideSubpaths
+from philo.exceptions import ViewDoesNotProvideSubpaths, AncestorDoesNotExist
 
 
 _view_content_type_limiter = ContentTypeSubclassLimiter(None)
@@ -30,6 +30,13 @@ class Node(TreeEntity):
        def render_to_response(self, request, path=None, subpath=None, extra_context=None):
                return self.view.render_to_response(self, request, path, subpath, extra_context)
        
+       def get_absolute_url(self):
+               root = Site.objects.get_current().root_node
+               try:
+                       return '/%s' % self.get_path(root=root)
+               except AncestorDoesNotExist:
+                       return None
+       
        class Meta:
                app_label = 'philo'
 
index ff8e876..ed55ca5 100644 (file)
@@ -76,7 +76,7 @@ class Template(TreeModel):
                return contentlet_node_names, contentreference_node_specs
        
        def __unicode__(self):
-               return self.get_path(u' › ', 'name')
+               return self.get_path(pathsep=u' › ', field='name')
        
        @staticmethod
        @fattr(is_usable=True)