X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/8aad28a2a098d4f6c0401070e28081c55d8d1e6e..6ac457d4ac226a474e988dfb898682ae04a86eb0:/contrib/shipherd/models.py diff --git a/contrib/shipherd/models.py b/contrib/shipherd/models.py index 010aa9f..bddaef3 100644 --- a/contrib/shipherd/models.py +++ b/contrib/shipherd/models.py @@ -4,7 +4,7 @@ from django.core.urlresolvers import NoReverseMatch from django.core.validators import RegexValidator, MinValueValidator from django.db import models from django.forms.models import model_to_dict -from philo.models import TreeEntity, JSONField, Node, TreeManager, Entity +from philo.models import TreeEntity, Node, TreeManager, Entity, TargetURLModel from philo.validators import RedirectValidator from UserDict import DictMixin @@ -155,6 +155,7 @@ class NavigationManager(models.Manager): # A distinct query is not strictly necessary. TODO: benchmark the efficiency # with/without distinct. + #change to navigation_navigation_items targets = list(Node.objects.filter(navigation_items__in=items).distinct()) for cache in caches: @@ -204,16 +205,12 @@ class NavigationItemManager(TreeManager): return NavigationCacheQuerySet(self.model, using=self._db) -class NavigationItem(TreeEntity): +class NavigationItem(TreeEntity, TargetURLModel): objects = NavigationItemManager() navigation = models.ForeignKey(Navigation, blank=True, null=True, related_name='roots', help_text="Be a root in this navigation tree.") text = models.CharField(max_length=50) - target_node = models.ForeignKey(Node, blank=True, null=True, related_name='navigation_items', help_text="Point to this node's url.") - url_or_subpath = models.CharField(max_length=200, validators=[RedirectValidator()], blank=True, help_text="Point to this url or, if a node is defined and accepts subpaths, this subpath of the node.") - reversing_parameters = JSONField(blank=True, help_text="If reversing parameters are defined, url_or_subpath will instead be interpreted as the view name to be reversed.") - order = models.PositiveSmallIntegerField(default=0) def __init__(self, *args, **kwargs): @@ -225,41 +222,10 @@ class NavigationItem(TreeEntity): return self.get_path(field='text', pathsep=u' › ') def clean(self): - # Should this be enforced? Not enforcing it would allow creation of "headers" in the navbar. - if not self.target_node and not self.url_or_subpath: - raise ValidationError("Either a target node or a url must be defined.") - - if self.reversing_parameters and (not self.url_or_subpath or not self.target_node): - raise ValidationError("Reversing parameters require a view name and a target node.") - - try: - self.get_target_url() - except NoReverseMatch, e: - raise ValidationError(e.message) - + super(NavigationItem, self).clean() if bool(self.parent) == bool(self.navigation): raise ValidationError("Exactly one of `parent` and `navigation` must be defined.") - def get_target_url(self): - node = self.target_node - if node is not None and node.accepts_subpath and self.url_or_subpath: - if self.reversing_parameters is not None: - view_name = self.url_or_subpath - params = self.reversing_parameters - args = isinstance(params, list) and params or None - kwargs = isinstance(params, dict) and params or None - subpath = node.view.reverse(view_name, args=args, kwargs=kwargs) - else: - subpath = self.url_or_subpath - if subpath[0] != '/': - subpath = '/' + subpath - return node.construct_url(subpath) - elif node is not None: - return node.get_absolute_url() - else: - return self.url_or_subpath - target_url = property(get_target_url) - def is_active(self, request): if self.target_url == request.path: # Handle the `default` case where the target_url and requested path