Debian Codegraph @

django_mailman3/lib/paginator.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2016-2023 by the Free Software Foundation, Inc.
4 #
5 # This file is part of Django-Mailman.
6 #
7 # Django-Mailman is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the Free
9 # Software Foundation, either version 3 of the License, or (at your option)
10 # any later version.
11 #
12 # Django-Mailman is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 # more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Django-Mailman. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # Author: Aurelien Bompard <[email protected]>
21 #
22
23
24 from django.core.paginator import EmptyPage, InvalidPage, Paginator
25 from django.http import Http404
26 from django.utils.functional import cached_property
27
28
29 class MailmanPaginator(Paginator):
30 """
31 Subclass of Django's Paginator that works with MailmanClient's 'get_*_page'
32 functions. Use the function reference as the first argument::
33
34 MailmanPaginator(get_member_page, 25)
35
36 """
37
38 def __init__(self, function, per_page, **kwargs):
39 self.function = function
40 super(MailmanPaginator, self).__init__(None, per_page, **kwargs)
41
42 def page(self, number):
43 """
44 Returns a Page object for the given 1-based page number.
45 """
46 number = self.validate_number(number)
47 result = self.function(count=self.per_page, page=number)
48 return self._get_page(result, number, self)
49
50 @cached_property
51 def count(self):
52 """
53 Returns the total number of objects, across all pages.
54 """
55 # See the rest/docs/collections.rst in the Mailman source tree.
56 return self.function(count=0, page=1).total_size
57
58
59 def paginate(objects=None, page_num=None, results_per_page=None,
60 max_page_range=10, paginator_class=Paginator):
61 try:
62 page_num = int(page_num)
63 except (ValueError, TypeError):
64 page_num = 1
65 try:
66 results_per_page = int(results_per_page)
67 except (ValueError, TypeError):
68 results_per_page = 10
69 paginator = paginator_class(objects, results_per_page)
70 try:
71 objects = paginator.page(page_num)
72 except EmptyPage:
73 # If page is out of range (e.g. 9999), deliver last page of results.
74 objects = paginator.page(paginator.num_pages)
75 except InvalidPage:
76 # This never happens with Django's Paginator, but just in case.
77 raise Http404("No such page of results!")
78 # Calculate the displayed page range
79 if paginator.num_pages > max_page_range:
80 paginator.page_range_ellipsis = [1]
81 subrange_lower = page_num - int(max_page_range / 2 - 2)
82 if subrange_lower > 3:
83 paginator.page_range_ellipsis.append("...")
84 else:
85 subrange_lower = 2
86 paginator.page_range_ellipsis.extend(
87 list(range(subrange_lower, page_num)))
88 if page_num != 1 and page_num != paginator.num_pages:
89 paginator.page_range_ellipsis.append(page_num)
90 subrange_upper = page_num + int(max_page_range / 2 - 2)
91 if subrange_upper >= paginator.num_pages - 2:
92 subrange_upper = paginator.num_pages - 1
93 paginator.page_range_ellipsis.extend(
94 list(range(page_num+1, subrange_upper+1)))
95 if subrange_upper < paginator.num_pages - 2:
96 paginator.page_range_ellipsis.append("...")
97 paginator.page_range_ellipsis.append(paginator.num_pages)
98 else:
99 paginator.page_range_ellipsis = paginator.page_range
100 return objects