Code

Ticket #2433: date_based.py.diff

File date_based.py.diff, 7.4 KB (added by Pete Crosier, 8 years ago)
Line 
1Index: django/views/generic/date_based.py
2===================================================================
3--- django/views/generic/date_based.py  (revision 3453)
4+++ django/views/generic/date_based.py  (working copy)
5@@ -7,7 +7,7 @@
6 def archive_index(request, queryset, date_field, num_latest=15,
7         template_name=None, template_loader=loader,
8         extra_context=None, allow_empty=False, context_processors=None,
9-        mimetype=None):
10+        mimetype=None, allow_future=False):
11     """
12     Generic top-level archive of date-based objects.
13 
14@@ -20,7 +20,8 @@
15     """
16     if extra_context is None: extra_context = {}
17     model = queryset.model
18-    queryset = queryset.filter(**{'%s__lte' % date_field: datetime.datetime.now()})
19+    if not allow_future:
20+        queryset = queryset.filter(**{'%s__lte' % date_field: datetime.datetime.now()})
21     date_list = queryset.dates(date_field, 'year')[::-1]
22     if not date_list and not allow_empty:
23         raise Http404, "No %s available" % model._meta.verbose_name
24@@ -47,7 +48,7 @@
25 def archive_year(request, year, queryset, date_field, template_name=None,
26         template_loader=loader, extra_context=None, allow_empty=False,
27         context_processors=None, template_object_name='object', mimetype=None,
28-        make_object_list=False):
29+        make_object_list=False, allow_future=False):
30     """
31     Generic yearly archive view.
32 
33@@ -67,8 +68,8 @@
34 
35     lookup_kwargs = {'%s__year' % date_field: year}
36 
37-    # Only bother to check current date if the year isn't in the past.
38-    if int(year) >= now.year:
39+    # Only bother to check current date if the year isn't in the past and future objects aren't requested.
40+    if int(year) >= now.year and not allow_future:
41         lookup_kwargs['%s__lte' % date_field] = now
42     date_list = queryset.filter(**lookup_kwargs).dates(date_field, 'month')
43     if not date_list and not allow_empty:
44@@ -95,7 +96,7 @@
45 def archive_month(request, year, month, queryset, date_field,
46         month_format='%b', template_name=None, template_loader=loader,
47         extra_context=None, allow_empty=False, context_processors=None,
48-        template_object_name='object', mimetype=None):
49+        template_object_name='object', mimetype=None, allow_future=False):
50     """
51     Generic monthly archive view.
52 
53@@ -127,19 +128,28 @@
54         last_day = first_day.replace(month=first_day.month + 1)
55     lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
56 
57-    # Only bother to check current date if the month isn't in the past.
58-    if last_day >= now.date():
59+    # Only bother to check current date if the month isn't in the past and future objects are requested.
60+    if last_day >= now.date() and not allow_future:
61         lookup_kwargs['%s__lte' % date_field] = now
62     object_list = queryset.filter(**lookup_kwargs)
63     if not object_list and not allow_empty:
64         raise Http404
65+
66+    # Calculate the next month, if applicable.
67+    if allow_future:
68+        next_month = last_day + datetime.timedelta(days=1)
69+    elif last_day < datetime.date.today():
70+        next_month = last_day + datetime.timedelta(days=1)
71+    else:
72+        next_month = None
73+
74     if not template_name:
75         template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
76     t = template_loader.get_template(template_name)
77     c = RequestContext(request, {
78         '%s_list' % template_object_name: object_list,
79         'month': date,
80-        'next_month': (last_day < datetime.date.today()) and (last_day + datetime.timedelta(days=1)) or None,
81+        'next_month': next_month,
82         'previous_month': first_day - datetime.timedelta(days=1),
83     }, context_processors)
84     for key, value in extra_context.items():
85@@ -152,7 +162,7 @@
86 def archive_week(request, year, week, queryset, date_field,
87         template_name=None, template_loader=loader,
88         extra_context=None, allow_empty=True, context_processors=None,
89-        template_object_name='object', mimetype=None):
90+        template_object_name='object', mimetype=None, allow_future=False):
91     """
92     Generic weekly archive view.
93 
94@@ -177,8 +187,8 @@
95     last_day = date + datetime.timedelta(days=7)
96     lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
97 
98-    # Only bother to check current date if the week isn't in the past.
99-    if last_day >= now.date():
100+    # Only bother to check current date if the week isn't in the past and future objects aren't requested.
101+    if last_day >= now.date() and not allow_future:
102         lookup_kwargs['%s__lte' % date_field] = now
103     object_list = queryset.filter(**lookup_kwargs)
104     if not object_list and not allow_empty:
105@@ -201,7 +211,7 @@
106         month_format='%b', day_format='%d', template_name=None,
107         template_loader=loader, extra_context=None, allow_empty=False,
108         context_processors=None, template_object_name='object',
109-        mimetype=None):
110+        mimetype=None, allow_future=False):
111     """
112     Generic daily archive view.
113 
114@@ -229,12 +239,21 @@
115         '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)),
116     }
117 
118-    # Only bother to check current date if the date isn't in the past.
119-    if date >= now.date():
120+    # Only bother to check current date if the date isn't in the past and future objects aren't requested.
121+    if date >= now.date() and not allow_future:
122         lookup_kwargs['%s__lte' % date_field] = now
123     object_list = queryset.filter(**lookup_kwargs)
124     if not allow_empty and not object_list:
125         raise Http404
126+
127+    # Calculate the next day, if applicable.
128+    if allow_future:
129+        next_day = date + datetime.timedelta(days=1)
130+    elif date < datetime.date.today():
131+        next_day = date + datetime.timedelta(days=1)
132+    else:
133+        next_day = None
134+
135     if not template_name:
136         template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower())
137     t = template_loader.get_template(template_name)
138@@ -242,7 +261,7 @@
139         '%s_list' % template_object_name: object_list,
140         'day': date,
141         'previous_day': date - datetime.timedelta(days=1),
142-        'next_day': (date < datetime.date.today()) and (date + datetime.timedelta(days=1)) or None,
143+        'next_day': next_day,
144     }, context_processors)
145     for key, value in extra_context.items():
146         if callable(value):
147@@ -267,7 +286,7 @@
148         month_format='%b', day_format='%d', object_id=None, slug=None,
149         slug_field=None, template_name=None, template_name_field=None,
150         template_loader=loader, extra_context=None, context_processors=None,
151-        template_object_name='object', mimetype=None):
152+        template_object_name='object', mimetype=None, allow_future=False):
153     """
154     Generic detail view from year/month/day/slug or year/month/day/id structure.
155 
156@@ -289,8 +308,8 @@
157         '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)),
158     }
159 
160-    # Only bother to check current date if the date isn't in the past.
161-    if date >= now.date():
162+    # Only bother to check current date if the date isn't in the past and future objects aren't requested.
163+    if date >= now.date() and not allow_future:
164         lookup_kwargs['%s__lte' % date_field] = now
165     if object_id:
166         lookup_kwargs['%s__exact' % model._meta.pk.name] = object_id