/usr/share/pyshared/extra_views/dates.py is in python-django-extra-views 0.2.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | from django.views.generic.dates import BaseMonthArchiveView
from django.views.generic.list import MultipleObjectTemplateResponseMixin
from django.core.exceptions import ImproperlyConfigured
from calendar import Calendar
from collections import defaultdict
import datetime
class BaseCalendarMonthArchiveView(BaseMonthArchiveView):
first_of_week = 0 # 0 = Monday, 6 = Sunday
paginate_by = None
def get_paginate_by(self, queryset):
if self.paginate_by is not None:
raise ImproperlyConfigured(u"'%s' cannot be paginated, it is a calendar view" % self.__class__.__name__)
return None
def get_first_of_week(self):
return self.first_of_week
def get_context_data(self, **kwargs):
data = super(BaseCalendarMonthArchiveView, self).get_context_data(**kwargs)
date = data['date_list'][0]
cal = Calendar(self.get_first_of_week())
month_calendar = []
now = datetime.datetime.utcnow()
date_lists = defaultdict(list)
for obj in data['object_list']:
obj_date = getattr(obj, self.get_date_field())
try:
obj_date = obj_date.date()
except AttributeError:
# It's a date rather than datetime, so we use it as is
pass
date_lists[obj_date].append(obj)
for week in cal.monthdatescalendar(date.year, date.month):
week_calendar = []
for day in week:
week_calendar.append({
'day': day,
'object_list': date_lists[day],
'today': day == now.date(),
'is_current_month': day.month == date.month,
})
month_calendar.append(week_calendar)
data['calendar'] = month_calendar
return data
class CalendarMonthArchiveView(MultipleObjectTemplateResponseMixin, BaseCalendarMonthArchiveView):
template_name_suffix = '_calendar_month'
|