##// 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 from boards.abstracts.constants import PARAM_PAGE
2 from boards.abstracts.constants import PARAM_PAGE
2
3
3 __author__ = 'neko259'
4 __author__ = 'neko259'
4
5
5 from django.core.paginator import Paginator
6 PATTERN_PAGE_URL = '<a {} href="{}">{}</a>'
6
7 PAGINATOR_LOOKAROUND_SIZE = 2
7 PAGINATOR_LOOKAROUND_SIZE = 2
8
8
9
9
@@ -16,11 +16,11 b' class DividedPaginator(Paginator):'
16 lookaround_size = PAGINATOR_LOOKAROUND_SIZE
16 lookaround_size = PAGINATOR_LOOKAROUND_SIZE
17
17
18 def __init__(self, object_list, per_page, orphans=0,
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 super().__init__(object_list, per_page, orphans, allow_empty_first_page)
20 super().__init__(object_list, per_page, orphans, allow_empty_first_page)
21
21
22 self.link = None
22 self.link = link
23 self.params = None
23 self.params = params
24 self.current_page = current_page
24 self.current_page = current_page
25
25
26 def _left_range(self):
26 def _left_range(self):
@@ -69,10 +69,6 b' class DividedPaginator(Paginator):'
69
69
70 return dividers
70 return dividers
71
71
72 def set_url(self, link, params):
73 self.link = link
74 self.params = params
75
76 def get_page_url(self, page):
72 def get_page_url(self, page):
77 self.params[PARAM_PAGE] = page
73 self.params[PARAM_PAGE] = page
78 url_params = '&'.join(['{}={}'.format(key, self.params[key])
74 url_params = '&'.join(['{}={}'.format(key, self.params[key])
@@ -91,3 +87,20 b' class DividedPaginator(Paginator):'
91 current = self.page(self.current_page)
87 current = self.page(self.current_page)
92 if current.has_previous():
88 if current.has_previous():
93 return self.get_page_url(current.previous_page_number())
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 <script src="{% static 'js/thread_create.js' %}"></script>
160 <script src="{% static 'js/thread_create.js' %}"></script>
161
161
162 {% endblock %}
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 {% load board %}
3 {% load board %}
4 {% load i18n %}
4 {% load i18n %}
@@ -6,21 +6,7 b''
6 {% block metapanel %}
6 {% block metapanel %}
7
7
8 <span class="metapanel">
8 <span class="metapanel">
9 {% trans "Pages:" %}
9 {% trans "Pages:" %} [{{ paginator.get_page_url_list|safe }}]
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 ]
24 </span>
10 </span>
25
11
26 {% endblock %}
12 {% endblock %}
@@ -1,4 +1,4 b''
1 {% extends "boards/base.html" %}
1 {% extends "boards/paginated.html" %}
2
2
3 {% load i18n %}
3 {% load i18n %}
4 {% load board %}
4 {% load board %}
@@ -88,27 +88,3 b''
88 </div>
88 </div>
89 {% endif %}
89 {% endif %}
90 {% endblock %}
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 threads = threads.distinct()
70 threads = threads.distinct()
71
71
72 paginator = get_paginator(threads, settings.get_int(
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 paginator.current_page = int(page)
76 paginator.current_page = int(page)
75
77
76 try:
78 try:
@@ -82,7 +84,6 b' class AllThreadsView(BaseBoardView, Pagi'
82 params[CONTEXT_FORM] = form
84 params[CONTEXT_FORM] = form
83 params[PARAMETER_RSS_URL] = self.get_rss_url()
85 params[PARAMETER_RSS_URL] = self.get_rss_url()
84
86
85 paginator.set_url(self.get_reverse_url(), request.GET.dict())
86 params.update(self.get_page_context(paginator, page))
87 params.update(self.get_page_context(paginator, page))
87
88
88 return render(request, TEMPLATE, params)
89 return render(request, TEMPLATE, params)
@@ -119,13 +119,13 b' class FeedView(PaginatedMixin, BaseBoard'
119 queries.append(query)
119 queries.append(query)
120 params[PARAMETER_QUERIES] = queries
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 paginator.current_page = int(page)
125 paginator.current_page = int(page)
124
126
125 params[PARAMETER_POSTS] = paginator.page(page).object_list
127 params[PARAMETER_POSTS] = paginator.page(page).object_list
126
128
127 paginator.set_url(reverse('feed'), request.GET.dict())
128
129 params.update(self.get_page_context(paginator, page))
129 params.update(self.get_page_context(paginator, page))
130
130
131 return render(request, TEMPLATE, params)
131 return render(request, TEMPLATE, params)
@@ -1,4 +1,5 b''
1 from django.shortcuts import render
1 from django.shortcuts import render
2 from django.urls import reverse
2
3
3 from boards.abstracts.constants import PARAM_PAGE
4 from boards.abstracts.constants import PARAM_PAGE
4 from boards.abstracts.paginator import get_paginator
5 from boards.abstracts.paginator import get_paginator
@@ -6,6 +7,7 b' from boards.abstracts.settingsmanager im'
6 SETTING_LAST_NOTIFICATION_ID
7 SETTING_LAST_NOTIFICATION_ID
7 from boards.models.user import Notification
8 from boards.models.user import Notification
8 from boards.views.base import BaseBoardView
9 from boards.views.base import BaseBoardView
10 from boards.views.mixins import PaginatedMixin
9
11
10 DEFAULT_PAGE = '1'
12 DEFAULT_PAGE = '1'
11
13
@@ -14,7 +16,7 b" PARAM_USERNAMES = 'notification_username"
14 RESULTS_PER_PAGE = 10
16 RESULTS_PER_PAGE = 10
15
17
16
18
17 class NotificationView(BaseBoardView):
19 class NotificationView(BaseBoardView, PaginatedMixin):
18
20
19 def get(self, request, username=None):
21 def get(self, request, username=None):
20 params = self.get_context_data()
22 params = self.get_context_data()
@@ -37,12 +39,13 b' class NotificationView(BaseBoardView):'
37 settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID,
39 settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID,
38 last_id)
40 last_id)
39
41
40
41 paginator = get_paginator(posts, RESULTS_PER_PAGE)
42
43 page = int(request.GET.get(PARAM_PAGE, DEFAULT_PAGE))
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 params[PARAM_PAGE] = paginator.page(page)
47 params[PARAM_PAGE] = paginator.page(page)
46 params[PARAM_USERNAMES] = notification_usernames
48 params[PARAM_USERNAMES] = notification_usernames
49 params.update(self.get_page_context(paginator, page))
47
50
48 return render(request, TEMPLATE, params)
51 return render(request, TEMPLATE, params)
@@ -40,8 +40,9 b' class BoardSearchView(View, PaginatedMix'
40 | Q(title__icontains=query) | Q(opening=True,
40 | Q(title__icontains=query) | Q(opening=True,
41 thread__tags__aliases__name__icontains=query)
41 thread__tags__aliases__name__icontains=query)
42 | Q(attachments__url__icontains=query)).order_by('-id').distinct()
42 | Q(attachments__url__icontains=query)).order_by('-id').distinct()
43 paginator = get_paginator(results, RESULTS_PER_PAGE)
43 paginator = get_paginator(results, RESULTS_PER_PAGE,
44 paginator.set_url(reverse('search'), request.GET.dict())
44 link=reverse('search'),
45 params=request.GET.dict())
45
46
46 page = int(request.GET.get(REQUEST_PAGE, '1'))
47 page = int(request.GET.get(REQUEST_PAGE, '1'))
47
48
@@ -23,12 +23,14 b' class TagGalleryView(BaseBoardView, Pagi'
23 tag_alias = get_object_or_404(TagAlias, name=tag_name)
23 tag_alias = get_object_or_404(TagAlias, name=tag_name)
24 tag = tag_alias.parent
24 tag = tag_alias.parent
25 params['tag'] = tag
25 params['tag'] = tag
26 paginator = get_paginator(tag.get_images(), IMAGES_PER_PAGE,
26 paginator = get_paginator(
27 current_page=page)
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 params['paginator'] = paginator
32 params['paginator'] = paginator
29 params['images'] = paginator.page(page).object_list
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 params.update(self.get_page_context(paginator, page))
34 params.update(self.get_page_context(paginator, page))
33
35
34 return render(request, TEMPLATE, params)
36 return render(request, TEMPLATE, params)
General Comments 0
You need to be logged in to leave comments. Login now