2010-10-18 21:34:47 +08:00
|
|
|
import datetime
|
2015-01-28 20:35:27 +08:00
|
|
|
|
2012-05-01 02:41:38 +08:00
|
|
|
from django.conf import settings
|
2010-10-18 21:34:47 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import models
|
2010-10-18 21:34:47 +08:00
|
|
|
from django.http import Http404
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils import timezone
|
2012-05-01 02:41:38 +08:00
|
|
|
from django.utils.functional import cached_property
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext as _
|
2010-10-18 21:34:47 +08:00
|
|
|
from django.views.generic.base import View
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.views.generic.detail import (
|
|
|
|
BaseDetailView, SingleObjectTemplateResponseMixin,
|
|
|
|
)
|
|
|
|
from django.views.generic.list import (
|
|
|
|
MultipleObjectMixin, MultipleObjectTemplateResponseMixin,
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class YearMixin:
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Mixin for views manipulating year-based data."""
|
2010-10-18 21:34:47 +08:00
|
|
|
year_format = '%Y'
|
|
|
|
year = None
|
|
|
|
|
|
|
|
def get_year_format(self):
|
|
|
|
"""
|
|
|
|
Get a year format string in strptime syntax to be used to parse the
|
|
|
|
year from url variables.
|
|
|
|
"""
|
|
|
|
return self.year_format
|
|
|
|
|
|
|
|
def get_year(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the year for which this view should display data."""
|
2010-10-18 21:34:47 +08:00
|
|
|
year = self.year
|
|
|
|
if year is None:
|
|
|
|
try:
|
|
|
|
year = self.kwargs['year']
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
year = self.request.GET['year']
|
|
|
|
except KeyError:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise Http404(_("No year specified"))
|
2010-10-18 21:34:47 +08:00
|
|
|
return year
|
|
|
|
|
2012-05-24 19:02:19 +08:00
|
|
|
def get_next_year(self, date):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Get the next valid year."""
|
2012-05-24 19:02:19 +08:00
|
|
|
return _get_next_prev(self, date, is_previous=False, period='year')
|
|
|
|
|
|
|
|
def get_previous_year(self, date):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Get the previous valid year."""
|
2012-05-24 19:02:19 +08:00
|
|
|
return _get_next_prev(self, date, is_previous=True, period='year')
|
|
|
|
|
2012-05-17 23:41:37 +08:00
|
|
|
def _get_next_year(self, date):
|
|
|
|
"""
|
|
|
|
Return the start date of the next interval.
|
|
|
|
|
|
|
|
The interval is defined by start date <= item date < next start date.
|
|
|
|
"""
|
2017-05-16 02:56:30 +08:00
|
|
|
try:
|
|
|
|
return date.replace(year=date.year + 1, month=1, day=1)
|
|
|
|
except ValueError:
|
|
|
|
raise Http404(_("Date out of range"))
|
2012-05-17 23:41:37 +08:00
|
|
|
|
|
|
|
def _get_current_year(self, date):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the start date of the current interval."""
|
2012-05-17 23:41:37 +08:00
|
|
|
return date.replace(month=1, day=1)
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class MonthMixin:
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Mixin for views manipulating month-based data."""
|
2010-10-18 21:34:47 +08:00
|
|
|
month_format = '%b'
|
|
|
|
month = None
|
|
|
|
|
|
|
|
def get_month_format(self):
|
|
|
|
"""
|
|
|
|
Get a month format string in strptime syntax to be used to parse the
|
|
|
|
month from url variables.
|
|
|
|
"""
|
|
|
|
return self.month_format
|
|
|
|
|
|
|
|
def get_month(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the month for which this view should display data."""
|
2010-10-18 21:34:47 +08:00
|
|
|
month = self.month
|
|
|
|
if month is None:
|
|
|
|
try:
|
|
|
|
month = self.kwargs['month']
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
month = self.request.GET['month']
|
|
|
|
except KeyError:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise Http404(_("No month specified"))
|
2010-10-18 21:34:47 +08:00
|
|
|
return month
|
|
|
|
|
|
|
|
def get_next_month(self, date):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Get the next valid month."""
|
2012-05-17 23:41:37 +08:00
|
|
|
return _get_next_prev(self, date, is_previous=False, period='month')
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_previous_month(self, date):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Get the previous valid month."""
|
2012-05-17 23:41:37 +08:00
|
|
|
return _get_next_prev(self, date, is_previous=True, period='month')
|
|
|
|
|
|
|
|
def _get_next_month(self, date):
|
|
|
|
"""
|
|
|
|
Return the start date of the next interval.
|
|
|
|
|
|
|
|
The interval is defined by start date <= item date < next start date.
|
|
|
|
"""
|
|
|
|
if date.month == 12:
|
2017-05-16 02:56:30 +08:00
|
|
|
try:
|
|
|
|
return date.replace(year=date.year + 1, month=1, day=1)
|
|
|
|
except ValueError:
|
|
|
|
raise Http404(_("Date out of range"))
|
2012-05-17 23:41:37 +08:00
|
|
|
else:
|
|
|
|
return date.replace(month=date.month + 1, day=1)
|
|
|
|
|
|
|
|
def _get_current_month(self, date):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the start date of the previous interval."""
|
2012-05-17 23:41:37 +08:00
|
|
|
return date.replace(day=1)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class DayMixin:
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Mixin for views manipulating day-based data."""
|
2010-10-18 21:34:47 +08:00
|
|
|
day_format = '%d'
|
|
|
|
day = None
|
|
|
|
|
|
|
|
def get_day_format(self):
|
|
|
|
"""
|
2011-01-26 11:23:59 +08:00
|
|
|
Get a day format string in strptime syntax to be used to parse the day
|
|
|
|
from url variables.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
return self.day_format
|
|
|
|
|
|
|
|
def get_day(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the day for which this view should display data."""
|
2010-10-18 21:34:47 +08:00
|
|
|
day = self.day
|
|
|
|
if day is None:
|
|
|
|
try:
|
|
|
|
day = self.kwargs['day']
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
day = self.request.GET['day']
|
|
|
|
except KeyError:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise Http404(_("No day specified"))
|
2010-10-18 21:34:47 +08:00
|
|
|
return day
|
|
|
|
|
|
|
|
def get_next_day(self, date):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Get the next valid day."""
|
2012-05-17 23:41:37 +08:00
|
|
|
return _get_next_prev(self, date, is_previous=False, period='day')
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_previous_day(self, date):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Get the previous valid day."""
|
2012-05-17 23:41:37 +08:00
|
|
|
return _get_next_prev(self, date, is_previous=True, period='day')
|
|
|
|
|
|
|
|
def _get_next_day(self, date):
|
|
|
|
"""
|
|
|
|
Return the start date of the next interval.
|
|
|
|
|
|
|
|
The interval is defined by start date <= item date < next start date.
|
|
|
|
"""
|
|
|
|
return date + datetime.timedelta(days=1)
|
|
|
|
|
|
|
|
def _get_current_day(self, date):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the start date of the current interval."""
|
2012-05-17 23:41:37 +08:00
|
|
|
return date
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class WeekMixin:
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Mixin for views manipulating week-based data."""
|
2010-10-18 21:34:47 +08:00
|
|
|
week_format = '%U'
|
|
|
|
week = None
|
|
|
|
|
|
|
|
def get_week_format(self):
|
|
|
|
"""
|
|
|
|
Get a week format string in strptime syntax to be used to parse the
|
|
|
|
week from url variables.
|
|
|
|
"""
|
|
|
|
return self.week_format
|
|
|
|
|
|
|
|
def get_week(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the week for which this view should display data."""
|
2010-10-18 21:34:47 +08:00
|
|
|
week = self.week
|
|
|
|
if week is None:
|
|
|
|
try:
|
|
|
|
week = self.kwargs['week']
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
week = self.request.GET['week']
|
|
|
|
except KeyError:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise Http404(_("No week specified"))
|
2010-10-18 21:34:47 +08:00
|
|
|
return week
|
|
|
|
|
2012-05-15 04:40:45 +08:00
|
|
|
def get_next_week(self, date):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Get the next valid week."""
|
2012-05-17 23:41:37 +08:00
|
|
|
return _get_next_prev(self, date, is_previous=False, period='week')
|
2012-05-15 04:40:45 +08:00
|
|
|
|
|
|
|
def get_previous_week(self, date):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Get the previous valid week."""
|
2012-05-17 23:41:37 +08:00
|
|
|
return _get_next_prev(self, date, is_previous=True, period='week')
|
|
|
|
|
|
|
|
def _get_next_week(self, date):
|
|
|
|
"""
|
|
|
|
Return the start date of the next interval.
|
|
|
|
|
|
|
|
The interval is defined by start date <= item date < next start date.
|
|
|
|
"""
|
2017-05-16 02:56:30 +08:00
|
|
|
try:
|
|
|
|
return date + datetime.timedelta(days=7 - self._get_weekday(date))
|
|
|
|
except OverflowError:
|
|
|
|
raise Http404(_("Date out of range"))
|
2012-05-17 23:41:37 +08:00
|
|
|
|
|
|
|
def _get_current_week(self, date):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the start date of the current interval."""
|
2012-05-17 23:41:37 +08:00
|
|
|
return date - datetime.timedelta(self._get_weekday(date))
|
2012-05-15 04:40:45 +08:00
|
|
|
|
|
|
|
def _get_weekday(self, date):
|
2012-05-17 23:41:37 +08:00
|
|
|
"""
|
|
|
|
Return the weekday for a given date.
|
|
|
|
|
|
|
|
The first day according to the week format is 0 and the last day is 6.
|
|
|
|
"""
|
2012-05-15 04:40:45 +08:00
|
|
|
week_format = self.get_week_format()
|
|
|
|
if week_format == '%W': # week starts on Monday
|
|
|
|
return date.weekday()
|
|
|
|
elif week_format == '%U': # week starts on Sunday
|
|
|
|
return (date.weekday() + 1) % 7
|
|
|
|
else:
|
|
|
|
raise ValueError("unknown week format: %s" % week_format)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2012-05-17 23:41:37 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class DateMixin:
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Mixin class for views manipulating date-based data."""
|
2010-10-18 21:34:47 +08:00
|
|
|
date_field = None
|
|
|
|
allow_future = False
|
|
|
|
|
|
|
|
def get_date_field(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Get the name of the date field to be used to filter by."""
|
2010-10-18 21:34:47 +08:00
|
|
|
if self.date_field is None:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise ImproperlyConfigured("%s.date_field is required." % self.__class__.__name__)
|
2010-10-18 21:34:47 +08:00
|
|
|
return self.date_field
|
|
|
|
|
|
|
|
def get_allow_future(self):
|
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Return `True` if the view should be allowed to display objects from
|
2010-10-18 21:34:47 +08:00
|
|
|
the future.
|
|
|
|
"""
|
|
|
|
return self.allow_future
|
|
|
|
|
2012-05-01 02:41:38 +08:00
|
|
|
# Note: the following three methods only work in subclasses that also
|
|
|
|
# inherit SingleObjectMixin or MultipleObjectMixin.
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def uses_datetime_field(self):
|
|
|
|
"""
|
|
|
|
Return `True` if the date field is a `DateTimeField` and `False`
|
|
|
|
if it's a `DateField`.
|
|
|
|
"""
|
|
|
|
model = self.get_queryset().model if self.model is None else self.model
|
|
|
|
field = model._meta.get_field(self.get_date_field())
|
|
|
|
return isinstance(field, models.DateTimeField)
|
|
|
|
|
|
|
|
def _make_date_lookup_arg(self, value):
|
|
|
|
"""
|
|
|
|
Convert a date into a datetime when the date field is a DateTimeField.
|
|
|
|
|
|
|
|
When time zone support is enabled, `date` is assumed to be in the
|
|
|
|
current time zone, so that displayed items are consistent with the URL.
|
|
|
|
"""
|
|
|
|
if self.uses_datetime_field:
|
|
|
|
value = datetime.datetime.combine(value, datetime.time.min)
|
|
|
|
if settings.USE_TZ:
|
2018-01-19 00:21:12 +08:00
|
|
|
value = timezone.make_aware(value)
|
2012-05-01 02:41:38 +08:00
|
|
|
return value
|
|
|
|
|
|
|
|
def _make_single_date_lookup(self, date):
|
|
|
|
"""
|
|
|
|
Get the lookup kwargs for filtering on a single date.
|
|
|
|
|
|
|
|
If the date field is a DateTimeField, we can't just filter on
|
|
|
|
date_field=date because that doesn't take the time into account.
|
|
|
|
"""
|
|
|
|
date_field = self.get_date_field()
|
|
|
|
if self.uses_datetime_field:
|
|
|
|
since = self._make_date_lookup_arg(date)
|
|
|
|
until = self._make_date_lookup_arg(date + datetime.timedelta(days=1))
|
|
|
|
return {
|
|
|
|
'%s__gte' % date_field: since,
|
|
|
|
'%s__lt' % date_field: until,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
# Skip self._make_date_lookup_arg, it's a no-op in this branch.
|
|
|
|
return {date_field: date}
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
class BaseDateListView(MultipleObjectMixin, DateMixin, View):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Abstract base class for date-based views displaying a list of objects."""
|
2010-10-18 21:34:47 +08:00
|
|
|
allow_empty = False
|
2012-08-18 23:28:17 +08:00
|
|
|
date_list_period = 'year'
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.date_list, self.object_list, extra_context = self.get_dated_items()
|
2017-08-18 05:55:58 +08:00
|
|
|
context = self.get_context_data(
|
|
|
|
object_list=self.object_list,
|
|
|
|
date_list=self.date_list,
|
|
|
|
**extra_context
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def get_dated_items(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Obtain the list of dates and items."""
|
2010-10-28 10:58:46 +08:00
|
|
|
raise NotImplementedError('A DateView must provide an implementation of get_dated_items()')
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2014-05-20 05:27:09 +08:00
|
|
|
def get_ordering(self):
|
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Return the field or fields to use for ordering the queryset; use the
|
2014-05-20 05:27:09 +08:00
|
|
|
date field by default.
|
|
|
|
"""
|
|
|
|
return '-%s' % self.get_date_field() if self.ordering is None else self.ordering
|
|
|
|
|
|
|
|
def get_dated_queryset(self, **lookup):
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
Get a queryset properly filtered according to `allow_future` and any
|
|
|
|
extra lookup kwargs.
|
|
|
|
"""
|
|
|
|
qs = self.get_queryset().filter(**lookup)
|
|
|
|
date_field = self.get_date_field()
|
|
|
|
allow_future = self.get_allow_future()
|
|
|
|
allow_empty = self.get_allow_empty()
|
2012-04-11 04:05:46 +08:00
|
|
|
paginate_by = self.get_paginate_by(qs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
if not allow_future:
|
2012-05-17 23:41:37 +08:00
|
|
|
now = timezone.now() if self.uses_datetime_field else timezone_today()
|
2012-05-01 02:41:38 +08:00
|
|
|
qs = qs.filter(**{'%s__lte' % date_field: now})
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2012-04-11 04:05:46 +08:00
|
|
|
if not allow_empty:
|
|
|
|
# When pagination is enabled, it's better to do a cheap query
|
|
|
|
# than to load the unpaginated queryset in memory.
|
2017-11-30 00:54:34 +08:00
|
|
|
is_empty = not qs if paginate_by is None else not qs.exists()
|
2012-04-11 04:05:46 +08:00
|
|
|
if is_empty:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise Http404(_("No %(verbose_name_plural)s available") % {
|
2017-03-04 22:47:49 +08:00
|
|
|
'verbose_name_plural': qs.model._meta.verbose_name_plural,
|
2012-04-11 04:05:46 +08:00
|
|
|
})
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
return qs
|
|
|
|
|
2012-08-18 23:28:17 +08:00
|
|
|
def get_date_list_period(self):
|
2012-09-09 00:20:20 +08:00
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Get the aggregation period for the list of dates: 'year', 'month', or
|
|
|
|
'day'.
|
2012-09-09 00:20:20 +08:00
|
|
|
"""
|
2012-08-18 23:28:17 +08:00
|
|
|
return self.date_list_period
|
|
|
|
|
2012-09-22 17:45:51 +08:00
|
|
|
def get_date_list(self, queryset, date_type=None, ordering='ASC'):
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2013-02-10 23:15:49 +08:00
|
|
|
Get a date list by calling `queryset.dates/datetimes()`, checking
|
|
|
|
along the way for empty lists that aren't allowed.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
date_field = self.get_date_field()
|
|
|
|
allow_empty = self.get_allow_empty()
|
2012-08-18 23:28:17 +08:00
|
|
|
if date_type is None:
|
|
|
|
date_type = self.get_date_list_period()
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
if self.uses_datetime_field:
|
|
|
|
date_list = queryset.datetimes(date_field, date_type, ordering)
|
|
|
|
else:
|
|
|
|
date_list = queryset.dates(date_field, date_type, ordering)
|
2010-10-18 21:34:47 +08:00
|
|
|
if date_list is not None and not date_list and not allow_empty:
|
2017-03-04 22:47:49 +08:00
|
|
|
raise Http404(
|
|
|
|
_("No %(verbose_name_plural)s available") % {
|
|
|
|
'verbose_name_plural': queryset.model._meta.verbose_name_plural,
|
|
|
|
}
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
return date_list
|
|
|
|
|
2012-05-01 02:41:38 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
class BaseArchiveIndexView(BaseDateListView):
|
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Base class for archives of date-based items. Requires a response mixin.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
context_object_name = 'latest'
|
|
|
|
|
|
|
|
def get_dated_items(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return (date_list, items, extra_context) for this request."""
|
2014-05-20 05:27:09 +08:00
|
|
|
qs = self.get_dated_queryset()
|
2012-09-22 17:45:51 +08:00
|
|
|
date_list = self.get_date_list(qs, ordering='DESC')
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2012-05-20 19:18:42 +08:00
|
|
|
if not date_list:
|
|
|
|
qs = qs.none()
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2012-05-20 19:18:42 +08:00
|
|
|
return (date_list, qs, {})
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ArchiveIndexView(MultipleObjectTemplateResponseMixin, BaseArchiveIndexView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Top-level archive of date-based items."""
|
2010-10-18 21:34:47 +08:00
|
|
|
template_name_suffix = '_archive'
|
|
|
|
|
|
|
|
|
|
|
|
class BaseYearArchiveView(YearMixin, BaseDateListView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""List of objects published in a given year."""
|
2012-08-18 23:28:17 +08:00
|
|
|
date_list_period = 'month'
|
2010-10-18 21:34:47 +08:00
|
|
|
make_object_list = False
|
|
|
|
|
|
|
|
def get_dated_items(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return (date_list, items, extra_context) for this request."""
|
2010-10-18 21:34:47 +08:00
|
|
|
year = self.get_year()
|
2012-05-01 02:41:38 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
date_field = self.get_date_field()
|
2012-05-01 02:41:38 +08:00
|
|
|
date = _date_from_string(year, self.get_year_format())
|
|
|
|
|
|
|
|
since = self._make_date_lookup_arg(date)
|
2012-05-17 23:41:37 +08:00
|
|
|
until = self._make_date_lookup_arg(self._get_next_year(date))
|
2012-05-01 02:41:38 +08:00
|
|
|
lookup_kwargs = {
|
|
|
|
'%s__gte' % date_field: since,
|
|
|
|
'%s__lt' % date_field: until,
|
|
|
|
}
|
|
|
|
|
2014-05-20 05:27:09 +08:00
|
|
|
qs = self.get_dated_queryset(**lookup_kwargs)
|
2012-08-18 23:28:17 +08:00
|
|
|
date_list = self.get_date_list(qs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2012-05-20 19:18:42 +08:00
|
|
|
if not self.get_make_object_list():
|
2010-10-18 21:34:47 +08:00
|
|
|
# We need this to be a queryset since parent classes introspect it
|
|
|
|
# to find information about the model.
|
2012-05-20 19:18:42 +08:00
|
|
|
qs = qs.none()
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2012-05-24 19:02:19 +08:00
|
|
|
return (date_list, qs, {
|
|
|
|
'year': date,
|
|
|
|
'next_year': self.get_next_year(date),
|
|
|
|
'previous_year': self.get_previous_year(date),
|
|
|
|
})
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_make_object_list(self):
|
|
|
|
"""
|
|
|
|
Return `True` if this view should contain the full list of objects in
|
|
|
|
the given year.
|
|
|
|
"""
|
|
|
|
return self.make_object_list
|
|
|
|
|
|
|
|
|
|
|
|
class YearArchiveView(MultipleObjectTemplateResponseMixin, BaseYearArchiveView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""List of objects published in a given year."""
|
2010-10-18 21:34:47 +08:00
|
|
|
template_name_suffix = '_archive_year'
|
|
|
|
|
|
|
|
|
|
|
|
class BaseMonthArchiveView(YearMixin, MonthMixin, BaseDateListView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""List of objects published in a given month."""
|
2012-08-18 23:28:17 +08:00
|
|
|
date_list_period = 'day'
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def get_dated_items(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return (date_list, items, extra_context) for this request."""
|
2010-10-18 21:34:47 +08:00
|
|
|
year = self.get_year()
|
|
|
|
month = self.get_month()
|
|
|
|
|
|
|
|
date_field = self.get_date_field()
|
|
|
|
date = _date_from_string(year, self.get_year_format(),
|
|
|
|
month, self.get_month_format())
|
|
|
|
|
2012-05-01 02:41:38 +08:00
|
|
|
since = self._make_date_lookup_arg(date)
|
2012-05-17 23:41:37 +08:00
|
|
|
until = self._make_date_lookup_arg(self._get_next_month(date))
|
2010-10-18 21:34:47 +08:00
|
|
|
lookup_kwargs = {
|
2012-05-01 02:41:38 +08:00
|
|
|
'%s__gte' % date_field: since,
|
|
|
|
'%s__lt' % date_field: until,
|
2010-10-18 21:34:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
qs = self.get_dated_queryset(**lookup_kwargs)
|
2012-08-18 23:28:17 +08:00
|
|
|
date_list = self.get_date_list(qs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
return (date_list, qs, {
|
|
|
|
'month': date,
|
|
|
|
'next_month': self.get_next_month(date),
|
|
|
|
'previous_month': self.get_previous_month(date),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
class MonthArchiveView(MultipleObjectTemplateResponseMixin, BaseMonthArchiveView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""List of objects published in a given month."""
|
2010-10-18 21:34:47 +08:00
|
|
|
template_name_suffix = '_archive_month'
|
|
|
|
|
|
|
|
|
|
|
|
class BaseWeekArchiveView(YearMixin, WeekMixin, BaseDateListView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""List of objects published in a given week."""
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_dated_items(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return (date_list, items, extra_context) for this request."""
|
2010-10-18 21:34:47 +08:00
|
|
|
year = self.get_year()
|
|
|
|
week = self.get_week()
|
|
|
|
|
|
|
|
date_field = self.get_date_field()
|
2010-11-23 09:46:48 +08:00
|
|
|
week_format = self.get_week_format()
|
2018-10-29 22:56:04 +08:00
|
|
|
week_choices = {'%W': '1', '%U': '0'}
|
|
|
|
try:
|
|
|
|
week_start = week_choices[week_format]
|
|
|
|
except KeyError:
|
|
|
|
raise ValueError('Unknown week format %r. Choices are: %s' % (
|
|
|
|
week_format,
|
|
|
|
', '.join(sorted(week_choices)),
|
|
|
|
))
|
2010-10-18 21:34:47 +08:00
|
|
|
date = _date_from_string(year, self.get_year_format(),
|
2010-11-23 09:46:48 +08:00
|
|
|
week_start, '%w',
|
|
|
|
week, week_format)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2012-05-01 02:41:38 +08:00
|
|
|
since = self._make_date_lookup_arg(date)
|
2012-05-17 23:41:37 +08:00
|
|
|
until = self._make_date_lookup_arg(self._get_next_week(date))
|
2010-10-18 21:34:47 +08:00
|
|
|
lookup_kwargs = {
|
2012-05-01 02:41:38 +08:00
|
|
|
'%s__gte' % date_field: since,
|
|
|
|
'%s__lt' % date_field: until,
|
2010-10-18 21:34:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
qs = self.get_dated_queryset(**lookup_kwargs)
|
|
|
|
|
2012-05-15 04:40:45 +08:00
|
|
|
return (None, qs, {
|
|
|
|
'week': date,
|
|
|
|
'next_week': self.get_next_week(date),
|
|
|
|
'previous_week': self.get_previous_week(date),
|
|
|
|
})
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class WeekArchiveView(MultipleObjectTemplateResponseMixin, BaseWeekArchiveView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""List of objects published in a given week."""
|
2010-10-18 21:34:47 +08:00
|
|
|
template_name_suffix = '_archive_week'
|
|
|
|
|
|
|
|
|
|
|
|
class BaseDayArchiveView(YearMixin, MonthMixin, DayMixin, BaseDateListView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""List of objects published on a given day."""
|
2010-10-18 21:34:47 +08:00
|
|
|
def get_dated_items(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return (date_list, items, extra_context) for this request."""
|
2010-10-18 21:34:47 +08:00
|
|
|
year = self.get_year()
|
|
|
|
month = self.get_month()
|
|
|
|
day = self.get_day()
|
|
|
|
|
|
|
|
date = _date_from_string(year, self.get_year_format(),
|
|
|
|
month, self.get_month_format(),
|
|
|
|
day, self.get_day_format())
|
|
|
|
|
|
|
|
return self._get_dated_items(date)
|
|
|
|
|
|
|
|
def _get_dated_items(self, date):
|
|
|
|
"""
|
|
|
|
Do the actual heavy lifting of getting the dated items; this accepts a
|
|
|
|
date object so that TodayArchiveView can be trivial.
|
|
|
|
"""
|
2012-05-01 02:41:38 +08:00
|
|
|
lookup_kwargs = self._make_single_date_lookup(date)
|
2010-10-18 21:34:47 +08:00
|
|
|
qs = self.get_dated_queryset(**lookup_kwargs)
|
|
|
|
|
|
|
|
return (None, qs, {
|
|
|
|
'day': date,
|
|
|
|
'previous_day': self.get_previous_day(date),
|
|
|
|
'next_day': self.get_next_day(date),
|
|
|
|
'previous_month': self.get_previous_month(date),
|
|
|
|
'next_month': self.get_next_month(date)
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
class DayArchiveView(MultipleObjectTemplateResponseMixin, BaseDayArchiveView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""List of objects published on a given day."""
|
2010-10-18 21:34:47 +08:00
|
|
|
template_name_suffix = "_archive_day"
|
|
|
|
|
|
|
|
|
|
|
|
class BaseTodayArchiveView(BaseDayArchiveView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""List of objects published today."""
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_dated_items(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return (date_list, items, extra_context) for this request."""
|
2010-10-18 21:34:47 +08:00
|
|
|
return self._get_dated_items(datetime.date.today())
|
|
|
|
|
|
|
|
|
|
|
|
class TodayArchiveView(MultipleObjectTemplateResponseMixin, BaseTodayArchiveView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""List of objects published today."""
|
2010-10-18 21:34:47 +08:00
|
|
|
template_name_suffix = "_archive_day"
|
|
|
|
|
|
|
|
|
|
|
|
class BaseDateDetailView(YearMixin, MonthMixin, DayMixin, DateMixin, BaseDetailView):
|
|
|
|
"""
|
|
|
|
Detail view of a single object on a single date; this differs from the
|
|
|
|
standard DetailView by accepting a year/month/day in the URL.
|
|
|
|
"""
|
2010-10-20 08:21:47 +08:00
|
|
|
def get_object(self, queryset=None):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Get the object this request displays."""
|
2010-10-18 21:34:47 +08:00
|
|
|
year = self.get_year()
|
|
|
|
month = self.get_month()
|
|
|
|
day = self.get_day()
|
|
|
|
date = _date_from_string(year, self.get_year_format(),
|
|
|
|
month, self.get_month_format(),
|
|
|
|
day, self.get_day_format())
|
|
|
|
|
2011-10-13 21:38:38 +08:00
|
|
|
# Use a custom queryset if provided
|
2015-02-03 21:46:17 +08:00
|
|
|
qs = self.get_queryset() if queryset is None else queryset
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
if not self.get_allow_future() and date > datetime.date.today():
|
2014-09-04 20:15:09 +08:00
|
|
|
raise Http404(_(
|
|
|
|
"Future %(verbose_name_plural)s not available because "
|
2016-02-06 02:53:03 +08:00
|
|
|
"%(class_name)s.allow_future is False."
|
|
|
|
) % {
|
2014-11-23 05:21:32 +08:00
|
|
|
'verbose_name_plural': qs.model._meta.verbose_name_plural,
|
|
|
|
'class_name': self.__class__.__name__,
|
2016-02-06 02:53:03 +08:00
|
|
|
})
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
# Filter down a queryset from self.queryset using the date from the
|
|
|
|
# URL. This'll get passed as the queryset to DetailView.get_object,
|
|
|
|
# which'll handle the 404
|
2012-05-01 02:41:38 +08:00
|
|
|
lookup_kwargs = self._make_single_date_lookup(date)
|
|
|
|
qs = qs.filter(**lookup_kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_object(queryset=qs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DateDetailView(SingleObjectTemplateResponseMixin, BaseDateDetailView):
|
|
|
|
"""
|
|
|
|
Detail view of a single object on a single date; this differs from the
|
|
|
|
standard DetailView by accepting a year/month/day in the URL.
|
|
|
|
"""
|
|
|
|
template_name_suffix = '_detail'
|
|
|
|
|
|
|
|
|
2012-05-01 02:41:38 +08:00
|
|
|
def _date_from_string(year, year_format, month='', month_format='', day='', day_format='', delim='__'):
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Get a datetime.date object given a format string and a year, month, and day
|
|
|
|
(only year is mandatory). Raise a 404 for an invalid date.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2017-05-21 05:16:36 +08:00
|
|
|
format = year_format + delim + month_format + delim + day_format
|
2016-10-21 01:29:04 +08:00
|
|
|
datestr = str(year) + delim + str(month) + delim + str(day)
|
2010-10-18 21:34:47 +08:00
|
|
|
try:
|
2017-01-12 06:17:25 +08:00
|
|
|
return datetime.datetime.strptime(datestr, format).date()
|
2010-10-18 21:34:47 +08:00
|
|
|
except ValueError:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise Http404(_("Invalid date string '%(datestr)s' given format '%(format)s'") % {
|
2011-02-14 21:05:31 +08:00
|
|
|
'datestr': datestr,
|
|
|
|
'format': format,
|
|
|
|
})
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2011-01-27 11:14:20 +08:00
|
|
|
|
2012-05-17 23:41:37 +08:00
|
|
|
def _get_next_prev(generic_view, date, is_previous, period):
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Get the next or the previous valid date. The idea is to allow links on
|
|
|
|
month/day views to never be 404s by never providing a date that'll be
|
|
|
|
invalid for the given view.
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2012-05-17 23:41:37 +08:00
|
|
|
This is a bit complicated since it handles different intervals of time,
|
|
|
|
hence the coupling to generic_view.
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2011-02-06 14:31:52 +08:00
|
|
|
However in essence the logic comes down to:
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
* If allow_empty and allow_future are both true, this is easy: just
|
2012-05-17 23:41:37 +08:00
|
|
|
return the naive result (just the next/previous day/week/month,
|
2014-04-27 01:18:45 +08:00
|
|
|
regardless of object existence.)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2012-05-17 23:41:37 +08:00
|
|
|
* If allow_empty is true, allow_future is false, and the naive result
|
2010-10-18 21:34:47 +08:00
|
|
|
isn't in the future, then return it; otherwise return None.
|
|
|
|
|
|
|
|
* If allow_empty is false and allow_future is true, return the next
|
|
|
|
date *that contains a valid object*, even if it's in the future. If
|
|
|
|
there are no next objects, return None.
|
|
|
|
|
|
|
|
* If allow_empty is false and allow_future is false, return the next
|
|
|
|
date that contains a valid object. If that date is in the future, or
|
|
|
|
if there are no next objects, return None.
|
|
|
|
"""
|
|
|
|
date_field = generic_view.get_date_field()
|
|
|
|
allow_empty = generic_view.get_allow_empty()
|
|
|
|
allow_future = generic_view.get_allow_future()
|
|
|
|
|
2012-05-17 23:41:37 +08:00
|
|
|
get_current = getattr(generic_view, '_get_current_%s' % period)
|
|
|
|
get_next = getattr(generic_view, '_get_next_%s' % period)
|
|
|
|
|
|
|
|
# Bounds of the current interval
|
|
|
|
start, end = get_current(date), get_next(date)
|
|
|
|
|
|
|
|
# If allow_empty is True, the naive result will be valid
|
2010-10-18 21:34:47 +08:00
|
|
|
if allow_empty:
|
2012-05-17 23:41:37 +08:00
|
|
|
if is_previous:
|
|
|
|
result = get_current(start - datetime.timedelta(days=1))
|
|
|
|
else:
|
|
|
|
result = end
|
|
|
|
|
|
|
|
if allow_future or result <= timezone_today():
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
return None
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
# Otherwise, we'll need to go to the database to look for an object
|
|
|
|
# whose date_field is at least (greater than/less than) the given
|
|
|
|
# naive result
|
|
|
|
else:
|
2011-02-06 14:31:52 +08:00
|
|
|
# Construct a lookup and an ordering depending on whether we're doing
|
2010-10-18 21:34:47 +08:00
|
|
|
# a previous date or a next date lookup.
|
|
|
|
if is_previous:
|
2012-05-17 23:41:37 +08:00
|
|
|
lookup = {'%s__lt' % date_field: generic_view._make_date_lookup_arg(start)}
|
2010-10-18 21:34:47 +08:00
|
|
|
ordering = '-%s' % date_field
|
|
|
|
else:
|
2012-05-17 23:41:37 +08:00
|
|
|
lookup = {'%s__gte' % date_field: generic_view._make_date_lookup_arg(end)}
|
2010-10-18 21:34:47 +08:00
|
|
|
ordering = date_field
|
|
|
|
|
2012-05-17 23:41:37 +08:00
|
|
|
# Filter out objects in the future if appropriate.
|
|
|
|
if not allow_future:
|
|
|
|
# Fortunately, to match the implementation of allow_future,
|
|
|
|
# we need __lte, which doesn't conflict with __lt above.
|
|
|
|
if generic_view.uses_datetime_field:
|
|
|
|
now = timezone.now()
|
|
|
|
else:
|
|
|
|
now = timezone_today()
|
|
|
|
lookup['%s__lte' % date_field] = now
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
qs = generic_view.get_queryset().filter(**lookup).order_by(ordering)
|
|
|
|
|
|
|
|
# Snag the first object from the queryset; if it doesn't exist that
|
|
|
|
# means there's no next/previous link available.
|
|
|
|
try:
|
|
|
|
result = getattr(qs[0], date_field)
|
|
|
|
except IndexError:
|
2012-05-17 23:41:37 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
# Convert datetimes to dates in the current time zone.
|
|
|
|
if generic_view.uses_datetime_field:
|
|
|
|
if settings.USE_TZ:
|
|
|
|
result = timezone.localtime(result)
|
|
|
|
result = result.date()
|
|
|
|
|
|
|
|
# Return the first day of the period.
|
|
|
|
return get_current(result)
|
|
|
|
|
|
|
|
|
|
|
|
def timezone_today():
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the current date in the current time zone."""
|
2012-05-17 23:41:37 +08:00
|
|
|
if settings.USE_TZ:
|
2016-09-01 08:19:33 +08:00
|
|
|
return timezone.localdate()
|
2010-10-18 21:34:47 +08:00
|
|
|
else:
|
2012-05-17 23:41:37 +08:00
|
|
|
return datetime.date.today()
|