Show More
@@ -1,87 +1,90 b'' | |||
|
1 | 1 | from boards.abstracts.settingsmanager import get_settings_manager, \ |
|
2 | 2 | SETTING_LAST_NOTIFICATION_ID, SETTING_IMAGE_VIEWER |
|
3 | 3 | from boards.models.user import Notification |
|
4 | from boards.models import Banner | |
|
4 | 5 | |
|
5 | 6 | __author__ = 'neko259' |
|
6 | 7 | |
|
7 | 8 | import neboard |
|
8 | 9 | from boards import settings |
|
9 | 10 | from boards.models import Post, Tag, Thread |
|
10 | 11 | |
|
11 | 12 | CONTEXT_SITE_NAME = 'site_name' |
|
12 | 13 | CONTEXT_VERSION = 'version' |
|
13 | 14 | CONTEXT_THEME_CSS = 'theme_css' |
|
14 | 15 | CONTEXT_THEME = 'theme' |
|
15 | 16 | CONTEXT_PPD = 'posts_per_day' |
|
16 | 17 | CONTEXT_TAGS = 'tags' |
|
17 | 18 | CONTEXT_USER = 'user' |
|
18 | 19 | CONTEXT_NEW_NOTIFICATIONS_COUNT = 'new_notifications_count' |
|
19 | 20 | CONTEXT_USERNAMES = 'usernames' |
|
20 | 21 | CONTEXT_TAGS_STR = 'tags_str' |
|
21 | 22 | CONTEXT_IMAGE_VIEWER = 'image_viewer' |
|
22 | 23 | CONTEXT_HAS_FAV_THREADS = 'has_fav_threads' |
|
23 | 24 | CONTEXT_POW_DIFFICULTY = 'pow_difficulty' |
|
24 | 25 | CONTEXT_NEW_POST_COUNT = 'new_post_count' |
|
25 | 26 | CONTEXT_SEARCH_ENABLED = 'search_enabled' |
|
27 | CONTEXT_BANNERS = 'banners' | |
|
26 | 28 | |
|
27 | 29 | |
|
28 | 30 | def get_notifications(context, request): |
|
29 | 31 | settings_manager = get_settings_manager(request) |
|
30 | 32 | usernames = settings_manager.get_notification_usernames() |
|
31 | 33 | new_notifications_count = 0 |
|
32 | 34 | if usernames is not None: |
|
33 | 35 | last_notification_id = settings_manager.get_setting( |
|
34 | 36 | SETTING_LAST_NOTIFICATION_ID) |
|
35 | 37 | |
|
36 | 38 | new_notifications_count = Notification.objects.get_notification_posts( |
|
37 | 39 | usernames=usernames, last=last_notification_id).only('id').count() |
|
38 | 40 | context[CONTEXT_NEW_NOTIFICATIONS_COUNT] = new_notifications_count |
|
39 | 41 | context[CONTEXT_USERNAMES] = usernames |
|
40 | 42 | |
|
41 | 43 | |
|
42 | 44 | def get_new_post_count(context, request): |
|
43 | 45 | settings_manager = get_settings_manager(request) |
|
44 | 46 | fav_threads = settings_manager.get_fav_threads() |
|
45 | 47 | fav_thread_ops = Post.objects.filter(id__in=fav_threads.keys()) \ |
|
46 | 48 | .order_by('-pub_time').only('thread_id', 'pub_time') |
|
47 | 49 | |
|
48 | 50 | ops = [{'op': op, 'last_id': fav_threads[str(op.id)]} for op in fav_thread_ops] |
|
49 | 51 | count = Thread.objects.get_new_post_count(ops) |
|
50 | 52 | if count > 0: |
|
51 | 53 | context[CONTEXT_NEW_POST_COUNT] = '(+{})'.format(count) |
|
52 | 54 | |
|
53 | 55 | |
|
54 | 56 | def user_and_ui_processor(request): |
|
55 | 57 | context = dict() |
|
56 | 58 | |
|
57 | 59 | context[CONTEXT_PPD] = float(Post.objects.get_posts_per_day()) |
|
58 | 60 | |
|
59 | 61 | settings_manager = get_settings_manager(request) |
|
60 | 62 | fav_tags = settings_manager.get_fav_tags() |
|
61 | 63 | context[CONTEXT_TAGS] = fav_tags |
|
62 | 64 | |
|
63 | 65 | context[CONTEXT_TAGS_STR] = Tag.objects.get_tag_url_list(fav_tags) |
|
64 | 66 | theme = settings_manager.get_theme() |
|
65 | 67 | context[CONTEXT_THEME] = theme |
|
66 | 68 | context[CONTEXT_THEME_CSS] = 'css/' + theme + '/base_page.css' |
|
67 | 69 | |
|
68 | 70 | context[CONTEXT_VERSION] = settings.get('Version', 'Version') |
|
69 | 71 | context[CONTEXT_SITE_NAME] = settings.get('Version', 'SiteName') |
|
70 | 72 | |
|
71 | 73 | if (settings.get_bool('Forms', 'LimitFirstPosting') and not settings_manager.get_setting('confirmed_user'))\ |
|
72 | 74 | or settings.get_bool('Forms', 'LimitPostingSpeed'): |
|
73 | 75 | context[CONTEXT_POW_DIFFICULTY] = settings.get_int('Forms', 'PowDifficulty') |
|
74 | 76 | |
|
75 | 77 | context[CONTEXT_IMAGE_VIEWER] = settings_manager.get_setting( |
|
76 | 78 | SETTING_IMAGE_VIEWER, |
|
77 | 79 | default=settings.get('View', 'DefaultImageViewer')) |
|
78 | 80 | |
|
79 | 81 | context[CONTEXT_HAS_FAV_THREADS] =\ |
|
80 | 82 | len(settings_manager.get_fav_threads()) > 0 |
|
81 | 83 | |
|
82 | 84 | context[CONTEXT_SEARCH_ENABLED] = 'haystack' in neboard.settings.INSTALLED_APPS |
|
85 | context[CONTEXT_BANNERS] = Banner.objects.order_by('-id') | |
|
83 | 86 | |
|
84 | 87 | get_notifications(context, request) |
|
85 | 88 | get_new_post_count(context, request) |
|
86 | 89 | |
|
87 | 90 | return context |
@@ -1,172 +1,170 b'' | |||
|
1 | 1 | from django.core.urlresolvers import reverse |
|
2 | 2 | from django.core.files import File |
|
3 | 3 | from django.core.files.temp import NamedTemporaryFile |
|
4 | 4 | from django.core.paginator import EmptyPage |
|
5 | 5 | from django.db import transaction |
|
6 | 6 | from django.http import Http404 |
|
7 | 7 | from django.shortcuts import render, redirect |
|
8 | 8 | from django.utils.decorators import method_decorator |
|
9 | 9 | from django.views.decorators.csrf import csrf_protect |
|
10 | 10 | |
|
11 | 11 | from boards import utils, settings |
|
12 | 12 | from boards.abstracts.paginator import get_paginator |
|
13 | 13 | from boards.abstracts.settingsmanager import get_settings_manager |
|
14 | 14 | from boards.forms import ThreadForm, PlainErrorList |
|
15 |
from boards.models import Post, Thread, Ban |
|
|
15 | from boards.models import Post, Thread, Ban | |
|
16 | 16 | from boards.views.banned import BannedView |
|
17 | 17 | from boards.views.base import BaseBoardView, CONTEXT_FORM |
|
18 | 18 | from boards.views.posting_mixin import PostMixin |
|
19 | 19 | from boards.views.mixins import FileUploadMixin, PaginatedMixin |
|
20 | 20 | |
|
21 | 21 | FORM_TAGS = 'tags' |
|
22 | 22 | FORM_TEXT = 'text' |
|
23 | 23 | FORM_TITLE = 'title' |
|
24 | 24 | FORM_IMAGE = 'image' |
|
25 | 25 | FORM_THREADS = 'threads' |
|
26 | 26 | |
|
27 | 27 | TAG_DELIMITER = ' ' |
|
28 | 28 | |
|
29 | 29 | PARAMETER_CURRENT_PAGE = 'current_page' |
|
30 | 30 | PARAMETER_PAGINATOR = 'paginator' |
|
31 | 31 | PARAMETER_THREADS = 'threads' |
|
32 | PARAMETER_BANNERS = 'banners' | |
|
33 | 32 | PARAMETER_ADDITIONAL = 'additional_params' |
|
34 | 33 | PARAMETER_MAX_FILE_SIZE = 'max_file_size' |
|
35 | 34 | PARAMETER_RSS_URL = 'rss_url' |
|
36 | 35 | |
|
37 | 36 | TEMPLATE = 'boards/all_threads.html' |
|
38 | 37 | DEFAULT_PAGE = 1 |
|
39 | 38 | |
|
40 | 39 | |
|
41 | 40 | class AllThreadsView(PostMixin, FileUploadMixin, BaseBoardView, PaginatedMixin): |
|
42 | 41 | |
|
43 | 42 | def __init__(self): |
|
44 | 43 | self.settings_manager = None |
|
45 | 44 | super(AllThreadsView, self).__init__() |
|
46 | 45 | |
|
47 | 46 | @method_decorator(csrf_protect) |
|
48 | 47 | def get(self, request, form: ThreadForm=None): |
|
49 | 48 | page = request.GET.get('page', DEFAULT_PAGE) |
|
50 | 49 | |
|
51 | 50 | params = self.get_context_data(request=request) |
|
52 | 51 | |
|
53 | 52 | if not form: |
|
54 | 53 | form = ThreadForm(error_class=PlainErrorList) |
|
55 | 54 | |
|
56 | 55 | self.settings_manager = get_settings_manager(request) |
|
57 | 56 | |
|
58 | 57 | threads = self.get_threads() |
|
59 | 58 | |
|
60 | 59 | order = request.GET.get('order', 'bump') |
|
61 | 60 | if order == 'bump': |
|
62 | 61 | threads = threads.order_by('-bump_time') |
|
63 | 62 | else: |
|
64 | 63 | threads = threads.filter(multi_replies__opening=True).order_by('-multi_replies__pub_time') |
|
65 | 64 | filter = request.GET.get('filter') |
|
66 | 65 | if filter == 'fav_tags': |
|
67 | 66 | fav_tags = self.settings_manager.get_fav_tags() |
|
68 | 67 | if len(fav_tags) > 0: |
|
69 | 68 | threads = threads.filter(tags__in=fav_tags) |
|
70 | 69 | threads = threads.distinct() |
|
71 | 70 | |
|
72 | 71 | paginator = get_paginator(threads, |
|
73 | 72 | settings.get_int('View', 'ThreadsPerPage')) |
|
74 | 73 | paginator.current_page = int(page) |
|
75 | 74 | |
|
76 | 75 | try: |
|
77 | 76 | threads = paginator.page(page).object_list |
|
78 | 77 | except EmptyPage: |
|
79 | 78 | raise Http404() |
|
80 | 79 | |
|
81 | 80 | params[PARAMETER_THREADS] = threads |
|
82 | 81 | params[CONTEXT_FORM] = form |
|
83 | params[PARAMETER_BANNERS] = Banner.objects.order_by('-id').all() | |
|
84 | 82 | params[PARAMETER_MAX_FILE_SIZE] = self.get_max_upload_size() |
|
85 | 83 | params[PARAMETER_RSS_URL] = self.get_rss_url() |
|
86 | 84 | |
|
87 | 85 | paginator.set_url(self.get_reverse_url(), request.GET.dict()) |
|
88 | 86 | self.get_page_context(paginator, params, page) |
|
89 | 87 | |
|
90 | 88 | return render(request, TEMPLATE, params) |
|
91 | 89 | |
|
92 | 90 | @method_decorator(csrf_protect) |
|
93 | 91 | def post(self, request): |
|
94 | 92 | form = ThreadForm(request.POST, request.FILES, |
|
95 | 93 | error_class=PlainErrorList) |
|
96 | 94 | form.session = request.session |
|
97 | 95 | |
|
98 | 96 | if form.is_valid(): |
|
99 | 97 | return self.create_thread(request, form) |
|
100 | 98 | if form.need_to_ban: |
|
101 | 99 | # Ban user because he is suspected to be a bot |
|
102 | 100 | self._ban_current_user(request) |
|
103 | 101 | |
|
104 | 102 | return self.get(request, form) |
|
105 | 103 | |
|
106 | 104 | def get_page_context(self, paginator, params, page): |
|
107 | 105 | """ |
|
108 | 106 | Get pagination context variables |
|
109 | 107 | """ |
|
110 | 108 | |
|
111 | 109 | params[PARAMETER_PAGINATOR] = paginator |
|
112 | 110 | current_page = paginator.page(int(page)) |
|
113 | 111 | params[PARAMETER_CURRENT_PAGE] = current_page |
|
114 | 112 | self.set_page_urls(paginator, params) |
|
115 | 113 | |
|
116 | 114 | def get_reverse_url(self): |
|
117 | 115 | return reverse('index') |
|
118 | 116 | |
|
119 | 117 | @transaction.atomic |
|
120 | 118 | def create_thread(self, request, form: ThreadForm, html_response=True): |
|
121 | 119 | """ |
|
122 | 120 | Creates a new thread with an opening post. |
|
123 | 121 | """ |
|
124 | 122 | |
|
125 | 123 | ip = utils.get_client_ip(request) |
|
126 | 124 | is_banned = Ban.objects.filter(ip=ip).exists() |
|
127 | 125 | |
|
128 | 126 | if is_banned: |
|
129 | 127 | if html_response: |
|
130 | 128 | return redirect(BannedView().as_view()) |
|
131 | 129 | else: |
|
132 | 130 | return |
|
133 | 131 | |
|
134 | 132 | data = form.cleaned_data |
|
135 | 133 | |
|
136 | 134 | title = form.get_title() |
|
137 | 135 | text = data[FORM_TEXT] |
|
138 | 136 | file = form.get_file() |
|
139 | 137 | threads = data[FORM_THREADS] |
|
140 | 138 | images = form.get_images() |
|
141 | 139 | |
|
142 | 140 | text = self._remove_invalid_links(text) |
|
143 | 141 | |
|
144 | 142 | tags = data[FORM_TAGS] |
|
145 | 143 | monochrome = form.is_monochrome() |
|
146 | 144 | |
|
147 | 145 | post = Post.objects.create_post(title=title, text=text, file=file, |
|
148 | 146 | ip=ip, tags=tags, opening_posts=threads, |
|
149 | 147 | tripcode=form.get_tripcode(), |
|
150 | 148 | monochrome=monochrome, images=images) |
|
151 | 149 | |
|
152 | 150 | # This is required to update the threads to which posts we have replied |
|
153 | 151 | # when creating this one |
|
154 | 152 | post.notify_clients() |
|
155 | 153 | |
|
156 | 154 | if form.is_subscribe(): |
|
157 | 155 | settings_manager = get_settings_manager(request) |
|
158 | 156 | settings_manager.add_or_read_fav_thread(post) |
|
159 | 157 | |
|
160 | 158 | if html_response: |
|
161 | 159 | return redirect(post.get_absolute_url()) |
|
162 | 160 | |
|
163 | 161 | def get_threads(self): |
|
164 | 162 | """ |
|
165 | 163 | Gets list of threads that will be shown on a page. |
|
166 | 164 | """ |
|
167 | 165 | |
|
168 | 166 | return Thread.objects\ |
|
169 | 167 | .exclude(tags__in=self.settings_manager.get_hidden_tags()) |
|
170 | 168 | |
|
171 | 169 | def get_rss_url(self): |
|
172 | 170 | return self.get_reverse_url() + 'rss/' |
General Comments 0
You need to be logged in to leave comments.
Login now