Show More
@@ -1,91 +1,93 b'' | |||
|
1 | from boards.abstracts.constants import PARAM_PAGE | |
|
2 | ||
|
1 | 3 | __author__ = 'neko259' |
|
2 | 4 | |
|
3 | 5 | from django.core.paginator import Paginator |
|
4 | 6 | |
|
5 | 7 | PAGINATOR_LOOKAROUND_SIZE = 2 |
|
6 | 8 | |
|
7 | 9 | |
|
8 | 10 | def get_paginator(*args, **kwargs): |
|
9 | 11 | return DividedPaginator(*args, **kwargs) |
|
10 | 12 | |
|
11 | 13 | |
|
12 | 14 | class DividedPaginator(Paginator): |
|
13 | 15 | |
|
14 | 16 | lookaround_size = PAGINATOR_LOOKAROUND_SIZE |
|
15 | 17 | |
|
16 | 18 | def __init__(self, object_list, per_page, orphans=0, |
|
17 | 19 | allow_empty_first_page=True, current_page=1): |
|
18 | 20 | super().__init__(object_list, per_page, orphans, allow_empty_first_page) |
|
19 | 21 | |
|
20 | 22 | self.link = None |
|
21 | 23 | self.params = None |
|
22 | 24 | self.current_page = current_page |
|
23 | 25 | |
|
24 | 26 | def _left_range(self): |
|
25 | 27 | return self.page_range[:self.lookaround_size] |
|
26 | 28 | |
|
27 | 29 | def _right_range(self): |
|
28 | 30 | pages = self.num_pages-self.lookaround_size |
|
29 | 31 | if pages <= 0: |
|
30 | 32 | return [] |
|
31 | 33 | else: |
|
32 | 34 | return self.page_range[pages:] |
|
33 | 35 | |
|
34 | 36 | def _center_range(self): |
|
35 | 37 | index = self.page_range.index(self.current_page) |
|
36 | 38 | |
|
37 | 39 | start = max(self.lookaround_size, index - self.lookaround_size) |
|
38 | 40 | end = min(self.num_pages - self.lookaround_size, index + self.lookaround_size + 1) |
|
39 | 41 | return self.page_range[start:end] |
|
40 | 42 | |
|
41 | 43 | def get_divided_range(self): |
|
42 | 44 | dr = list() |
|
43 | 45 | |
|
44 | 46 | dr += self._left_range() |
|
45 | 47 | dr += self._center_range() |
|
46 | 48 | dr += self._right_range() |
|
47 | 49 | |
|
48 | 50 | # Remove duplicates |
|
49 | 51 | dr = list(set(dr)) |
|
50 | 52 | dr.sort() |
|
51 | 53 | |
|
52 | 54 | return dr |
|
53 | 55 | |
|
54 | 56 | def get_dividers(self): |
|
55 | 57 | dividers = [] |
|
56 | 58 | |
|
57 | 59 | prev_page = 1 |
|
58 | 60 | for page in self.get_divided_range(): |
|
59 | 61 | if page - prev_page > 1: |
|
60 | 62 | dividers.append(page) |
|
61 | 63 | |
|
62 | 64 | # There can be no more than 2 dividers, so don't bother going |
|
63 | 65 | # further |
|
64 | 66 | if len(dividers) > 2: |
|
65 | 67 | break |
|
66 | 68 | prev_page = page |
|
67 | 69 | |
|
68 | 70 | return dividers |
|
69 | 71 | |
|
70 | 72 | def set_url(self, link, params): |
|
71 | 73 | self.link = link |
|
72 | 74 | self.params = params |
|
73 | 75 | |
|
74 | 76 | def get_page_url(self, page): |
|
75 |
self.params[ |
|
|
76 |
url_params = |
|
|
77 |
|
|
|
78 |
return self.link |
|
|
77 | self.params[PARAM_PAGE] = page | |
|
78 | url_params = '&'.join(['{}={}'.format(key, self.params[key]) | |
|
79 | for key in self.params.keys()]) | |
|
80 | return '{}?{}'.format(self.link, url_params) | |
|
79 | 81 | |
|
80 | 82 | def supports_urls(self): |
|
81 | 83 | return self.link is not None and self.params is not None |
|
82 | 84 | |
|
83 | 85 | def get_next_page_url(self): |
|
84 | 86 | current = self.page(self.current_page) |
|
85 | 87 | if current.has_next(): |
|
86 | 88 | return self.get_page_url(current.next_page_number()) |
|
87 | 89 | |
|
88 | 90 | def get_prev_page_url(self): |
|
89 | 91 | current = self.page(self.current_page) |
|
90 | 92 | if current.has_previous(): |
|
91 | return self.get_page_url(current.previous_page_number()) No newline at end of file | |
|
93 | return self.get_page_url(current.previous_page_number()) |
@@ -1,132 +1,132 b'' | |||
|
1 | 1 | from django.core.paginator import EmptyPage |
|
2 | 2 | from django.http import Http404 |
|
3 |
from django.shortcuts import render |
|
|
3 | from django.shortcuts import render | |
|
4 | 4 | from django.urls import reverse |
|
5 | 5 | from django.utils.decorators import method_decorator |
|
6 | 6 | from django.views.decorators.csrf import csrf_protect |
|
7 | 7 | |
|
8 | 8 | from boards import settings |
|
9 | 9 | from boards.abstracts.constants import PARAM_PAGE |
|
10 | 10 | from boards.abstracts.paginator import get_paginator |
|
11 | 11 | from boards.abstracts.settingsmanager import get_settings_manager, \ |
|
12 | 12 | SETTING_ONLY_FAVORITES |
|
13 | 13 | from boards.forms import ThreadForm, PlainErrorList |
|
14 | 14 | from boards.models import Post, Thread |
|
15 | from boards.settings import SECTION_VIEW | |
|
15 | 16 | from boards.views.base import BaseBoardView, CONTEXT_FORM |
|
16 |
from boards.views.mixins import |
|
|
17 | from boards.views.mixins import PaginatedMixin, \ | |
|
17 | 18 | DispatcherMixin, PARAMETER_METHOD |
|
18 | from boards.settings import SECTION_VIEW, SECTION_FORMS | |
|
19 | 19 | |
|
20 | 20 | ORDER_BUMP = 'bump' |
|
21 | 21 | |
|
22 | 22 | PARAM_ORDER = 'order' |
|
23 | 23 | |
|
24 | 24 | FORM_TAGS = 'tags' |
|
25 | 25 | FORM_TEXT = 'text' |
|
26 | 26 | FORM_TITLE = 'title' |
|
27 | 27 | FORM_IMAGE = 'image' |
|
28 | 28 | FORM_THREADS = 'threads' |
|
29 | 29 | |
|
30 | 30 | TAG_DELIMITER = ' ' |
|
31 | 31 | |
|
32 | 32 | PARAMETER_CURRENT_PAGE = 'current_page' |
|
33 | 33 | PARAMETER_PAGINATOR = 'paginator' |
|
34 | 34 | PARAMETER_THREADS = 'threads' |
|
35 | 35 | PARAMETER_ADDITIONAL = 'additional_params' |
|
36 | 36 | PARAMETER_RSS_URL = 'rss_url' |
|
37 | 37 | |
|
38 | 38 | TEMPLATE = 'boards/all_threads.html' |
|
39 | 39 | DEFAULT_PAGE = 1 |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | class AllThreadsView(BaseBoardView, PaginatedMixin, DispatcherMixin): |
|
43 | 43 | |
|
44 | 44 | tag_name = '' |
|
45 | 45 | |
|
46 | 46 | def __init__(self): |
|
47 | 47 | self.settings_manager = None |
|
48 | 48 | super(AllThreadsView, self).__init__() |
|
49 | 49 | |
|
50 | 50 | @method_decorator(csrf_protect) |
|
51 | 51 | def get(self, request, form: ThreadForm=None): |
|
52 | 52 | page = request.GET.get(PARAM_PAGE, DEFAULT_PAGE) |
|
53 | 53 | |
|
54 | 54 | params = self.get_context_data(request=request) |
|
55 | 55 | |
|
56 | 56 | if not form: |
|
57 | 57 | form = ThreadForm(error_class=PlainErrorList, |
|
58 | 58 | initial={FORM_TAGS: self.tag_name}) |
|
59 | 59 | |
|
60 | 60 | self.settings_manager = get_settings_manager(request) |
|
61 | 61 | |
|
62 | 62 | threads = self.get_threads() |
|
63 | 63 | |
|
64 | 64 | order = request.GET.get(PARAM_ORDER, ORDER_BUMP) |
|
65 | 65 | if order == ORDER_BUMP: |
|
66 | 66 | threads = threads.order_by('-bump_time') |
|
67 | 67 | else: |
|
68 | 68 | threads = threads.filter(replies__opening=True)\ |
|
69 | 69 | .order_by('-replies__pub_time') |
|
70 | 70 | threads = threads.distinct() |
|
71 | 71 | |
|
72 | 72 | paginator = get_paginator(threads, settings.get_int( |
|
73 | 73 | SECTION_VIEW, 'ThreadsPerPage')) |
|
74 | 74 | paginator.current_page = int(page) |
|
75 | 75 | |
|
76 | 76 | try: |
|
77 | 77 | threads = paginator.page(page).object_list |
|
78 | 78 | except EmptyPage: |
|
79 | 79 | raise Http404() |
|
80 | 80 | |
|
81 | 81 | params[PARAMETER_THREADS] = threads |
|
82 | 82 | params[CONTEXT_FORM] = form |
|
83 | 83 | params[PARAMETER_RSS_URL] = self.get_rss_url() |
|
84 | 84 | |
|
85 | 85 | paginator.set_url(self.get_reverse_url(), request.GET.dict()) |
|
86 | 86 | params.update(self.get_page_context(paginator, page)) |
|
87 | 87 | |
|
88 | 88 | return render(request, TEMPLATE, params) |
|
89 | 89 | |
|
90 | 90 | @method_decorator(csrf_protect) |
|
91 | 91 | def post(self, request): |
|
92 | 92 | if PARAMETER_METHOD in request.POST: |
|
93 | 93 | self.dispatch_method(request) |
|
94 | 94 | |
|
95 | 95 | return self.get_reverse_url() |
|
96 | 96 | |
|
97 | 97 | form = ThreadForm(request.POST, request.FILES, |
|
98 | 98 | error_class=PlainErrorList) |
|
99 | 99 | form.session = request.session |
|
100 | 100 | |
|
101 | 101 | if form.is_valid(): |
|
102 | 102 | return Post.objects.create_from_form(request, form, None) |
|
103 | 103 | if form.need_to_ban: |
|
104 | 104 | # Ban user because he is suspected to be a bot |
|
105 | 105 | self._ban_current_user(request) |
|
106 | 106 | |
|
107 | 107 | return self.get(request, form) |
|
108 | 108 | |
|
109 | 109 | def get_reverse_url(self): |
|
110 | 110 | return reverse('index') |
|
111 | 111 | |
|
112 | 112 | def get_threads(self): |
|
113 | 113 | """ |
|
114 | 114 | Gets list of threads that will be shown on a page. |
|
115 | 115 | """ |
|
116 | 116 | |
|
117 | 117 | threads = Thread.objects\ |
|
118 | 118 | .exclude(tags__in=self.settings_manager.get_hidden_tags()) |
|
119 | 119 | if self.settings_manager.get_setting(SETTING_ONLY_FAVORITES): |
|
120 | 120 | fav_tags = self.settings_manager.get_fav_tags() |
|
121 | 121 | if len(fav_tags) > 0: |
|
122 | 122 | threads = threads.filter(tags__in=fav_tags) |
|
123 | 123 | |
|
124 | 124 | return threads |
|
125 | 125 | |
|
126 | 126 | def get_rss_url(self): |
|
127 | 127 | return self.get_reverse_url() + 'rss/' |
|
128 | 128 | |
|
129 | 129 | def toggle_fav(self, request): |
|
130 | 130 | settings_manager = get_settings_manager(request) |
|
131 | 131 | settings_manager.set_setting(SETTING_ONLY_FAVORITES, |
|
132 | 132 | not settings_manager.get_setting(SETTING_ONLY_FAVORITES, False)) |
@@ -1,62 +1,53 b'' | |||
|
1 | import boards | |
|
2 | from boards.settings import SECTION_FORMS | |
|
3 | ||
|
4 | 1 |
|
|
5 | 2 | PARAMETER_METHOD = 'method' |
|
6 | 3 | |
|
7 | 4 | PARAMETER_CURRENT_PAGE = 'current_page' |
|
8 | 5 | PARAMETER_PAGINATOR = 'paginator' |
|
9 | 6 | |
|
10 | 7 | PARAMETER_PREV_LINK = 'prev_page_link' |
|
11 | 8 | PARAMETER_NEXT_LINK = 'next_page_link' |
|
12 | 9 | |
|
13 | 10 | |
|
14 | 11 | class DispatcherMixin: |
|
15 | 12 | """ |
|
16 | 13 | This class contains a dispather method that can run a method specified by |
|
17 | 14 | 'method' request parameter. |
|
18 | 15 | """ |
|
19 | 16 | |
|
20 | 17 | def __init__(self): |
|
21 | 18 | self.user = None |
|
22 | 19 | |
|
23 | 20 | def dispatch_method(self, *args, **kwargs): |
|
24 | 21 | request = args[0] |
|
25 | 22 | |
|
26 | 23 | self.user = request.user |
|
27 | 24 | |
|
28 | 25 | method_name = None |
|
29 | 26 | if PARAMETER_METHOD in request.GET: |
|
30 | 27 | method_name = request.GET[PARAMETER_METHOD] |
|
31 | 28 | elif PARAMETER_METHOD in request.POST: |
|
32 | 29 | method_name = request.POST[PARAMETER_METHOD] |
|
33 | 30 | |
|
34 | 31 | if method_name: |
|
35 | 32 | return getattr(self, method_name)(*args, **kwargs) |
|
36 | 33 | |
|
37 | 34 | |
|
38 | # TODO Check if it is still used anywhere and safely remove | |
|
39 | class FileUploadMixin: | |
|
40 | def get_max_upload_size(self): | |
|
41 | return boards.settings.get_int(SECTION_FORMS, 'MaxFileSize') | |
|
42 | ||
|
43 | ||
|
44 | 35 | class PaginatedMixin: |
|
45 | 36 | def get_page_context(self, paginator, page): |
|
46 | 37 | """ |
|
47 | 38 | Get pagination context variables |
|
48 | 39 | """ |
|
49 | 40 | |
|
50 | 41 | params = {} |
|
51 | 42 | |
|
52 | 43 | params[PARAMETER_PAGINATOR] = paginator |
|
53 | 44 | current_page = paginator.page(int(page)) |
|
54 | 45 | params[PARAMETER_CURRENT_PAGE] = current_page |
|
55 | 46 | if current_page.has_previous(): |
|
56 | 47 | params[PARAMETER_PREV_LINK] = paginator.get_page_url( |
|
57 | 48 | current_page.previous_page_number()) |
|
58 | 49 | if current_page.has_next(): |
|
59 | 50 | params[PARAMETER_NEXT_LINK] = paginator.get_page_url( |
|
60 | 51 | current_page.next_page_number()) |
|
61 | 52 | |
|
62 | 53 | return params |
@@ -1,33 +1,30 b'' | |||
|
1 | from boards import settings | |
|
2 | from boards.settings import SECTION_FORMS | |
|
3 | 1 |
|
|
4 | from boards.views.mixins import FileUploadMixin | |
|
5 | 2 | |
|
6 | 3 | TEMPLATE_NORMAL = 'boards/thread_normal.html' |
|
7 | 4 | |
|
8 | 5 | CONTEXT_BUMPLIMIT_PRG = 'bumplimit_progress' |
|
9 | 6 | CONTEXT_POSTS_LEFT = 'posts_left' |
|
10 | 7 | CONTEXT_BUMPABLE = 'bumpable' |
|
11 | 8 | |
|
12 | 9 | |
|
13 | 10 | class NormalThreadView(ThreadView): |
|
14 | 11 | |
|
15 | 12 | def get_template(self): |
|
16 | 13 | return TEMPLATE_NORMAL |
|
17 | 14 | |
|
18 | 15 | def get_mode(self): |
|
19 | 16 | return 'normal' |
|
20 | 17 | |
|
21 | 18 | def get_data(self, thread): |
|
22 | 19 | params = dict() |
|
23 | 20 | |
|
24 | 21 | bumpable = thread.can_bump() |
|
25 | 22 | params[CONTEXT_BUMPABLE] = bumpable |
|
26 | 23 | max_posts = thread.max_posts |
|
27 | 24 | if bumpable and thread.has_post_limit(): |
|
28 | 25 | left_posts = max_posts - thread.get_reply_count() |
|
29 | 26 | params[CONTEXT_POSTS_LEFT] = left_posts |
|
30 | 27 | params[CONTEXT_BUMPLIMIT_PRG] = str( |
|
31 | 28 | float(left_posts) / max_posts * 100) |
|
32 | 29 | |
|
33 | 30 | return params |
General Comments 0
You need to be logged in to leave comments.
Login now