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 | 116 | posts = self.filter(poster_ip=ip) |
|
117 | 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 | 119 | # TODO Move this method to thread manager |
|
140 | 120 | def _delete_old_threads(self): |
|
141 | 121 | """ |
@@ -143,7 +123,7 b' class PostManager(models.Manager):' | |||
|
143 | 123 | archive the old ones. |
|
144 | 124 | """ |
|
145 | 125 | |
|
146 | threads = self.get_threads() | |
|
126 | threads = Thread.objects.filter(archived=False) | |
|
147 | 127 | thread_count = threads.count() |
|
148 | 128 | |
|
149 | 129 | if thread_count > settings.MAX_THREAD_COUNT: |
@@ -1,7 +1,7 b'' | |||
|
1 | 1 | from django.contrib.syndication.views import Feed |
|
2 | 2 | from django.core.urlresolvers import reverse |
|
3 | 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 | 5 | from neboard import settings |
|
6 | 6 | |
|
7 | 7 | __author__ = 'neko259' |
@@ -15,7 +15,7 b' class AllThreadsFeed(Feed):' | |||
|
15 | 15 | description_template = 'boards/rss/post.html' |
|
16 | 16 | |
|
17 | 17 | def items(self): |
|
18 |
return |
|
|
18 | return Thread.objects.filter(archived=False).order_by('-id') | |
|
19 | 19 | |
|
20 | 20 | def item_title(self, item): |
|
21 | 21 | return item.get_opening_post().title |
@@ -33,7 +33,7 b' class TagThreadsFeed(Feed):' | |||
|
33 | 33 | description_template = 'boards/rss/post.html' |
|
34 | 34 | |
|
35 | 35 | def items(self, obj): |
|
36 |
return |
|
|
36 | return obj.threads.filter(archived=False).order_by('-id') | |
|
37 | 37 | |
|
38 | 38 | def get_object(self, request, tag_name): |
|
39 | 39 | return get_object_or_404(Tag, name=tag_name) |
@@ -139,8 +139,18 b'' | |||
|
139 | 139 | |
|
140 | 140 | <span class="metapanel"> |
|
141 | 141 | <b><a href="{% url "authors" %}">{{ site_name }}</a> {{ version }}</b> |
|
142 |
{% trans "Pages:" %} |
|
|
143 | {% for page in paginator.page_range %} | |
|
142 | {% trans "Pages:" %} | |
|
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 | "><<</a> | |
|
152 | [ | |
|
153 | {% for page in paginator.center_range %} | |
|
144 | 154 | <a |
|
145 | 155 | {% ifequal page current_page.number %} |
|
146 | 156 | class="current_page" |
@@ -157,6 +167,15 b'' | |||
|
157 | 167 | {% if not forloop.last %},{% endif %} |
|
158 | 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 | ">>></a> | |
|
160 | 179 | [<a href="rss/">RSS</a>] |
|
161 | 180 | </span> |
|
162 | 181 |
@@ -1,12 +1,13 b'' | |||
|
1 | 1 | # coding=utf-8 |
|
2 | 2 | import time |
|
3 | 3 | import logging |
|
4 | from django.core.paginator import Paginator | |
|
4 | 5 | |
|
5 | 6 | from django.test import TestCase |
|
6 | 7 | from django.test.client import Client |
|
7 | 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 | 11 | from boards import urls |
|
11 | 12 | from neboard import settings |
|
12 | 13 | |
@@ -111,7 +112,7 b' class PostTests(TestCase):' | |||
|
111 | 112 | self._create_post() |
|
112 | 113 | |
|
113 | 114 | self.assertEqual(settings.MAX_THREAD_COUNT, |
|
114 |
len( |
|
|
115 | len(Thread.objects.filter(archived=False))) | |
|
115 | 116 | |
|
116 | 117 | def test_pages(self): |
|
117 | 118 | """Test that the thread list is properly split into pages""" |
@@ -119,9 +120,11 b' class PostTests(TestCase):' | |||
|
119 | 120 | for i in range(settings.MAX_THREAD_COUNT): |
|
120 | 121 | self._create_post() |
|
121 | 122 | |
|
122 |
all_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 | 128 | first_post = posts_in_second_page[0] |
|
126 | 129 | |
|
127 | 130 | self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id, |
@@ -1,11 +1,11 b'' | |||
|
1 | 1 | import string |
|
2 | 2 | |
|
3 | from django.core.paginator import Paginator | |
|
4 | 3 | from django.core.urlresolvers import reverse |
|
5 | 4 | from django.db import transaction |
|
6 | 5 | from django.shortcuts import render, redirect |
|
7 | 6 | |
|
8 | 7 | from boards import utils |
|
8 | from boards.abstracts.paginator import get_paginator | |
|
9 | 9 | from boards.forms import ThreadForm, PlainErrorList |
|
10 | 10 | from boards.models import Post, Thread, Ban, Tag |
|
11 | 11 | from boards.views.banned import BannedView |
@@ -31,8 +31,9 b' class AllThreadsView(PostMixin, BaseBoar' | |||
|
31 | 31 | if not form: |
|
32 | 32 | form = ThreadForm(error_class=PlainErrorList) |
|
33 | 33 | |
|
34 |
paginator = |
|
|
34 | paginator = get_paginator(self.get_threads(), | |
|
35 | 35 | neboard.settings.THREADS_PER_PAGE) |
|
36 | paginator.current_page = int(page) | |
|
36 | 37 | |
|
37 | 38 | threads = paginator.page(page).object_list |
|
38 | 39 | |
@@ -44,8 +45,6 b' class AllThreadsView(PostMixin, BaseBoar' | |||
|
44 | 45 | return render(request, TEMPLATE, context) |
|
45 | 46 | |
|
46 | 47 | def post(self, request, page=DEFAULT_PAGE): |
|
47 | context = self.get_context_data(request=request) | |
|
48 | ||
|
49 | 48 | form = ThreadForm(request.POST, request.FILES, |
|
50 | 49 | error_class=PlainErrorList) |
|
51 | 50 | form.session = request.session |
General Comments 0
You need to be logged in to leave comments.
Login now