Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15717 -- Updated databrowse to use class-based generic date vi…

…ews. 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
  • Loading branch information...
commit 74f7ad32a51c7bf9e013f1865e4039ac07fd71b9 1 parent e6a5e41
Russell Keith-Magee authored April 02, 2011
90  django/contrib/databrowse/plugins/calendars.py
@@ -6,9 +6,43 @@
6 6
 from django.utils.text import capfirst
7 7
 from django.utils.encoding import force_unicode
8 8
 from django.utils.safestring import mark_safe
9  
-from django.views.generic import date_based
  9
+from django.views.generic import dates
10 10
 from django.utils import datetime_safe
11 11
 
  12
+
  13
+class DateViewMixin(object):
  14
+    allow_empty = False
  15
+    allow_future = True
  16
+    root_url = None
  17
+    model = None
  18
+    field = None
  19
+
  20
+    def get_context_data(self, **kwargs):
  21
+        context = super(DateViewMixin, self).get_context_data(**kwargs)
  22
+        context.update({
  23
+            'root_url': self.root_url,
  24
+            'model': self.model,
  25
+            'field': self.field
  26
+        })
  27
+        return context
  28
+
  29
+
  30
+class DayView(DateViewMixin, dates.DayArchiveView):
  31
+    template_name = 'databrowse/calendar_day.html'
  32
+
  33
+
  34
+class MonthView(DateViewMixin, dates.MonthArchiveView):
  35
+    template_name = 'databrowse/calendar_month.html'
  36
+
  37
+
  38
+class YearView(DateViewMixin, dates.YearArchiveView):
  39
+    template_name = 'databrowse/calendar_year.html'
  40
+
  41
+
  42
+class IndexView(DateViewMixin, dates.ArchiveIndexView):
  43
+    template_name = 'databrowse/calendar_main.html'
  44
+
  45
+
12 46
 class CalendarPlugin(DatabrowsePlugin):
13 47
     def __init__(self, field_names=None):
14 48
         self.field_names = field_names
@@ -61,26 +95,50 @@ def homepage_view(self, request):
61 95
         easy_model = EasyModel(self.site, self.model)
62 96
         field_list = self.fields.values()
63 97
         field_list.sort(key=lambda k:k.verbose_name)
64  
-        return render_to_response('databrowse/calendar_homepage.html', {'root_url': self.site.root_url, 'model': easy_model, 'field_list': field_list})
  98
+        return render_to_response('databrowse/calendar_homepage.html', {
  99
+                'root_url': self.site.root_url,
  100
+                'model': easy_model,
  101
+                'field_list': field_list
  102
+            })
65 103
 
66 104
     def calendar_view(self, request, field, year=None, month=None, day=None):
67 105
         easy_model = EasyModel(self.site, self.model)
68  
-        queryset = easy_model.get_query_set()
69  
-        extra_context = {'root_url': self.site.root_url, 'model': easy_model, 'field': field}
  106
+        root_url = self.site.root_url
  107
+
70 108
         if day is not None:
71  
-            return date_based.archive_day(request, year, month, day, queryset, field.name,
72  
-                template_name='databrowse/calendar_day.html', allow_empty=False, allow_future=True,
73  
-                extra_context=extra_context)
  109
+            return DayView.as_view(
  110
+                                year=year, month=month, day=day,
  111
+                                date_field=field.name,
  112
+                                queryset=easy_model.get_query_set(),
  113
+                                root_url=root_url,
  114
+                                model=easy_model,
  115
+                                field=field
  116
+                            )(request)
74 117
         elif month is not None:
75  
-            return date_based.archive_month(request, year, month, queryset, field.name,
76  
-                template_name='databrowse/calendar_month.html', allow_empty=False, allow_future=True,
77  
-                extra_context=extra_context)
  118
+            return MonthView.as_view(
  119
+                                year=year, month=month,
  120
+                                date_field=field.name,
  121
+                                queryset=easy_model.get_query_set(),
  122
+                                root_url=root_url,
  123
+                                model=easy_model,
  124
+                                field=field
  125
+                            )(request)
78 126
         elif year is not None:
79  
-            return date_based.archive_year(request, year, queryset, field.name,
80  
-                template_name='databrowse/calendar_year.html', allow_empty=False, allow_future=True,
81  
-                extra_context=extra_context)
  127
+            return YearView.as_view(
  128
+                                year=year,
  129
+                                date_field=field.name,
  130
+                                queryset=easy_model.get_query_set(),
  131
+                                root_url=root_url,
  132
+                                model=easy_model,
  133
+                                field=field
  134
+                            )(request)
82 135
         else:
83  
-            return date_based.archive_index(request, queryset, field.name,
84  
-                template_name='databrowse/calendar_main.html', allow_empty=True, allow_future=True,
85  
-                extra_context=extra_context)
  136
+            return IndexView.as_view(
  137
+                                date_field=field.name,
  138
+                                queryset=easy_model.get_query_set(),
  139
+                                root_url=root_url,
  140
+                                model=easy_model,
  141
+                                field=field
  142
+                            )(request)
  143
+
86 144
         assert False, ('%s, %s, %s, %s' % (field, year, month, day))

0 notes on commit 74f7ad3

Please sign in to comment.
Something went wrong with that request. Please try again.