diff --git a/django/contrib/databrowse/plugins/calendars.py b/django/contrib/databrowse/plugins/calendars.py index 8a08cfd8eb9..4a9468e3960 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))