From 9b40768160da75802636fc2b45c1f68bd36fd7bd Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Tue, 10 Aug 2010 15:51:39 -0400 Subject: [PATCH] Implements get_absolute_url for nodes (Feature #17). Fixes that mysterious 'name' bug. ;-) --- exceptions.py | 8 +++++++- models/base.py | 5 ++++- models/nodes.py | 9 ++++++++- models/pages.py | 2 +- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/exceptions.py b/exceptions.py index b40f08a..4759bae 100644 --- a/exceptions.py +++ b/exceptions.py @@ -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 diff --git a/models/base.py b/models/base.py index 7a872ca..9da9230 100644 --- a/models/base.py +++ b/models/base.py @@ -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: diff --git a/models/nodes.py b/models/nodes.py index 56cbb12..23be42b 100644 --- a/models/nodes.py +++ b/models/nodes.py @@ -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' diff --git a/models/pages.py b/models/pages.py index ff8e876..ed55ca5 100644 --- a/models/pages.py +++ b/models/pages.py @@ -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) -- 2.20.1