##// END OF EJS Templates
Updated paginator for long page lists. Removed old get_threads method in the post manager
neko259 -
r596:5657c06f default
parent child Browse files
Show More
@@ -0,0 +1,1 b''
1 __author__ = 'vurdalak'
@@ -0,0 +1,22 b''
1 from django.core.paginator import Paginator
2
3 __author__ = 'neko259'
4
5 PAGINATOR_LOOKAROUND_SIZE = 3
6
7
8 def get_paginator(*args, **kwargs):
9 return DividedPaginator(*args, **kwargs)
10
11
12 class DividedPaginator(Paginator):
13
14 lookaround_size = PAGINATOR_LOOKAROUND_SIZE
15 current_page = 0
16
17 def center_range(self):
18 index = self.page_range.index(self.current_page)
19
20 start = max(0, index - self.lookaround_size)
21 end = min(len(self.page_range), index + self.lookaround_size + 1)
22 return self.page_range[start:end] No newline at end of file
@@ -116,26 +116,6 b' class PostManager(models.Manager):'
116 posts = self.filter(poster_ip=ip)
116 posts = self.filter(poster_ip=ip)
117 map(self.delete_post, posts)
117 map(self.delete_post, posts)
118
118
119 # TODO This method may not be needed any more, because django's paginator
120 # is used
121 def get_threads(self, tag=None, page=ALL_PAGES,
122 order_by='-bump_time', archived=False):
123 if tag:
124 threads = tag.threads
125
126 if not threads.exists():
127 raise Http404
128 else:
129 threads = Thread.objects.all()
130
131 threads = threads.filter(archived=archived).order_by(order_by)
132
133 if page != ALL_PAGES:
134 threads = Paginator(threads, settings.THREADS_PER_PAGE).page(
135 page).object_list
136
137 return threads
138
139 # TODO Move this method to thread manager
119 # TODO Move this method to thread manager
140 def _delete_old_threads(self):
120 def _delete_old_threads(self):
141 """
121 """
@@ -143,7 +123,7 b' class PostManager(models.Manager):'
143 archive the old ones.
123 archive the old ones.
144 """
124 """
145
125
146 threads = self.get_threads()
126 threads = Thread.objects.filter(archived=False)
147 thread_count = threads.count()
127 thread_count = threads.count()
148
128
149 if thread_count > settings.MAX_THREAD_COUNT:
129 if thread_count > settings.MAX_THREAD_COUNT:
@@ -1,7 +1,7 b''
1 from django.contrib.syndication.views import Feed
1 from django.contrib.syndication.views import Feed
2 from django.core.urlresolvers import reverse
2 from django.core.urlresolvers import reverse
3 from django.shortcuts import get_object_or_404
3 from django.shortcuts import get_object_or_404
4 from boards.models import Post, Tag
4 from boards.models import Post, Tag, Thread
5 from neboard import settings
5 from neboard import settings
6
6
7 __author__ = 'neko259'
7 __author__ = 'neko259'
@@ -15,7 +15,7 b' class AllThreadsFeed(Feed):'
15 description_template = 'boards/rss/post.html'
15 description_template = 'boards/rss/post.html'
16
16
17 def items(self):
17 def items(self):
18 return Post.objects.get_threads(order_by='-id')
18 return Thread.objects.filter(archived=False).order_by('-id')
19
19
20 def item_title(self, item):
20 def item_title(self, item):
21 return item.get_opening_post().title
21 return item.get_opening_post().title
@@ -33,7 +33,7 b' class TagThreadsFeed(Feed):'
33 description_template = 'boards/rss/post.html'
33 description_template = 'boards/rss/post.html'
34
34
35 def items(self, obj):
35 def items(self, obj):
36 return Post.objects.get_threads(tag=obj, order_by='-id')
36 return obj.threads.filter(archived=False).order_by('-id')
37
37
38 def get_object(self, request, tag_name):
38 def get_object(self, request, tag_name):
39 return get_object_or_404(Tag, name=tag_name)
39 return get_object_or_404(Tag, name=tag_name)
@@ -139,8 +139,18 b''
139
139
140 <span class="metapanel">
140 <span class="metapanel">
141 <b><a href="{% url "authors" %}">{{ site_name }}</a> {{ version }}</b>
141 <b><a href="{% url "authors" %}">{{ site_name }}</a> {{ version }}</b>
142 {% trans "Pages:" %}[
142 {% trans "Pages:" %}
143 {% for page in paginator.page_range %}
143 <a href="
144 {% if tag %}
145 {% url "tag" tag_name=tag page=paginator.page_range|first %}
146 {% elif archived %}
147 {% url "archive" page=paginator.page_range|first %}
148 {% else %}
149 {% url "index" page=paginator.page_range|first %}
150 {% endif %}
151 ">&lt;&lt;</a>
152 [
153 {% for page in paginator.center_range %}
144 <a
154 <a
145 {% ifequal page current_page.number %}
155 {% ifequal page current_page.number %}
146 class="current_page"
156 class="current_page"
@@ -157,6 +167,15 b''
157 {% if not forloop.last %},{% endif %}
167 {% if not forloop.last %},{% endif %}
158 {% endfor %}
168 {% endfor %}
159 ]
169 ]
170 <a href="
171 {% if tag %}
172 {% url "tag" tag_name=tag page=paginator.page_range|last %}
173 {% elif archived %}
174 {% url "archive" page=paginator.page_range|last %}
175 {% else %}
176 {% url "index" page=paginator.page_range|last %}
177 {% endif %}
178 ">&gt;&gt;</a>
160 [<a href="rss/">RSS</a>]
179 [<a href="rss/">RSS</a>]
161 </span>
180 </span>
162
181
@@ -1,12 +1,13 b''
1 # coding=utf-8
1 # coding=utf-8
2 import time
2 import time
3 import logging
3 import logging
4 from django.core.paginator import Paginator
4
5
5 from django.test import TestCase
6 from django.test import TestCase
6 from django.test.client import Client
7 from django.test.client import Client
7 from django.core.urlresolvers import reverse, NoReverseMatch
8 from django.core.urlresolvers import reverse, NoReverseMatch
8
9
9 from boards.models import Post, Tag
10 from boards.models import Post, Tag, Thread
10 from boards import urls
11 from boards import urls
11 from neboard import settings
12 from neboard import settings
12
13
@@ -111,7 +112,7 b' class PostTests(TestCase):'
111 self._create_post()
112 self._create_post()
112
113
113 self.assertEqual(settings.MAX_THREAD_COUNT,
114 self.assertEqual(settings.MAX_THREAD_COUNT,
114 len(Post.objects.get_threads()))
115 len(Thread.objects.filter(archived=False)))
115
116
116 def test_pages(self):
117 def test_pages(self):
117 """Test that the thread list is properly split into pages"""
118 """Test that the thread list is properly split into pages"""
@@ -119,9 +120,11 b' class PostTests(TestCase):'
119 for i in range(settings.MAX_THREAD_COUNT):
120 for i in range(settings.MAX_THREAD_COUNT):
120 self._create_post()
121 self._create_post()
121
122
122 all_threads = Post.objects.get_threads()
123 all_threads = Thread.objects.filter(archived=False)
123
124
124 posts_in_second_page = Post.objects.get_threads(page=2)
125 paginator = Paginator(Thread.objects.filter(archived=False),
126 settings.THREADS_PER_PAGE)
127 posts_in_second_page = paginator.page(2).object_list
125 first_post = posts_in_second_page[0]
128 first_post = posts_in_second_page[0]
126
129
127 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
130 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
@@ -1,11 +1,11 b''
1 import string
1 import string
2
2
3 from django.core.paginator import Paginator
4 from django.core.urlresolvers import reverse
3 from django.core.urlresolvers import reverse
5 from django.db import transaction
4 from django.db import transaction
6 from django.shortcuts import render, redirect
5 from django.shortcuts import render, redirect
7
6
8 from boards import utils
7 from boards import utils
8 from boards.abstracts.paginator import get_paginator
9 from boards.forms import ThreadForm, PlainErrorList
9 from boards.forms import ThreadForm, PlainErrorList
10 from boards.models import Post, Thread, Ban, Tag
10 from boards.models import Post, Thread, Ban, Tag
11 from boards.views.banned import BannedView
11 from boards.views.banned import BannedView
@@ -31,8 +31,9 b' class AllThreadsView(PostMixin, BaseBoar'
31 if not form:
31 if not form:
32 form = ThreadForm(error_class=PlainErrorList)
32 form = ThreadForm(error_class=PlainErrorList)
33
33
34 paginator = Paginator(self.get_threads(),
34 paginator = get_paginator(self.get_threads(),
35 neboard.settings.THREADS_PER_PAGE)
35 neboard.settings.THREADS_PER_PAGE)
36 paginator.current_page = int(page)
36
37
37 threads = paginator.page(page).object_list
38 threads = paginator.page(page).object_list
38
39
@@ -44,8 +45,6 b' class AllThreadsView(PostMixin, BaseBoar'
44 return render(request, TEMPLATE, context)
45 return render(request, TEMPLATE, context)
45
46
46 def post(self, request, page=DEFAULT_PAGE):
47 def post(self, request, page=DEFAULT_PAGE):
47 context = self.get_context_data(request=request)
48
49 form = ThreadForm(request.POST, request.FILES,
48 form = ThreadForm(request.POST, request.FILES,
50 error_class=PlainErrorList)
49 error_class=PlainErrorList)
51 form.session = request.session
50 form.session = request.session
General Comments 0
You need to be logged in to leave comments. Login now