diff --git a/django/views/generic/dates.py b/django/views/generic/dates.py
index 6964624..5185d7f 100644
a
|
b
|
class YearMixin(object):
|
23 | 23 | return self.year_format |
24 | 24 | |
25 | 25 | def get_year(self): |
26 | | "Return the year for which this view should display data" |
| 26 | """ |
| 27 | Return the year for which this view should display data. |
| 28 | """ |
27 | 29 | year = self.year |
28 | 30 | if year is None: |
29 | 31 | try: |
… |
… |
class YearMixin(object):
|
35 | 37 | raise Http404(_(u"No year specified")) |
36 | 38 | return year |
37 | 39 | |
| 40 | def _get_next_year(self, date): |
| 41 | """ |
| 42 | Return the start date of the next interval. |
| 43 | |
| 44 | The interval is defined by start date <= item date < next start date. |
| 45 | """ |
| 46 | return date.replace(year=date.year + 1, month=1, day=1) |
| 47 | |
| 48 | def _get_current_year(self, date): |
| 49 | """ |
| 50 | Return the start date of the current interval. |
| 51 | """ |
| 52 | return date.replace(month=1, day=1) |
| 53 | |
38 | 54 | |
39 | 55 | class MonthMixin(object): |
40 | 56 | month_format = '%b' |
… |
… |
class MonthMixin(object):
|
48 | 64 | return self.month_format |
49 | 65 | |
50 | 66 | def get_month(self): |
51 | | "Return the month for which this view should display data" |
| 67 | """ |
| 68 | Return the month for which this view should display data. |
| 69 | """ |
52 | 70 | month = self.month |
53 | 71 | if month is None: |
54 | 72 | try: |
… |
… |
class MonthMixin(object):
|
64 | 82 | """ |
65 | 83 | Get the next valid month. |
66 | 84 | """ |
67 | | # next must be the first day of the next month. |
68 | | if date.month == 12: |
69 | | next = date.replace(year=date.year + 1, month=1, day=1) |
70 | | else: |
71 | | next = date.replace(month=date.month + 1, day=1) |
72 | | return _get_next_prev(self, next, is_previous=False, period='month') |
| 85 | return _get_next_prev(self, date, is_previous=False, period='month') |
73 | 86 | |
74 | 87 | def get_previous_month(self, date): |
75 | 88 | """ |
76 | 89 | Get the previous valid month. |
77 | 90 | """ |
78 | | # prev must be the last day of the previous month. |
79 | | prev = date.replace(day=1) - datetime.timedelta(days=1) |
80 | | return _get_next_prev(self, prev, is_previous=True, period='month') |
| 91 | return _get_next_prev(self, date, is_previous=True, period='month') |
| 92 | |
| 93 | def _get_next_month(self, date): |
| 94 | """ |
| 95 | Return the start date of the next interval. |
| 96 | |
| 97 | The interval is defined by start date <= item date < next start date. |
| 98 | """ |
| 99 | if date.month == 12: |
| 100 | return date.replace(year=date.year + 1, month=1, day=1) |
| 101 | else: |
| 102 | return date.replace(month=date.month + 1, day=1) |
| 103 | |
| 104 | def _get_current_month(self, date): |
| 105 | """ |
| 106 | Return the start date of the previous interval. |
| 107 | """ |
| 108 | return date.replace(day=1) |
81 | 109 | |
82 | 110 | |
83 | 111 | class DayMixin(object): |
… |
… |
class DayMixin(object):
|
92 | 120 | return self.day_format |
93 | 121 | |
94 | 122 | def get_day(self): |
95 | | "Return the day for which this view should display data" |
| 123 | """ |
| 124 | Return the day for which this view should display data. |
| 125 | """ |
96 | 126 | day = self.day |
97 | 127 | if day is None: |
98 | 128 | try: |
… |
… |
class DayMixin(object):
|
108 | 138 | """ |
109 | 139 | Get the next valid day. |
110 | 140 | """ |
111 | | next = date + datetime.timedelta(days=1) |
112 | | return _get_next_prev(self, next, is_previous=False, period='day') |
| 141 | return _get_next_prev(self, date, is_previous=False, period='day') |
113 | 142 | |
114 | 143 | def get_previous_day(self, date): |
115 | 144 | """ |
116 | 145 | Get the previous valid day. |
117 | 146 | """ |
118 | | prev = date - datetime.timedelta(days=1) |
119 | | return _get_next_prev(self, prev, is_previous=True, period='day') |
| 147 | return _get_next_prev(self, date, is_previous=True, period='day') |
| 148 | |
| 149 | def _get_next_day(self, date): |
| 150 | """ |
| 151 | Return the start date of the next interval. |
| 152 | |
| 153 | The interval is defined by start date <= item date < next start date. |
| 154 | """ |
| 155 | return date + datetime.timedelta(days=1) |
| 156 | |
| 157 | def _get_current_day(self, date): |
| 158 | """ |
| 159 | Return the start date of the current interval. |
| 160 | """ |
| 161 | return date |
120 | 162 | |
121 | 163 | |
122 | 164 | class WeekMixin(object): |
… |
… |
class WeekMixin(object):
|
131 | 173 | return self.week_format |
132 | 174 | |
133 | 175 | def get_week(self): |
134 | | "Return the week for which this view should display data" |
| 176 | """ |
| 177 | Return the week for which this view should display data |
| 178 | """ |
135 | 179 | week = self.week |
136 | 180 | if week is None: |
137 | 181 | try: |
… |
… |
class WeekMixin(object):
|
147 | 191 | """ |
148 | 192 | Get the next valid week. |
149 | 193 | """ |
150 | | # next must be the first day of the next week. |
151 | | next = date + datetime.timedelta(days=7 - self._get_weekday(date)) |
152 | | return _get_next_prev(self, next, is_previous=False, period='week') |
| 194 | return _get_next_prev(self, date, is_previous=False, period='week') |
153 | 195 | |
154 | 196 | def get_previous_week(self, date): |
155 | 197 | """ |
156 | 198 | Get the previous valid week. |
157 | 199 | """ |
158 | | # prev must be the last day of the previous week. |
159 | | prev = date - datetime.timedelta(days=self._get_weekday(date) + 1) |
160 | | return _get_next_prev(self, prev, is_previous=True, period='week') |
| 200 | return _get_next_prev(self, date, is_previous=True, period='week') |
| 201 | |
| 202 | def _get_next_week(self, date): |
| 203 | """ |
| 204 | Return the start date of the next interval. |
| 205 | |
| 206 | The interval is defined by start date <= item date < next start date. |
| 207 | """ |
| 208 | return date + datetime.timedelta(days=7 - self._get_weekday(date)) |
| 209 | |
| 210 | def _get_current_week(self, date): |
| 211 | """ |
| 212 | Return the start date of the current interval. |
| 213 | """ |
| 214 | return date - datetime.timedelta(self._get_weekday(date)) |
161 | 215 | |
162 | 216 | def _get_weekday(self, date): |
163 | 217 | week_format = self.get_week_format() |
… |
… |
class WeekMixin(object):
|
168 | 222 | else: |
169 | 223 | raise ValueError("unknown week format: %s" % week_format) |
170 | 224 | |
| 225 | |
171 | 226 | class DateMixin(object): |
172 | 227 | """ |
173 | 228 | Mixin class for views manipulating date-based data. |
… |
… |
class BaseDateListView(MultipleObjectMixin, DateMixin, View):
|
267 | 322 | paginate_by = self.get_paginate_by(qs) |
268 | 323 | |
269 | 324 | if not allow_future: |
270 | | now = timezone.now() if self.uses_datetime_field else datetime.date.today() |
| 325 | now = timezone.now() if self.uses_datetime_field else timezone_today() |
271 | 326 | qs = qs.filter(**{'%s__lte' % date_field: now}) |
272 | 327 | |
273 | 328 | if not allow_empty: |
… |
… |
class BaseYearArchiveView(YearMixin, BaseDateListView):
|
344 | 399 | date = _date_from_string(year, self.get_year_format()) |
345 | 400 | |
346 | 401 | since = self._make_date_lookup_arg(date) |
347 | | until = self._make_date_lookup_arg(datetime.date(date.year + 1, 1, 1)) |
| 402 | until = self._make_date_lookup_arg(self._get_next_year(date)) |
348 | 403 | lookup_kwargs = { |
349 | 404 | '%s__gte' % date_field: since, |
350 | 405 | '%s__lt' % date_field: until, |
… |
… |
class BaseMonthArchiveView(YearMixin, MonthMixin, BaseDateListView):
|
392 | 447 | date = _date_from_string(year, self.get_year_format(), |
393 | 448 | month, self.get_month_format()) |
394 | 449 | |
395 | | # Construct a date-range lookup. |
396 | 450 | since = self._make_date_lookup_arg(date) |
397 | | if date.month == 12: |
398 | | until = self._make_date_lookup_arg(datetime.date(date.year + 1, 1, 1)) |
399 | | else: |
400 | | until = self._make_date_lookup_arg(datetime.date(date.year, date.month + 1, 1)) |
| 451 | until = self._make_date_lookup_arg(self._get_next_month(date)) |
401 | 452 | lookup_kwargs = { |
402 | 453 | '%s__gte' % date_field: since, |
403 | 454 | '%s__lt' % date_field: until, |
… |
… |
class BaseWeekArchiveView(YearMixin, WeekMixin, BaseDateListView):
|
442 | 493 | week_start, '%w', |
443 | 494 | week, week_format) |
444 | 495 | |
445 | | # Construct a date-range lookup. |
446 | 496 | since = self._make_date_lookup_arg(date) |
447 | | until = self._make_date_lookup_arg(date + datetime.timedelta(days=7)) |
| 497 | until = self._make_date_lookup_arg(self._get_next_week(date)) |
448 | 498 | lookup_kwargs = { |
449 | 499 | '%s__gte' % date_field: since, |
450 | 500 | '%s__lt' % date_field: until, |
… |
… |
def _date_from_string(year, year_format, month='', month_format='', day='', day_
|
585 | 635 | }) |
586 | 636 | |
587 | 637 | |
588 | | def _get_next_prev(generic_view, naive_result, is_previous, period): |
| 638 | def _get_next_prev(generic_view, date, is_previous, period): |
589 | 639 | """ |
590 | 640 | Helper: Get the next or the previous valid date. The idea is to allow |
591 | 641 | links on month/day views to never be 404s by never providing a date |
592 | 642 | that'll be invalid for the given view. |
593 | 643 | |
594 | | This is a bit complicated since it handles both next and previous months |
595 | | and days (for MonthArchiveView and DayArchiveView); hence the coupling to generic_view. |
| 644 | This is a bit complicated since it handles different intervals of time, |
| 645 | hence the coupling to generic_view. |
596 | 646 | |
597 | 647 | However in essence the logic comes down to: |
598 | 648 | |
599 | 649 | * If allow_empty and allow_future are both true, this is easy: just |
600 | | return the naive result (just the next/previous day or month, |
| 650 | return the naive result (just the next/previous day/week/month, |
601 | 651 | reguardless of object existence.) |
602 | 652 | |
603 | | * If allow_empty is true, allow_future is false, and the naive month |
| 653 | * If allow_empty is true, allow_future is false, and the naive result |
604 | 654 | isn't in the future, then return it; otherwise return None. |
605 | 655 | |
606 | 656 | * If allow_empty is false and allow_future is true, return the next |
… |
… |
def _get_next_prev(generic_view, naive_result, is_previous, period):
|
616 | 666 | allow_empty = generic_view.get_allow_empty() |
617 | 667 | allow_future = generic_view.get_allow_future() |
618 | 668 | |
619 | | # If allow_empty is True the naive value will be valid |
| 669 | get_current = getattr(generic_view, '_get_current_%s' % period) |
| 670 | get_next = getattr(generic_view, '_get_next_%s' % period) |
| 671 | |
| 672 | # Bounds of the current interval |
| 673 | start, end = get_current(date), get_next(date) |
| 674 | |
| 675 | # If allow_empty is True the naive result will be valid |
620 | 676 | if allow_empty: |
621 | | result = naive_result |
| 677 | if is_previous: |
| 678 | result = get_current(start - datetime.timedelta(days=1)) |
| 679 | else: |
| 680 | result = end |
622 | 681 | |
623 | 682 | # Otherwise, we'll need to go to the database to look for an object |
624 | 683 | # whose date_field is at least (greater than/less than) the given |
… |
… |
def _get_next_prev(generic_view, naive_result, is_previous, period):
|
627 | 686 | # Construct a lookup and an ordering depending on whether we're doing |
628 | 687 | # a previous date or a next date lookup. |
629 | 688 | if is_previous: |
630 | | lookup = {'%s__lte' % date_field: generic_view._make_date_lookup_arg(naive_result)} |
| 689 | lookup = {'%s__lt' % date_field: generic_view._make_date_lookup_arg(start)} |
631 | 690 | ordering = '-%s' % date_field |
632 | 691 | else: |
633 | | lookup = {'%s__gte' % date_field: generic_view._make_date_lookup_arg(naive_result)} |
| 692 | lookup = {'%s__gte' % date_field: generic_view._make_date_lookup_arg(end)} |
634 | 693 | ordering = date_field |
635 | 694 | |
636 | 695 | qs = generic_view.get_queryset().filter(**lookup).order_by(ordering) |
… |
… |
def _get_next_prev(generic_view, naive_result, is_previous, period):
|
640 | 699 | try: |
641 | 700 | result = getattr(qs[0], date_field) |
642 | 701 | except IndexError: |
643 | | result = None |
| 702 | return None |
644 | 703 | |
645 | | # Convert datetimes to a dates |
646 | | if result and generic_view.uses_datetime_field: |
| 704 | # Check against future dates. |
| 705 | now = timezone.now() if generic_view.uses_datetime_field else timezone_today() |
| 706 | if not allow_future and result > now: |
| 707 | return None |
| 708 | |
| 709 | # Convert datetimes to dates |
| 710 | if generic_view.uses_datetime_field: |
647 | 711 | if settings.USE_TZ: |
648 | 712 | result = timezone.localtime(result) |
649 | 713 | result = result.date() |
650 | 714 | |
651 | | if result: |
652 | | if period == 'month': |
653 | | # first day of the month |
654 | | result = result.replace(day=1) |
655 | | elif period == 'week': |
656 | | # monday of the week |
657 | | result = result - datetime.timedelta(days=generic_view._get_weekday(result)) |
658 | | elif period != 'day': |
659 | | raise ValueError('invalid period: %s' % period) |
| 715 | return get_current(result) |
660 | 716 | |
661 | | # Check against future dates. |
662 | | if result and (allow_future or result < datetime.date.today()): |
663 | | return result |
| 717 | |
| 718 | def timezone_today(): |
| 719 | """ |
| 720 | Return the current date in the current time zone. |
| 721 | """ |
| 722 | if settings.USE_TZ: |
| 723 | return timezone.localtime(timezone.now()).date() |
664 | 724 | else: |
665 | | return None |
| 725 | return datetime.date.today() |