Merged cowell back into core.
[philo.git] / models / nodes.py
index 90184ab..2bfb4fd 100644 (file)
@@ -10,7 +10,6 @@ from django.template import add_to_builtins as register_templatetags
 from inspect import getargspec
 from philo.exceptions import MIDDLEWARE_NOT_CONFIGURED
 from philo.models.base import TreeEntity, Entity, QuerySetMapper, register_value_model
 from inspect import getargspec
 from philo.exceptions import MIDDLEWARE_NOT_CONFIGURED
 from philo.models.base import TreeEntity, Entity, QuerySetMapper, register_value_model
-from philo.models.fields import JSONField
 from philo.utils import ContentTypeSubclassLimiter
 from philo.validators import RedirectValidator
 from philo.exceptions import ViewCanNotProvideSubpath, ViewDoesNotProvideSubpaths, AncestorDoesNotExist
 from philo.utils import ContentTypeSubclassLimiter
 from philo.validators import RedirectValidator
 from philo.exceptions import ViewCanNotProvideSubpath, ViewDoesNotProvideSubpaths, AncestorDoesNotExist
@@ -18,7 +17,6 @@ from philo.signals import view_about_to_render, view_finished_rendering
 
 
 _view_content_type_limiter = ContentTypeSubclassLimiter(None)
 
 
 _view_content_type_limiter = ContentTypeSubclassLimiter(None)
-DEFAULT_NAVIGATION_DEPTH = 3
 
 
 class Node(TreeEntity):
 
 
 class Node(TreeEntity):
@@ -50,43 +48,6 @@ class Node(TreeEntity):
                except AncestorDoesNotExist, ViewDoesNotExist:
                        return None
        
                except AncestorDoesNotExist, ViewDoesNotExist:
                        return None
        
-       def get_navigation(self, depth=DEFAULT_NAVIGATION_DEPTH, current_depth=0, found_node_pks=None):
-               navigation = self.view.get_navigation(self, depth, current_depth)
-               
-               if depth == current_depth:
-                       return navigation
-               import pdb
-               pdb.set_trace()
-               found_node_pks = found_node_pks or [self.pk]
-               ordered_child_pks = NodeNavigationOverride.objects.filter(parent=self, child__parent=self).values_list('child__pk', flat=True)
-               
-               children = self.children.exclude(pk__in=found_node_pks)
-               ordered_children = children.filter(pk__in=ordered_child_pks)
-               unordered_children = children.exclude(pk__in=ordered_child_pks)
-               
-               children = list(ordered_children) + list(unordered_children)
-               
-               if children:
-                       child_navigation = []
-                       for child in children:
-                               found_node_pks.append(child.pk)
-                               try:
-                                       child_navigation.append(child.get_navigation(depth, current_depth + 1, found_node_pks))
-                               except NotImplementedError:
-                                       pass
-                       
-                       if child_navigation:
-                               if 'children' in navigation:
-                                       navigation['children'] += child_navigation
-                               else:
-                                       navigation['children'] = child_navigation
-               
-               return navigation
-       
-       def save(self):
-               super(Node, self).save()
-               
-       
        class Meta:
                app_label = 'philo'
 
        class Meta:
                app_label = 'philo'
 
@@ -95,32 +56,6 @@ class Node(TreeEntity):
 models.ForeignKey(Node, related_name='sites', null=True, blank=True).contribute_to_class(Site, 'root_node')
 
 
 models.ForeignKey(Node, related_name='sites', null=True, blank=True).contribute_to_class(Site, 'root_node')
 
 
-class NodeNavigationOverride(Entity):
-       parent = models.ForeignKey(Node, related_name="navigation_override_child_set")
-       child = models.OneToOneField(Node, related_name="navigation_override")
-       url = models.CharField(max_length=200, validators=[RedirectValidator()], blank=True)
-       title = models.CharField(max_length=100, blank=True)
-       order = models.PositiveSmallIntegerField(blank=True, null=True)
-       child_navigation = JSONField()
-       
-       def get_navigation(self, node, depth, current_depth):
-               if self.child_navigation:
-                       depth = current_depth
-               default = node.get_navigation(depth, current_depth)
-               if self.url:
-                       default['url'] = self.url
-               if self.title:
-                       default['title'] = self.title
-               if self.child_navigation:
-                       if 'children' in default:
-                               default['children'] += self.child_navigation
-                       else:
-                               default['children'] = self.child_navigation
-       
-       class Meta:
-               ordering = ['order']
-
-
 class View(Entity):
        nodes = generic.GenericRelation(Node, content_type_field='view_content_type', object_id_field='view_object_id')
        
 class View(Entity):
        nodes = generic.GenericRelation(Node, content_type_field='view_content_type', object_id_field='view_object_id')
        
@@ -156,9 +91,6 @@ class View(Entity):
        def actually_render_to_response(self, request, extra_context=None):
                raise NotImplementedError('View subclasses must implement render_to_response.')
        
        def actually_render_to_response(self, request, extra_context=None):
                raise NotImplementedError('View subclasses must implement render_to_response.')
        
-       def get_navigation(self, node, depth, current_depth):
-               raise NotImplementedError('View subclasses must implement get_navigation.')
-       
        class Meta:
                abstract = True
 
        class Meta:
                abstract = True
 
@@ -187,6 +119,36 @@ class MultiView(View):
                        kwargs['extra_context'] = extra_context
                return view(request, *args, **kwargs)
        
                        kwargs['extra_context'] = extra_context
                return view(request, *args, **kwargs)
        
+       def reverse(self, view_name, args=None, kwargs=None, node=None):
+               """Shortcut method to handle the common pattern of getting the absolute url for a multiview's
+               subpaths."""
+               subpath = reverse(view_name, urlconf=self, args=args or [], kwargs=kwargs or {})
+               if node is not None:
+                       return '/%s/%s/' % (node.get_absolute_url().strip('/'), subpath.strip('/'))
+               return subpath
+       
+       def get_context(self):
+               """Hook for providing instance-specific context - such as the value of a Field - to all views."""
+               return {}
+       
+       def basic_view(self, view_name):
+               """
+               Wraps a field name and returns a simple view function that will render that view
+               with a basic context. This assumes that the field name is a ForeignKey to a
+               model with a render_to_response method.
+               """
+               field = self._meta.get_field(view_name)
+               view = getattr(self, field.name, None)
+               
+               def inner(request, extra_context=None, **kwargs):
+                       if not view:
+                               raise Http404
+                       context = self.get_context()
+                       context.update(extra_context or {})
+                       return view.render_to_response(request, extra_context=context)
+               
+               return inner
+       
        class Meta:
                abstract = True
 
        class Meta:
                abstract = True