Code

Ticket #2367: django_pagination.diff

File django_pagination.diff, 10.1 KB (added by Tyler <tyler@…>, 6 years ago)

The previous patch uses the deprecated ObjectPaginator class and changes paginator.py. This patch changes only date_based.py and uses the newer interface as defined in list_detail.py's object_list method.

Line 
1--- a/django/views/generic/date_based.py
2+++ b/django/views/generic/date_based.py
3@@ -6,11 +6,13 @@ from django.core.exceptions import ObjectDoesNotExist
4 from django.core.xheaders import populate_xheaders
5 from django.db.models.fields import DateTimeField
6 from django.http import Http404, HttpResponse
7+from django.core.paginator import QuerySetPaginator, InvalidPage
8 
9 def archive_index(request, queryset, date_field, num_latest=15,
10         template_name=None, template_loader=loader,
11         extra_context=None, allow_empty=True, context_processors=None,
12-        mimetype=None, allow_future=False, template_object_name='latest'):
13+        mimetype=None, allow_future=False, template_object_name='latest',
14+        paginate_by=None, page=None):
15     """
16     Generic top-level archive of date-based objects.
17 
18@@ -20,6 +22,10 @@ def archive_index(request, queryset, date_field, num_latest=15,
19             List of years
20         latest
21             Latest N (defaults to 15) objects by date
22+        paginator
23+            paginator object
24+        page_obj
25+            page object
26     """
27     if extra_context is None: extra_context = {}
28     model = queryset.model
29
30@@ -46,12 +54,41 @@ def archive_index(request, queryset, date_field, num_latest=15,
31             c[key] = value()
32         else:
33             c[key] = value
34+
35+    if paginate_by:
36+        paginator = QuerySetPaginator(latest, paginate_by, allow_empty_first_page=allow_empty)
37+        if not page:
38+            page = request.GET.get('page', 1)
39+        try:
40+            page_number = int(page)
41+        except ValueError:
42+            if page == 'last':
43+                page_number = paginator.num_pages
44+            else:
45+                # Page is not 'last', nor can it be converted to an int.
46+                raise Http404
47+        try:
48+            page_obj = paginator.page(page_number)
49+        except InvalidPage:
50+            raise Http404
51+        c[template_object_name] = page_obj.object_list
52+        c['paginator'] = paginator
53+        c['page_obj'] = page_obj
54+        c['is_paginated'] = True
55+    else:
56+        c['paginator'] = None
57+        c['page_obj'] = None
58+        c['is_paginated'] = False
59+        if not allow_empty and len(queryset) == 0:
60+            raise Http404
61+
62     return HttpResponse(t.render(c), mimetype=mimetype)
63 
64 def archive_year(request, year, queryset, date_field, template_name=None,
65         template_loader=loader, extra_context=None, allow_empty=False,
66         context_processors=None, template_object_name='object', mimetype=None,
67-        make_object_list=False, allow_future=False):
68+        make_object_list=False, allow_future=False,
69+        paginate_by=None, page=None):
70     """
71     Generic yearly archive view.
72 
73@@ -64,6 +101,10 @@ def archive_year(request, year, queryset, date_field, template_name=None,
74         object_list
75             List of objects published in the given month
76             (Only available if make_object_list argument is True)
77+        paginator
78+            paginator object
79+        page_obj
80+            page object
81     """
82     if extra_context is None: extra_context = {}
83     model = queryset.model
84@@ -94,12 +135,41 @@ def archive_year(request, year, queryset, date_field, template_name=None,
85             c[key] = value()
86         else:
87             c[key] = value
88+
89+    if paginate_by:
90+        paginator = QuerySetPaginator(object_list, paginate_by, allow_empty_first_page=allow_empty)
91+        if not page:
92+            page = request.GET.get('page', 1)
93+        try:
94+            page_number = int(page)
95+        except ValueError:
96+            if page == 'last':
97+                page_number = paginator.num_pages
98+            else:
99+                # Page is not 'last', nor can it be converted to an int.
100+                raise Http404
101+        try:
102+            page_obj = paginator.page(page_number)
103+        except InvalidPage:
104+            raise Http404
105+        c['%s_list' % template_object_name] = page_obj.object_list
106+        c['paginator'] = paginator
107+        c['page_obj'] = page_obj
108+        c['is_paginated'] = True
109+    else:
110+        c['paginator'] = None
111+        c['page_obj'] = None
112+        c['is_paginated'] = False
113+        if not allow_empty and len(queryset) == 0:
114+            raise Http404
115+
116     return HttpResponse(t.render(c), mimetype=mimetype)
117 
118 def archive_month(request, year, month, queryset, date_field,
119         month_format='%b', template_name=None, template_loader=loader,
120         extra_context=None, allow_empty=False, context_processors=None,
121-        template_object_name='object', mimetype=None, allow_future=False):
122+        template_object_name='object', mimetype=None, allow_future=False,
123+        paginate_by=None, page=None):
124     """
125     Generic monthly archive view.
126 
127@@ -113,6 +183,10 @@ def archive_month(request, year, month, queryset, date_field,
128             (date) the first day of the previous month
129         object_list:
130             list of objects published in the given month
131+        paginator
132+            paginator object
133+        page_obj
134+            page object
135     """
136     if extra_context is None: extra_context = {}
137     try:
138@@ -160,12 +234,41 @@ def archive_month(request, year, month, queryset, date_field,
139             c[key] = value()
140         else:
141             c[key] = value
142+
143+    if paginate_by:
144+        paginator = QuerySetPaginator(object_list, paginate_by, allow_empty_first_page=allow_empty)
145+        if not page:
146+            page = request.GET.get('page', 1)
147+        try:
148+            page_number = int(page)
149+        except ValueError:
150+            if page == 'last':
151+                page_number = paginator.num_pages
152+            else:
153+                # Page is not 'last', nor can it be converted to an int.
154+                raise Http404
155+        try:
156+            page_obj = paginator.page(page_number)
157+        except InvalidPage:
158+            raise Http404
159+        c['%s_list' % template_object_name] = page_obj.object_list
160+        c['paginator'] = paginator
161+        c['page_obj'] = page_obj
162+        c['is_paginated'] = True
163+    else:
164+        c['paginator'] = None
165+        c['page_obj'] = None
166+        c['is_paginated'] = False
167+        if not allow_empty and len(queryset) == 0:
168+            raise Http404
169+
170     return HttpResponse(t.render(c), mimetype=mimetype)
171 
172 def archive_week(request, year, week, queryset, date_field,
173         template_name=None, template_loader=loader,
174         extra_context=None, allow_empty=True, context_processors=None,
175-        template_object_name='object', mimetype=None, allow_future=False):
176+        template_object_name='object', mimetype=None, allow_future=False,
177+        paginate_by=None, page=None):
178     """
179     Generic weekly archive view.
180 
181@@ -175,6 +278,10 @@ def archive_week(request, year, week, queryset, date_field,
182             (date) this week
183         object_list:
184             list of objects published in the given week
185+        paginator
186+            paginator object
187+        page_obj
188+            page object
189     """
190     if extra_context is None: extra_context = {}
191     try:
192@@ -208,13 +315,42 @@ def archive_week(request, year, week, queryset, date_field,
193             c[key] = value()
194         else:
195             c[key] = value
196+
197+    if paginate_by:
198+        paginator = QuerySetPaginator(object_list, paginate_by, allow_empty_first_page=allow_empty)
199+        if not page:
200+            page = request.GET.get('page', 1)
201+        try:
202+            page_number = int(page)
203+        except ValueError:
204+            if page == 'last':
205+                page_number = paginator.num_pages
206+            else:
207+                # Page is not 'last', nor can it be converted to an int.
208+                raise Http404
209+        try:
210+            page_obj = paginator.page(page_number)
211+        except InvalidPage:
212+            raise Http404
213+        c['%s_list' % template_object_name] = page_obj.object_list
214+        c['paginator'] = paginator
215+        c['page_obj'] = page_obj
216+        c['is_paginated'] = True
217+    else:
218+        c['paginator'] = None
219+        c['page_obj'] = None
220+        c['is_paginated'] = False
221+        if not allow_empty and len(queryset) == 0:
222+            raise Http404
223+
224     return HttpResponse(t.render(c), mimetype=mimetype)
225 
226 def archive_day(request, year, month, day, queryset, date_field,
227         month_format='%b', day_format='%d', template_name=None,
228         template_loader=loader, extra_context=None, allow_empty=False,
229         context_processors=None, template_object_name='object',
230-        mimetype=None, allow_future=False):
231+        mimetype=None, allow_future=False,
232+        paginate_by=None, page=None):
233     """
234     Generic daily archive view.
235 
236@@ -228,6 +364,10 @@ def archive_day(request, year, month, day, queryset, date_field,
237             (datetime) the previous day
238         next_day
239             (datetime) the next day, or None if the current day is today
240+        paginator
241+            paginator object
242+        page_obj
243+            page object
244     """
245     if extra_context is None: extra_context = {}
246     try:
247@@ -272,6 +412,34 @@ def archive_day(request, year, month, day, queryset, date_field,
248             c[key] = value()
249         else:
250             c[key] = value
251+
252+    if paginate_by:
253+        paginator = QuerySetPaginator(object_list, paginate_by, allow_empty_first_page=allow_empty)
254+        if not page:
255+            page = request.GET.get('page', 1)
256+        try:
257+            page_number = int(page)
258+        except ValueError:
259+            if page == 'last':
260+                page_number = paginator.num_pages
261+            else:
262+                # Page is not 'last', nor can it be converted to an int.
263+                raise Http404
264+        try:
265+            page_obj = paginator.page(page_number)
266+        except InvalidPage:
267+            raise Http404
268+        c['%s_list' % template_object_name] = page_obj.object_list
269+        c['paginator'] = paginator
270+        c['page_obj'] = page_obj
271+        c['is_paginated'] = True
272+    else:
273+        c['paginator'] = None
274+        c['page_obj'] = None
275+        c['is_paginated'] = False
276+        if not allow_empty and len(queryset) == 0:
277+            raise Http404
278+
279     return HttpResponse(t.render(c), mimetype=mimetype)
280 
281 def archive_today(request, **kwargs):