X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/54535e5c96e8a7ed0055d27e01cdbd25b44d4c15..048812e5eaeff5eb9ba3b1877cdb9fb3a84662aa:/models/nodes.py?ds=inline diff --git a/models/nodes.py b/models/nodes.py index 0ece55f..526400c 100644 --- a/models/nodes.py +++ b/models/nodes.py @@ -89,7 +89,7 @@ class View(Entity): return response def actually_render_to_response(self, request, extra_context=None): - raise NotImplementedError('View subclasses must implement render_to_response.') + raise NotImplementedError('View subclasses must implement actually_render_to_response.') class Meta: abstract = True @@ -102,7 +102,7 @@ class MultiView(View): accepts_subpath = True @property - def urlpatterns(self, obj): + def urlpatterns(self): raise NotImplementedError("MultiView subclasses must implement urlpatterns.") def actually_render_to_response(self, request, extra_context=None): @@ -119,6 +119,39 @@ 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, field_name): + """ + Given the name of a field on ``self``, accesses the value of + that field and treats it as a ``View`` instance. Creates a + basic context based on self.get_context() and any extra_context + that was passed in, then calls the ``View`` instance's + render_to_response() method. This method is meant to be called + to return a view function appropriate for urlpatterns. + """ + field = self._meta.get_field(field_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 @@ -140,7 +173,6 @@ class Redirect(View): app_label = 'philo' -# Why does this exist? class File(View): """ For storing arbitrary files """