Add dynamic-class trickery to get ListView titles.
[cookbook.git] / cookbook / views.py
index dbc1bd2166c66c416f8a94efc442004bd4050abe..648ec66f2dd1ffa458b3ad8f0263e84d3c2cc6e4 100644 (file)
@@ -1,6 +1,7 @@
 from django.http import HttpResponse
 from django.shortcuts import render_to_response, get_object_or_404
 from django.template import RequestContext
+from django.views.generic import ListView
 
 from taggit.models import Tag
 from .models import Recipe
@@ -12,3 +13,20 @@ def tag(request, pk):
     return render_to_response(
         'cookbook/recipes.html', {'recipes': recipes, 'title': tag.name},
         RequestContext(request))
+
+class StaticContextListView (ListView):
+    _name_counter = 0
+    def get_context_data(self, **kwargs):
+        context = super(StaticContextListView, self).get_context_data(**kwargs)
+        context.update(self._extra_context)
+        return context
+
+def static_context_list_view_factory(extra_context):
+    class_name = 'StaticContextListView_{:d}'.format(
+        StaticContextListView._name_counter)
+    StaticContextListView._name_counter += 1
+    class_bases = (StaticContextListView,)
+    class_dict = dict(StaticContextListView.__dict__)
+    new_class = type(class_name, class_bases, class_dict)
+    new_class._extra_context = extra_context
+    return new_class