##// END OF EJS Templates
Using django paginator instead of manual pagination
neko259 -
r493:1e58394d 1.6-dev
parent child Browse files
Show More
@@ -6,6 +6,7 b' import time'
6 6 import math
7 7 import re
8 8 from django.core.cache import cache
9 from django.core.paginator import Paginator
9 10
10 11 from django.db import models
11 12 from django.http import Http404
@@ -126,28 +127,12 b' class PostManager(models.Manager):'
126 127 threads = threads.filter(archived=archived).order_by(order_by)
127 128
128 129 if page != ALL_PAGES:
129 thread_count = threads.count()
130
131 if page < self._get_page_count(thread_count):
132 start_thread = page * settings.THREADS_PER_PAGE
133 end_thread = min(start_thread + settings.THREADS_PER_PAGE,
134 thread_count)
135 threads = threads[start_thread:end_thread]
130 threads = Paginator(threads, settings.THREADS_PER_PAGE).page(
131 page).object_list
136 132
137 133 return threads
138 134
139 135 # TODO Move this method to thread manager
140 def get_thread_page_count(self, tag=None, archived=False):
141 if tag:
142 threads = Thread.objects.filter(tags=tag)
143 else:
144 threads = Thread.objects.all()
145
146 threads = threads.filter(archived=archived)
147
148 return self._get_page_count(threads.count())
149
150 # TODO Move this method to thread manager
151 136 def _delete_old_threads(self):
152 137 """
153 138 Preserves maximum thread count. If there are too many threads,
@@ -180,13 +165,6 b' class PostManager(models.Manager):'
180 165 referenced_post.last_edit_time = post.pub_time
181 166 referenced_post.save()
182 167
183 def _get_page_count(self, thread_count):
184 """
185 Get number of pages that will be needed for all threads
186 """
187
188 return int(math.ceil(thread_count / float(settings.THREADS_PER_PAGE)))
189
190 168 def get_posts_per_day(self):
191 169 """
192 170 Get average count of posts per day for the last 7 days
@@ -6,23 +6,23 b''
6 6 {% load static %}
7 7
8 8 {% block head %}
9 <title>Neboard</title>
9 <title>Neboard - {% trans 'Archive' %}</title>
10 10
11 {% if prev_page %}
12 <link rel="next" href="
11 {% if current_page.has_previous %}
12 <link rel="prev" href="
13 13 {% if tag %}
14 {% url "tag" tag_name=tag page=prev_page %}
14 {% url "tag" tag_name=tag page=current_page.previous_page_number %}
15 15 {% else %}
16 {% url "index" page=prev_page %}
16 {% url "index" page=current_page.previous_page_number %}
17 17 {% endif %}
18 18 " />
19 19 {% endif %}
20 {% if next_page %}
20 {% if current_page.has_next %}
21 21 <link rel="next" href="
22 22 {% if tag %}
23 {% url "tag" tag_name=tag page=next_page %}
23 {% url "tag" tag_name=tag page=current_page.next_page_number %}
24 24 {% else %}
25 {% url "index" page=next_page %}
25 {% url "index" page=current_page.next_page_number %}
26 26 {% endif %}
27 27 " />
28 28 {% endif %}
@@ -34,9 +34,9 b''
34 34 {% get_current_language as LANGUAGE_CODE %}
35 35
36 36 {% if threads %}
37 {% if prev_page %}
37 {% if current_page.has_previous %}
38 38 <div class="page_link">
39 <a href="{% url "archive" page=prev_page %}">{% trans "Previous page" %}</a>
39 <a href="{% url "archive" page=current_page.previous_page_number %}">{% trans "Previous page" %}</a>
40 40 </div>
41 41 {% endif %}
42 42
@@ -111,9 +111,9 b''
111 111 {% endcache %}
112 112 {% endfor %}
113 113
114 {% if next_page %}
114 {% if current_page.has_next %}
115 115 <div class="page_link">
116 <a href="{% url "archive" page=next_page %}">{% trans "Next page" %}</a>
116 <a href="{% url "archive" page=current_page.next_page_number %}">{% trans "Next page" %}</a>
117 117 </div>
118 118 {% endif %}
119 119 {% else %}
@@ -126,11 +126,11 b''
126 126 {% block metapanel %}
127 127
128 128 <span class="metapanel">
129 <b><a href="{% url "authors" %}">Neboard</a> 1.5 Aker</b>
129 <b><a href="{% url "authors" %}">Neboard</a> 1.6 Amon</b>
130 130 {% trans "Pages:" %}[
131 {% for page in pages %}
131 {% for page in paginator.page_range %}
132 132 <a
133 {% ifequal page current_page %}
133 {% ifequal page current_page.number %}
134 134 class="current_page"
135 135 {% endifequal %}
136 136 href="
@@ -12,21 +12,21 b''
12 12 <title>Neboard</title>
13 13 {% endif %}
14 14
15 {% if prev_page %}
16 <link rel="next" href="
15 {% if current_page.has_previous %}
16 <link rel="prev" href="
17 17 {% if tag %}
18 {% url "tag" tag_name=tag page=prev_page %}
18 {% url "tag" tag_name=tag page=current_page.previous_page_number %}
19 19 {% else %}
20 {% url "index" page=prev_page %}
20 {% url "index" page=current_page.previous_page_number %}
21 21 {% endif %}
22 22 " />
23 23 {% endif %}
24 {% if next_page %}
24 {% if current_page.has_next %}
25 25 <link rel="next" href="
26 26 {% if tag %}
27 {% url "tag" tag_name=tag page=next_page %}
27 {% url "tag" tag_name=tag page=current_page.next_page_number %}
28 28 {% else %}
29 {% url "index" page=next_page %}
29 {% url "index" page=current_page.next_page_number %}
30 30 {% endif %}
31 31 " />
32 32 {% endif %}
@@ -53,13 +53,13 b''
53 53 {% endif %}
54 54
55 55 {% if threads %}
56 {% if prev_page %}
56 {% if current_page.has_previous %}
57 57 <div class="page_link">
58 58 <a href="
59 59 {% if tag %}
60 {% url "tag" tag_name=tag page=prev_page %}
60 {% url "tag" tag_name=tag page=current_page.previous_page_number %}
61 61 {% else %}
62 {% url "index" page=prev_page %}
62 {% url "index" page=current_page.previous_page_number %}
63 63 {% endif %}
64 64 ">{% trans "Previous page" %}</a>
65 65 </div>
@@ -69,9 +69,9 b''
69 69 {% cache 600 thread_short thread.id thread.thread.last_edit_time moderator LANGUAGE_CODE %}
70 70 <div class="thread">
71 71 {% if thread.bumpable %}
72 <div class="post" id="{{ thread.op.id }}">
72 <div class="post" id="{{ thread.op.id }}">
73 73 {% else %}
74 <div class="post dead_post" id="{{ thread.op.id }}">
74 <div class="post dead_post" id="{{ thread.op.id }}">
75 75 {% endif %}
76 76 {% if thread.op.image %}
77 77 <div class="image">
@@ -192,13 +192,13 b''
192 192 {% endcache %}
193 193 {% endfor %}
194 194
195 {% if next_page %}
195 {% if current_page.has_next %}
196 196 <div class="page_link">
197 197 <a href="
198 198 {% if tag %}
199 {% url "tag" tag_name=tag page=next_page %}
199 {% url "tag" tag_name=tag page=current_page.next_page_number %}
200 200 {% else %}
201 {% url "index" page=next_page %}
201 {% url "index" page=current_page.next_page_number %}
202 202 {% endif %}
203 203 ">{% trans "Next page" %}</a>
204 204 </div>
@@ -233,9 +233,9 b''
233 233 <span class="metapanel">
234 234 <b><a href="{% url "authors" %}">Neboard</a> 1.6 Amon</b>
235 235 {% trans "Pages:" %}[
236 {% for page in pages %}
236 {% for page in paginator.page_range %}
237 237 <a
238 {% ifequal page current_page %}
238 {% ifequal page current_page.number %}
239 239 class="current_page"
240 240 {% endifequal %}
241 241 href="
@@ -22,13 +22,14 b' from django.utils import timezone'
22 22 from django.db import transaction
23 23 from django.views.decorators.cache import cache_page
24 24 from django.views.i18n import javascript_catalog
25 from django.core.paginator import Paginator
25 26
26 27 from boards import forms
27 28 import boards
28 29 from boards import utils
29 30 from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \
30 31 ThreadCaptchaForm, PostCaptchaForm, LoginForm, ModeratorSettingsForm
31 from boards.models import Post, Tag, Ban, User
32 from boards.models import Post, Tag, Ban, User, Thread
32 33 from boards.models.post import SETTING_MODERATE, REGEX_REPLY
33 34 from boards.models.user import RANK_USER
34 35 from boards import authors
@@ -40,8 +41,10 b" BAN_REASON_SPAM = 'Autoban: spam bot'"
40 41 MODE_GALLERY = 'gallery'
41 42 MODE_NORMAL = 'normal'
42 43
44 DEFAULT_PAGE = 1
43 45
44 def index(request, page=0):
46
47 def index(request, page=DEFAULT_PAGE):
45 48 context = _init_default_context(request)
46 49
47 50 if utils.need_include_captcha(request):
@@ -71,21 +74,16 b' def index(request, page=0):'
71 74 # TODO Make this generic for tag and threads list pages
72 75 context['threads'] = None if len(threads) == 0 else threads
73 76 context['form'] = form
74 context['current_page'] = int(page)
75 77
76 page_count = Post.objects.get_thread_page_count()
77 context['pages'] = range(page_count) if page_count > 0 else [0]
78 page = int(page)
79 if page < page_count - 1:
80 context['next_page'] = str(page + 1)
81 if page > 0:
82 context['prev_page'] = str(page - 1)
78 paginator = Paginator(Thread.objects.filter(archived=False),
79 neboard.settings.THREADS_PER_PAGE)
80 _get_page_context(paginator, context, page)
83 81
84 82 return render(request, 'boards/posting_general.html',
85 83 context)
86 84
87 85
88 def archive(request, page=0):
86 def archive(request, page=DEFAULT_PAGE):
89 87 context = _init_default_context(request)
90 88
91 89 threads = []
@@ -94,15 +92,10 b' def archive(request, page=0):'
94 92 threads.append(_get_template_thread(thread_to_show))
95 93
96 94 context['threads'] = threads
97 context['current_page'] = int(page)
98 95
99 page_count = Post.objects.get_thread_page_count(archived=True)
100 context['pages'] = range(page_count) if page_count > 0 else [0]
101 page = int(page)
102 if page < page_count - 1:
103 context['next_page'] = str(page + 1)
104 if page > 0:
105 context['prev_page'] = str(page - 1)
96 paginator = Paginator(Thread.objects.filter(archived=True),
97 neboard.settings.THREADS_PER_PAGE)
98 _get_page_context(paginator, context, page)
106 99
107 100 return render(request, 'boards/archive.html', context)
108 101
@@ -158,10 +151,10 b' def _new_post(request, form, opening_pos'
158 151 return redirect(thread, post_id=thread_to_show)
159 152
160 153
161 def tag(request, tag_name, page=0):
154 def tag(request, tag_name, page=DEFAULT_PAGE):
162 155 """
163 156 Get all tag threads. Threads are split in pages, so some page is
164 requested. Default page is 0.
157 requested.
165 158 """
166 159
167 160 tag = get_object_or_404(Tag, name=tag_name)
@@ -186,15 +179,10 b' def tag(request, tag_name, page=0):'
186 179 context = _init_default_context(request)
187 180 context['threads'] = None if len(threads) == 0 else threads
188 181 context['tag'] = tag
189 context['current_page'] = int(page)
190 182
191 page_count = Post.objects.get_thread_page_count(tag=tag)
192 context['pages'] = range(page_count)
193 page = int(page)
194 if page < page_count - 1:
195 context['next_page'] = str(page + 1)
196 if page > 0:
197 context['prev_page'] = str(page - 1)
183 paginator = Paginator(Post.objects.get_threads(tag=tag),
184 neboard.settings.THREADS_PER_PAGE)
185 _get_page_context(paginator, context, page)
198 186
199 187 context['form'] = form
200 188
@@ -602,3 +590,12 b' def _delete_old_users():'
602 590 tags_count=0).filter(registration_time__lt=old_registration_date):
603 591 if not Post.objects.filter(user=user).exists():
604 592 user.delete()
593
594
595 def _get_page_context(paginator, context, page):
596 """
597 Get pagination context variables
598 """
599
600 context['paginator'] = paginator
601 context['current_page'] = paginator.page(int(page))
General Comments 0
You need to be logged in to leave comments. Login now