Show More
@@ -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, redirect | |
|
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 | 15 | from boards.settings import SECTION_VIEW |
|
16 | 16 | from boards.views.base import BaseBoardView, CONTEXT_FORM |
|
17 | 17 | from boards.views.mixins import PaginatedMixin, \ |
|
18 | 18 | DispatcherMixin, PARAMETER_METHOD |
|
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 | return self.get_reverse_url() | |
|
95 | return redirect(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)) |
General Comments 0
You need to be logged in to leave comments.
Login now