##// END OF EJS Templates
Simplify paginator page list building and unified it between different views
neko259 -
r2061:b31844e6 default
parent child Browse files
Show More
@@ -1,9 +1,9 b''
1 from django.core.paginator import Paginator
1 2 from boards.abstracts.constants import PARAM_PAGE
2 3
3 4 __author__ = 'neko259'
4 5
5 from django.core.paginator import Paginator
6
6 PATTERN_PAGE_URL = '<a {} href="{}">{}</a>'
7 7 PAGINATOR_LOOKAROUND_SIZE = 2
8 8
9 9
@@ -16,11 +16,11 b' class DividedPaginator(Paginator):'
16 16 lookaround_size = PAGINATOR_LOOKAROUND_SIZE
17 17
18 18 def __init__(self, object_list, per_page, orphans=0,
19 allow_empty_first_page=True, current_page=1):
19 allow_empty_first_page=True, current_page=1, link=None, params=None):
20 20 super().__init__(object_list, per_page, orphans, allow_empty_first_page)
21 21
22 self.link = None
23 self.params = None
22 self.link = link
23 self.params = params
24 24 self.current_page = current_page
25 25
26 26 def _left_range(self):
@@ -69,10 +69,6 b' class DividedPaginator(Paginator):'
69 69
70 70 return dividers
71 71
72 def set_url(self, link, params):
73 self.link = link
74 self.params = params
75
76 72 def get_page_url(self, page):
77 73 self.params[PARAM_PAGE] = page
78 74 url_params = '&'.join(['{}={}'.format(key, self.params[key])
@@ -91,3 +87,20 b' class DividedPaginator(Paginator):'
91 87 current = self.page(self.current_page)
92 88 if current.has_previous():
93 89 return self.get_page_url(current.previous_page_number())
90
91 def get_page_url_list(self):
92 output = []
93
94 dividers = self.get_dividers()
95 for page in self.get_divided_range():
96 if page in dividers:
97 output.append('...')
98
99 if page == self.current_page:
100 cls = 'class="current_page'
101 else:
102 cls = ''
103 output.append(PATTERN_PAGE_URL.format(cls, self.get_page_url(page),
104 page))
105
106 return ', '.join(output)
@@ -160,25 +160,3 b''
160 160 <script src="{% static 'js/thread_create.js' %}"></script>
161 161
162 162 {% endblock %}
163
164 {% block metapanel %}
165
166 <span class="metapanel">
167 {% trans "Pages:" %}
168 [
169 {% with dividers=paginator.get_dividers %}
170 {% for page in paginator.get_divided_range %}
171 {% if page in dividers %}
172 …,
173 {% endif %}
174 <a
175 {% ifequal page current_page.number %}
176 class="current_page"
177 {% endifequal %}
178 href="{% page_url paginator page %}">{{ page }}</a>{% if not forloop.last %},{% endif %}
179 {% endfor %}
180 {% endwith %}
181 ]
182 </span>
183
184 {% endblock %}
@@ -1,4 +1,4 b''
1 {% extends 'boards/base.html' %}
1 {% extends 'boards/paginated.html' %}
2 2
3 3 {% load board %}
4 4 {% load i18n %}
@@ -6,21 +6,7 b''
6 6 {% block metapanel %}
7 7
8 8 <span class="metapanel">
9 {% trans "Pages:" %}
10 [
11 {% with dividers=paginator.get_dividers %}
12 {% for page in paginator.get_divided_range %}
13 {% if page in dividers %}
14 …,
15 {% endif %}
16 <a
17 {% ifequal page current_page.number %}
18 class="current_page"
19 {% endifequal %}
20 href="{% page_url paginator page %}">{{ page }}</a>{% if not forloop.last %},{% endif %}
21 {% endfor %}
22 {% endwith %}
23 ]
9 {% trans "Pages:" %} [{{ paginator.get_page_url_list|safe }}]
24 10 </span>
25 11
26 12 {% endblock %}
@@ -1,4 +1,4 b''
1 {% extends "boards/base.html" %}
1 {% extends "boards/paginated.html" %}
2 2
3 3 {% load i18n %}
4 4 {% load board %}
@@ -88,27 +88,3 b''
88 88 </div>
89 89 {% endif %}
90 90 {% endblock %}
91
92 {% block metapanel %}
93
94 <span class="metapanel">
95 <b><a href="{% url "authors" %}">{{ site_name }}</a> {{ version }}</b>
96 {% trans "Pages:" %}
97 [
98 {% with dividers=paginator.get_dividers %}
99 {% for page in paginator.get_divided_range %}
100 {% if page in dividers %}
101 …,
102 {% endif %}
103 <a
104 {% ifequal page paginator.current_page %}
105 class="current_page"
106 {% endifequal %}
107 href="{% page_url paginator page %}">{{ page }}</a>
108 {% if not forloop.last %},{% endif %}
109 {% endfor %}
110 {% endwith %}
111 ]
112 </span>
113
114 {% endblock %}
@@ -70,7 +70,9 b' class AllThreadsView(BaseBoardView, Pagi'
70 70 threads = threads.distinct()
71 71
72 72 paginator = get_paginator(threads, settings.get_int(
73 SECTION_VIEW, 'ThreadsPerPage'))
73 SECTION_VIEW, 'ThreadsPerPage'),
74 link=self.get_reverse_url(),
75 params=request.GET.dict())
74 76 paginator.current_page = int(page)
75 77
76 78 try:
@@ -82,7 +84,6 b' class AllThreadsView(BaseBoardView, Pagi'
82 84 params[CONTEXT_FORM] = form
83 85 params[PARAMETER_RSS_URL] = self.get_rss_url()
84 86
85 paginator.set_url(self.get_reverse_url(), request.GET.dict())
86 87 params.update(self.get_page_context(paginator, page))
87 88
88 89 return render(request, TEMPLATE, params)
@@ -119,13 +119,13 b' class FeedView(PaginatedMixin, BaseBoard'
119 119 queries.append(query)
120 120 params[PARAMETER_QUERIES] = queries
121 121
122 paginator = get_paginator(posts, POSTS_PER_PAGE)
122 paginator = get_paginator(posts, POSTS_PER_PAGE,
123 link=reverse('feed'),
124 params=request.GET.dict())
123 125 paginator.current_page = int(page)
124 126
125 127 params[PARAMETER_POSTS] = paginator.page(page).object_list
126 128
127 paginator.set_url(reverse('feed'), request.GET.dict())
128
129 129 params.update(self.get_page_context(paginator, page))
130 130
131 131 return render(request, TEMPLATE, params)
@@ -1,4 +1,5 b''
1 1 from django.shortcuts import render
2 from django.urls import reverse
2 3
3 4 from boards.abstracts.constants import PARAM_PAGE
4 5 from boards.abstracts.paginator import get_paginator
@@ -6,6 +7,7 b' from boards.abstracts.settingsmanager im'
6 7 SETTING_LAST_NOTIFICATION_ID
7 8 from boards.models.user import Notification
8 9 from boards.views.base import BaseBoardView
10 from boards.views.mixins import PaginatedMixin
9 11
10 12 DEFAULT_PAGE = '1'
11 13
@@ -14,7 +16,7 b" PARAM_USERNAMES = 'notification_username"
14 16 RESULTS_PER_PAGE = 10
15 17
16 18
17 class NotificationView(BaseBoardView):
19 class NotificationView(BaseBoardView, PaginatedMixin):
18 20
19 21 def get(self, request, username=None):
20 22 params = self.get_context_data()
@@ -37,12 +39,13 b' class NotificationView(BaseBoardView):'
37 39 settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID,
38 40 last_id)
39 41
40
41 paginator = get_paginator(posts, RESULTS_PER_PAGE)
42
43 42 page = int(request.GET.get(PARAM_PAGE, DEFAULT_PAGE))
43 paginator = get_paginator(posts, RESULTS_PER_PAGE,
44 link=reverse('notifications'),
45 params=request.GET.dict())
44 46
45 47 params[PARAM_PAGE] = paginator.page(page)
46 48 params[PARAM_USERNAMES] = notification_usernames
49 params.update(self.get_page_context(paginator, page))
47 50
48 51 return render(request, TEMPLATE, params)
@@ -40,8 +40,9 b' class BoardSearchView(View, PaginatedMix'
40 40 | Q(title__icontains=query) | Q(opening=True,
41 41 thread__tags__aliases__name__icontains=query)
42 42 | Q(attachments__url__icontains=query)).order_by('-id').distinct()
43 paginator = get_paginator(results, RESULTS_PER_PAGE)
44 paginator.set_url(reverse('search'), request.GET.dict())
43 paginator = get_paginator(results, RESULTS_PER_PAGE,
44 link=reverse('search'),
45 params=request.GET.dict())
45 46
46 47 page = int(request.GET.get(REQUEST_PAGE, '1'))
47 48
@@ -23,12 +23,14 b' class TagGalleryView(BaseBoardView, Pagi'
23 23 tag_alias = get_object_or_404(TagAlias, name=tag_name)
24 24 tag = tag_alias.parent
25 25 params['tag'] = tag
26 paginator = get_paginator(tag.get_images(), IMAGES_PER_PAGE,
27 current_page=page)
26 paginator = get_paginator(
27 tag.get_images(),
28 IMAGES_PER_PAGE,
29 current_page=page,
30 link=reverse('tag_gallery', kwargs={'tag_name': tag_name}),
31 params=request.GET.dict())
28 32 params['paginator'] = paginator
29 33 params['images'] = paginator.page(page).object_list
30 paginator.set_url(reverse('tag_gallery', kwargs={'tag_name': tag_name}),
31 request.GET.dict())
32 34 params.update(self.get_page_context(paginator, page))
33 35
34 36 return render(request, TEMPLATE, params)
General Comments 0
You need to be logged in to leave comments. Login now