From 74f7ad32a51c7bf9e013f1865e4039ac07fd71b9 Mon Sep 17 00:00:00 2001
From: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat, 2 Apr 2011 13:55:03 +0000
Subject: [PATCH] Fixed #15717 -- Updated databrowse to use class-based generic
 date views. Thanks to aaugustin for the report and draft patch

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16002 bcc190cf-cafb-0310-a4f2-bffc1f526a37
---
 .../contrib/databrowse/plugins/calendars.py   | 90 +++++++++++++++----
 1 file changed, 74 insertions(+), 16 deletions(-)

diff --git a/django/contrib/databrowse/plugins/calendars.py b/django/contrib/databrowse/plugins/calendars.py
index 8a08cfd8eb..4a9468e396 100644
--- a/django/contrib/databrowse/plugins/calendars.py
+++ b/django/contrib/databrowse/plugins/calendars.py
@@ -6,9 +6,43 @@ from django.shortcuts import render_to_response
 from django.utils.text import capfirst
 from django.utils.encoding import force_unicode
 from django.utils.safestring import mark_safe
-from django.views.generic import date_based
+from django.views.generic import dates
 from django.utils import datetime_safe
 
+
+class DateViewMixin(object):
+    allow_empty = False
+    allow_future = True
+    root_url = None
+    model = None
+    field = None
+
+    def get_context_data(self, **kwargs):
+        context = super(DateViewMixin, self).get_context_data(**kwargs)
+        context.update({
+            'root_url': self.root_url,
+            'model': self.model,
+            'field': self.field
+        })
+        return context
+
+
+class DayView(DateViewMixin, dates.DayArchiveView):
+    template_name = 'databrowse/calendar_day.html'
+
+
+class MonthView(DateViewMixin, dates.MonthArchiveView):
+    template_name = 'databrowse/calendar_month.html'
+
+
+class YearView(DateViewMixin, dates.YearArchiveView):
+    template_name = 'databrowse/calendar_year.html'
+
+
+class IndexView(DateViewMixin, dates.ArchiveIndexView):
+    template_name = 'databrowse/calendar_main.html'
+
+
 class CalendarPlugin(DatabrowsePlugin):
     def __init__(self, field_names=None):
         self.field_names = field_names
@@ -61,26 +95,50 @@ class CalendarPlugin(DatabrowsePlugin):
         easy_model = EasyModel(self.site, self.model)
         field_list = self.fields.values()
         field_list.sort(key=lambda k:k.verbose_name)
-        return render_to_response('databrowse/calendar_homepage.html', {'root_url': self.site.root_url, 'model': easy_model, 'field_list': field_list})
+        return render_to_response('databrowse/calendar_homepage.html', {
+                'root_url': self.site.root_url,
+                'model': easy_model,
+                'field_list': field_list
+            })
 
     def calendar_view(self, request, field, year=None, month=None, day=None):
         easy_model = EasyModel(self.site, self.model)
-        queryset = easy_model.get_query_set()
-        extra_context = {'root_url': self.site.root_url, 'model': easy_model, 'field': field}
+        root_url = self.site.root_url
+
         if day is not None:
-            return date_based.archive_day(request, year, month, day, queryset, field.name,
-                template_name='databrowse/calendar_day.html', allow_empty=False, allow_future=True,
-                extra_context=extra_context)
+            return DayView.as_view(
+                                year=year, month=month, day=day,
+                                date_field=field.name,
+                                queryset=easy_model.get_query_set(),
+                                root_url=root_url,
+                                model=easy_model,
+                                field=field
+                            )(request)
         elif month is not None:
-            return date_based.archive_month(request, year, month, queryset, field.name,
-                template_name='databrowse/calendar_month.html', allow_empty=False, allow_future=True,
-                extra_context=extra_context)
+            return MonthView.as_view(
+                                year=year, month=month,
+                                date_field=field.name,
+                                queryset=easy_model.get_query_set(),
+                                root_url=root_url,
+                                model=easy_model,
+                                field=field
+                            )(request)
         elif year is not None:
-            return date_based.archive_year(request, year, queryset, field.name,
-                template_name='databrowse/calendar_year.html', allow_empty=False, allow_future=True,
-                extra_context=extra_context)
+            return YearView.as_view(
+                                year=year,
+                                date_field=field.name,
+                                queryset=easy_model.get_query_set(),
+                                root_url=root_url,
+                                model=easy_model,
+                                field=field
+                            )(request)
         else:
-            return date_based.archive_index(request, queryset, field.name,
-                template_name='databrowse/calendar_main.html', allow_empty=True, allow_future=True,
-                extra_context=extra_context)
+            return IndexView.as_view(
+                                date_field=field.name,
+                                queryset=easy_model.get_query_set(),
+                                root_url=root_url,
+                                model=easy_model,
+                                field=field
+                            )(request)
+
         assert False, ('%s, %s, %s, %s' % (field, year, month, day))