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