X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/be40499f02a3457bb17a8dc02a78224823eb1406..f50cd63e40d7dfc9ffb51b679748b0003fddb019:/models/nodes.py diff --git a/models/nodes.py b/models/nodes.py index de10ed1..2bfb4fd 100644 --- a/models/nodes.py +++ b/models/nodes.py @@ -127,6 +127,28 @@ class MultiView(View): 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