Merge branch 'master' of git://github.com/melinath/philo
[philo.git] / models / nodes.py
index 0ece55f..2bfb4fd 100644 (file)
@@ -119,6 +119,36 @@ class MultiView(View):
                        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