Code

Ticket #2386: date_based.py.2.diff

File date_based.py.2.diff, 3.3 KB (added by Pete Crosier, 8 years ago)

Works a bit more logically.

Line 
1Index: django/views/generic/date_based.py
2===================================================================
3--- django/views/generic/date_based.py  (revision 3415)
4+++ django/views/generic/date_based.py  (working copy)
5@@ -133,14 +133,34 @@
6     object_list = queryset.filter(**lookup_kwargs)
7     if not object_list and not allow_empty:
8         raise Http404
9+
10+    # Get the closest previous post's date, if it exists.
11+    lookup_kwargs = {'%s__lte' % date_field: date.replace(day=1) - datetime.timedelta(days=1)}
12+    previous_list = queryset.filter(**lookup_kwargs).order_by('-%s' % date_field)
13+    if previous_list:
14+        previous_month = getattr(previous_list[0], date_field).date()
15+    else:
16+        previous_month = None
17+
18+    # Get the closest next post's date, if it exists.
19+    if date.month == 12:
20+        lookup_kwargs = {'%s__gte' % date_field: date.replace(day=1, month=1, year=date.year + 1)}
21+    else:
22+        lookup_kwargs = {'%s__gte' % date_field: date.replace(day=1, month=date.month + 1)}
23+    next_list = queryset.filter(**lookup_kwargs).order_by(date_field)
24+    if next_list:
25+        next_month = getattr(next_list[0], date_field).date()
26+    else:
27+        next_month = None
28+
29     if not template_name:
30         template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
31     t = template_loader.get_template(template_name)
32     c = RequestContext(request, {
33         '%s_list' % template_object_name: object_list,
34         'month': date,
35-        'next_month': (last_day < datetime.date.today()) and (last_day + datetime.timedelta(days=1)) or None,
36-        'previous_month': first_day - datetime.timedelta(days=1),
37+        'next_month': next_month,
38+        'previous_month': previous_month,
39     }, context_processors)
40     for key, value in extra_context.items():
41         if callable(value):
42@@ -235,14 +255,31 @@
43     object_list = queryset.filter(**lookup_kwargs)
44     if not allow_empty and not object_list:
45         raise Http404
46+
47+    # Get the closest previous post's date, if it exists.
48+    lookup_kwargs = {'%s__lte' % date_field: date - datetime.timedelta(days=1)}
49+    previous_list = queryset.filter(**lookup_kwargs).order_by('-%s' % date_field)
50+    if previous_list:
51+        previous_day = getattr(previous_list[0], date_field).date()
52+    else:
53+        previous_day = None
54+
55+    # Get the closest next post's date, if it exists.
56+    lookup_kwargs = {'%s__gte' % date_field: date + datetime.timedelta(days=1)}
57+    next_list = queryset.filter(**lookup_kwargs).order_by(date_field)
58+    if next_list:
59+        next_day = getattr(next_list[0], date_field).date()
60+    else:
61+        next_day = None
62+
63     if not template_name:
64         template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower())
65     t = template_loader.get_template(template_name)
66     c = RequestContext(request, {
67         '%s_list' % template_object_name: object_list,
68         'day': date,
69-        'previous_day': date - datetime.timedelta(days=1),
70-        'next_day': (date < datetime.date.today()) and (date + datetime.timedelta(days=1)) or None,
71+        'previous_day': previous_day,
72+        'next_day': next_day,
73     }, context_processors)
74     for key, value in extra_context.items():
75         if callable(value):