Show More
@@ -1,142 +1,143 b'' | |||||
1 | import string |
|
|||
2 |
|
||||
3 |
|
|
1 | from django.db import transaction | |
4 | from django.shortcuts import render, redirect |
|
2 | from django.shortcuts import render, redirect | |
5 |
|
3 | |||
6 | from boards import utils, settings |
|
4 | from boards import utils, settings | |
7 | from boards.abstracts.paginator import get_paginator |
|
5 | from boards.abstracts.paginator import get_paginator | |
8 | from boards.abstracts.settingsmanager import get_settings_manager |
|
6 | from boards.abstracts.settingsmanager import get_settings_manager | |
9 | from boards.forms import ThreadForm, PlainErrorList |
|
7 | from boards.forms import ThreadForm, PlainErrorList | |
10 | from boards.models import Post, Thread, Ban, Tag |
|
8 | from boards.models import Post, Thread, Ban, Tag | |
11 | from boards.views.banned import BannedView |
|
9 | from boards.views.banned import BannedView | |
12 | from boards.views.base import BaseBoardView, CONTEXT_FORM |
|
10 | from boards.views.base import BaseBoardView, CONTEXT_FORM | |
13 | from boards.views.posting_mixin import PostMixin |
|
11 | from boards.views.posting_mixin import PostMixin | |
14 |
|
12 | |||
|
13 | ||||
15 | FORM_TAGS = 'tags' |
|
14 | FORM_TAGS = 'tags' | |
16 | FORM_TEXT = 'text' |
|
15 | FORM_TEXT = 'text' | |
17 | FORM_TITLE = 'title' |
|
16 | FORM_TITLE = 'title' | |
18 | FORM_IMAGE = 'image' |
|
17 | FORM_IMAGE = 'image' | |
19 |
|
18 | |||
20 | TAG_DELIMITER = ' ' |
|
19 | TAG_DELIMITER = ' ' | |
21 |
|
20 | |||
22 | PARAMETER_CURRENT_PAGE = 'current_page' |
|
21 | PARAMETER_CURRENT_PAGE = 'current_page' | |
23 | PARAMETER_PAGINATOR = 'paginator' |
|
22 | PARAMETER_PAGINATOR = 'paginator' | |
24 | PARAMETER_THREADS = 'threads' |
|
23 | PARAMETER_THREADS = 'threads' | |
25 |
|
24 | |||
26 | TEMPLATE = 'boards/posting_general.html' |
|
25 | TEMPLATE = 'boards/posting_general.html' | |
27 | DEFAULT_PAGE = 1 |
|
26 | DEFAULT_PAGE = 1 | |
28 |
|
27 | |||
29 |
|
28 | |||
30 | class AllThreadsView(PostMixin, BaseBoardView): |
|
29 | class AllThreadsView(PostMixin, BaseBoardView): | |
31 |
|
30 | |||
32 | def __init__(self): |
|
31 | def __init__(self): | |
33 | self.settings_manager = None |
|
32 | self.settings_manager = None | |
34 | super(AllThreadsView, self).__init__() |
|
33 | super(AllThreadsView, self).__init__() | |
35 |
|
34 | |||
36 | def get(self, request, page=DEFAULT_PAGE, form=None): |
|
35 | def get(self, request, page=DEFAULT_PAGE, form=None): | |
37 | context = self.get_context_data(request=request) |
|
36 | context = self.get_context_data(request=request) | |
38 |
|
37 | |||
39 | if not form: |
|
38 | if not form: | |
40 | form = ThreadForm(error_class=PlainErrorList) |
|
39 | form = ThreadForm(error_class=PlainErrorList) | |
41 |
|
40 | |||
42 | self.settings_manager = get_settings_manager(request) |
|
41 | self.settings_manager = get_settings_manager(request) | |
43 | paginator = get_paginator(self.get_threads(), |
|
42 | paginator = get_paginator(self.get_threads(), | |
44 | settings.THREADS_PER_PAGE) |
|
43 | settings.THREADS_PER_PAGE) | |
45 | paginator.current_page = int(page) |
|
44 | paginator.current_page = int(page) | |
46 |
|
45 | |||
47 | threads = paginator.page(page).object_list |
|
46 | threads = paginator.page(page).object_list | |
48 |
|
47 | |||
49 | context[PARAMETER_THREADS] = threads |
|
48 | context[PARAMETER_THREADS] = threads | |
50 | context[CONTEXT_FORM] = form |
|
49 | context[CONTEXT_FORM] = form | |
51 |
|
50 | |||
52 | self._get_page_context(paginator, context, page) |
|
51 | self._get_page_context(paginator, context, page) | |
53 |
|
52 | |||
54 | # TODO Use dict here |
|
53 | # TODO Use dict here | |
55 | return render(request, TEMPLATE, context_instance=context) |
|
54 | return render(request, TEMPLATE, context_instance=context) | |
56 |
|
55 | |||
57 | def post(self, request, page=DEFAULT_PAGE): |
|
56 | def post(self, request, page=DEFAULT_PAGE): | |
58 | form = ThreadForm(request.POST, request.FILES, |
|
57 | form = ThreadForm(request.POST, request.FILES, | |
59 | error_class=PlainErrorList) |
|
58 | error_class=PlainErrorList) | |
60 | form.session = request.session |
|
59 | form.session = request.session | |
61 |
|
60 | |||
62 | if form.is_valid(): |
|
61 | if form.is_valid(): | |
63 | return self.create_thread(request, form) |
|
62 | return self.create_thread(request, form) | |
64 | if form.need_to_ban: |
|
63 | if form.need_to_ban: | |
65 | # Ban user because he is suspected to be a bot |
|
64 | # Ban user because he is suspected to be a bot | |
66 | self._ban_current_user(request) |
|
65 | self._ban_current_user(request) | |
67 |
|
66 | |||
68 | return self.get(request, page, form) |
|
67 | return self.get(request, page, form) | |
69 |
|
68 | |||
70 | @staticmethod |
|
69 | @staticmethod | |
71 | def _get_page_context(paginator, context, page): |
|
70 | def _get_page_context(paginator, context, page): | |
72 | """ |
|
71 | """ | |
73 | Get pagination context variables |
|
72 | Get pagination context variables | |
74 | """ |
|
73 | """ | |
75 |
|
74 | |||
76 | context[PARAMETER_PAGINATOR] = paginator |
|
75 | context[PARAMETER_PAGINATOR] = paginator | |
77 | context[PARAMETER_CURRENT_PAGE] = paginator.page(int(page)) |
|
76 | context[PARAMETER_CURRENT_PAGE] = paginator.page(int(page)) | |
78 |
|
77 | |||
79 | @staticmethod |
|
78 | @staticmethod | |
80 | def parse_tags_string(tag_strings): |
|
79 | def parse_tags_string(tag_strings): | |
81 | """ |
|
80 | """ | |
82 | Parses tag list string and returns tag object list. |
|
81 | Parses tag list string and returns tag object list. | |
83 | """ |
|
82 | """ | |
84 |
|
83 | |||
85 | tags = [] |
|
84 | tags = [] | |
86 |
|
85 | |||
87 | if tag_strings: |
|
86 | if tag_strings: | |
88 | tag_strings = tag_strings.split(TAG_DELIMITER) |
|
87 | tag_strings = tag_strings.split(TAG_DELIMITER) | |
89 | for tag_name in tag_strings: |
|
88 | for tag_name in tag_strings: | |
90 | tag_name = tag_name.strip().lower() |
|
89 | tag_name = tag_name.strip().lower() | |
91 | if len(tag_name) > 0: |
|
90 | if len(tag_name) > 0: | |
92 | tag, created = Tag.objects.get_or_create(name=tag_name) |
|
91 | tag, created = Tag.objects.get_or_create(name=tag_name) | |
93 | tags.append(tag) |
|
92 | tags.append(tag) | |
94 |
|
93 | |||
95 | return tags |
|
94 | return tags | |
96 |
|
95 | |||
97 | @transaction.atomic |
|
96 | @transaction.atomic | |
98 | def create_thread(self, request, form, html_response=True): |
|
97 | def create_thread(self, request, form, html_response=True): | |
99 | """ |
|
98 | """ | |
100 | Creates a new thread with an opening post. |
|
99 | Creates a new thread with an opening post. | |
101 | """ |
|
100 | """ | |
102 |
|
101 | |||
103 | ip = utils.get_client_ip(request) |
|
102 | ip = utils.get_client_ip(request) | |
104 | is_banned = Ban.objects.filter(ip=ip).exists() |
|
103 | is_banned = Ban.objects.filter(ip=ip).exists() | |
105 |
|
104 | |||
106 | if is_banned: |
|
105 | if is_banned: | |
107 | if html_response: |
|
106 | if html_response: | |
108 | return redirect(BannedView().as_view()) |
|
107 | return redirect(BannedView().as_view()) | |
109 | else: |
|
108 | else: | |
110 | return |
|
109 | return | |
111 |
|
110 | |||
112 | data = form.cleaned_data |
|
111 | data = form.cleaned_data | |
113 |
|
112 | |||
114 | title = data[FORM_TITLE] |
|
113 | title = data[FORM_TITLE] | |
115 | text = data[FORM_TEXT] |
|
114 | text = data[FORM_TEXT] | |
116 |
|
115 | |||
117 | text = self._remove_invalid_links(text) |
|
116 | text = self._remove_invalid_links(text) | |
118 |
|
117 | |||
119 | if FORM_IMAGE in list(data.keys()): |
|
118 | if FORM_IMAGE in list(data.keys()): | |
120 | image = data[FORM_IMAGE] |
|
119 | image = data[FORM_IMAGE] | |
121 | else: |
|
120 | else: | |
122 | image = None |
|
121 | image = None | |
123 |
|
122 | |||
124 | tag_strings = data[FORM_TAGS] |
|
123 | tag_strings = data[FORM_TAGS] | |
125 |
|
124 | |||
126 | tags = self.parse_tags_string(tag_strings) |
|
125 | tags = self.parse_tags_string(tag_strings) | |
127 |
|
126 | |||
128 | post = Post.objects.create_post(title=title, text=text, image=image, |
|
127 | post = Post.objects.create_post(title=title, text=text, image=image, | |
129 | ip=ip, tags=tags) |
|
128 | ip=ip, tags=tags) | |
130 | # FIXME |
|
129 | ||
|
130 | # This is required to update the threads to which posts we have replied | |||
|
131 | # when creating this one | |||
131 | post.send_to_websocket(request) |
|
132 | post.send_to_websocket(request) | |
132 |
|
133 | |||
133 | if html_response: |
|
134 | if html_response: | |
134 | return redirect(post.get_url()) |
|
135 | return redirect(post.get_url()) | |
135 |
|
136 | |||
136 | def get_threads(self): |
|
137 | def get_threads(self): | |
137 | """ |
|
138 | """ | |
138 | Gets list of threads that will be shown on a page. |
|
139 | Gets list of threads that will be shown on a page. | |
139 | """ |
|
140 | """ | |
140 |
|
141 | |||
141 | return Thread.objects.all().order_by('-bump_time')\ |
|
142 | return Thread.objects.all().order_by('-bump_time')\ | |
142 | .exclude(tags__in=self.settings_manager.get_hidden_tags()) |
|
143 | .exclude(tags__in=self.settings_manager.get_hidden_tags()) |
@@ -1,35 +1,39 b'' | |||||
1 | from django.db import transaction |
|
1 | from django.db import transaction | |
2 | from django.template import RequestContext |
|
2 | from django.template import RequestContext | |
3 | from django.views.generic import View |
|
3 | from django.views.generic import View | |
4 |
|
4 | |||
5 | from boards import utils |
|
5 | from boards import utils | |
6 | from boards.models.user import Ban |
|
6 | from boards.models.user import Ban | |
7 |
|
7 | |||
8 |
|
8 | |||
9 | BAN_REASON_SPAM = 'Autoban: spam bot' |
|
9 | BAN_REASON_SPAM = 'Autoban: spam bot' | |
10 |
|
10 | |||
11 | CONTEXT_FORM = 'form' |
|
11 | CONTEXT_FORM = 'form' | |
12 |
|
12 | |||
13 |
|
13 | |||
14 | class BaseBoardView(View): |
|
14 | class BaseBoardView(View): | |
15 |
|
15 | |||
16 | def get_context_data(self, **kwargs): |
|
16 | def get_context_data(self, **kwargs): | |
|
17 | """ | |||
|
18 | This method is deprecated. You need to use dicts instead of context | |||
|
19 | instances in all places it is used now. | |||
|
20 | """ | |||
|
21 | ||||
17 | request = kwargs['request'] |
|
22 | request = kwargs['request'] | |
18 | # context = self._default_context(request) |
|
|||
19 | context = RequestContext(request) |
|
23 | context = RequestContext(request) | |
20 |
|
24 | |||
21 | return context |
|
25 | return context | |
22 |
|
26 | |||
23 | @transaction.atomic |
|
27 | @transaction.atomic | |
24 | def _ban_current_user(self, request): |
|
28 | def _ban_current_user(self, request): | |
25 | """ |
|
29 | """ | |
26 | Add current user to the IP ban list |
|
30 | Add current user to the IP ban list | |
27 | """ |
|
31 | """ | |
28 |
|
32 | |||
29 | ip = utils.get_client_ip(request) |
|
33 | ip = utils.get_client_ip(request) | |
30 | ban, created = Ban.objects.get_or_create(ip=ip) |
|
34 | ban, created = Ban.objects.get_or_create(ip=ip) | |
31 | if created: |
|
35 | if created: | |
32 | ban.can_read = False |
|
36 | ban.can_read = False | |
33 | ban.reason = BAN_REASON_SPAM |
|
37 | ban.reason = BAN_REASON_SPAM | |
34 | ban.save() |
|
38 | ban.save() | |
35 |
|
39 |
@@ -1,39 +1,40 b'' | |||||
|
1 | PARAM_NEXT = 'next' | |||
1 | PARAMETER_METHOD = 'method' |
|
2 | PARAMETER_METHOD = 'method' | |
2 |
|
3 | |||
3 | from django.shortcuts import redirect |
|
4 | from django.shortcuts import redirect | |
4 | from django.http import HttpResponseRedirect |
|
5 | from django.http import HttpResponseRedirect | |
5 |
|
6 | |||
6 |
|
7 | |||
7 | class RedirectNextMixin: |
|
8 | class RedirectNextMixin: | |
8 |
|
9 | |||
9 | def redirect_to_next(self, request): |
|
10 | def redirect_to_next(self, request): | |
10 | """ |
|
11 | """ | |
11 | If a 'next' parameter was specified, redirect to the next page. This |
|
12 | If a 'next' parameter was specified, redirect to the next page. This | |
12 | is used when the user is required to return to some page after the |
|
13 | is used when the user is required to return to some page after the | |
13 | current view has finished its work. |
|
14 | current view has finished its work. | |
14 | """ |
|
15 | """ | |
15 |
|
16 | |||
16 |
if |
|
17 | if PARAM_NEXT in request.GET: | |
17 |
next_page = request.GET[ |
|
18 | next_page = request.GET[PARAM_NEXT] | |
18 | return HttpResponseRedirect(next_page) |
|
19 | return HttpResponseRedirect(next_page) | |
19 | else: |
|
20 | else: | |
20 | return redirect('index') |
|
21 | return redirect('index') | |
21 |
|
22 | |||
22 |
|
23 | |||
23 | class DispatcherMixin: |
|
24 | class DispatcherMixin: | |
24 | """ |
|
25 | """ | |
25 | This class contains a dispather method that can run a method specified by |
|
26 | This class contains a dispather method that can run a method specified by | |
26 | 'method' request parameter. |
|
27 | 'method' request parameter. | |
27 | """ |
|
28 | """ | |
28 |
|
29 | |||
29 | def dispatch_method(self, *args, **kwargs): |
|
30 | def dispatch_method(self, *args, **kwargs): | |
30 | request = args[0] |
|
31 | request = args[0] | |
31 |
|
32 | |||
32 | method_name = None |
|
33 | method_name = None | |
33 | if PARAMETER_METHOD in request.GET: |
|
34 | if PARAMETER_METHOD in request.GET: | |
34 | method_name = request.GET[PARAMETER_METHOD] |
|
35 | method_name = request.GET[PARAMETER_METHOD] | |
35 | elif PARAMETER_METHOD in request.POST: |
|
36 | elif PARAMETER_METHOD in request.POST: | |
36 | method_name = request.POST[PARAMETER_METHOD] |
|
37 | method_name = request.POST[PARAMETER_METHOD] | |
37 |
|
38 | |||
38 | if method_name: |
|
39 | if method_name: | |
39 | return getattr(self, method_name)(*args, **kwargs) |
|
40 | return getattr(self, method_name)(*args, **kwargs) |
@@ -1,39 +1,41 b'' | |||||
1 | from django.db import transaction |
|
1 | from django.db import transaction | |
2 | from django.shortcuts import render, redirect |
|
2 | from django.shortcuts import render, redirect | |
3 |
|
3 | |||
4 | from boards.abstracts.settingsmanager import get_settings_manager |
|
4 | from boards.abstracts.settingsmanager import get_settings_manager | |
5 | from boards.views.base import BaseBoardView, CONTEXT_FORM |
|
5 | from boards.views.base import BaseBoardView, CONTEXT_FORM | |
6 | from boards.forms import SettingsForm, PlainErrorList |
|
6 | from boards.forms import SettingsForm, PlainErrorList | |
7 |
|
7 | |||
|
8 | FORM_THEME = 'theme' | |||
|
9 | ||||
8 | CONTEXT_HIDDEN_TAGS = 'hidden_tags' |
|
10 | CONTEXT_HIDDEN_TAGS = 'hidden_tags' | |
9 |
|
11 | |||
10 |
|
12 | |||
11 | class SettingsView(BaseBoardView): |
|
13 | class SettingsView(BaseBoardView): | |
12 |
|
14 | |||
13 | def get(self, request): |
|
15 | def get(self, request): | |
14 | context = self.get_context_data(request=request) |
|
16 | context = self.get_context_data(request=request) | |
15 | settings_manager = get_settings_manager(request) |
|
17 | settings_manager = get_settings_manager(request) | |
16 |
|
18 | |||
17 | selected_theme = settings_manager.get_theme() |
|
19 | selected_theme = settings_manager.get_theme() | |
18 |
|
20 | |||
19 |
form = SettingsForm(initial={ |
|
21 | form = SettingsForm(initial={FORM_THEME: selected_theme}, | |
20 | error_class=PlainErrorList) |
|
22 | error_class=PlainErrorList) | |
21 |
|
23 | |||
22 | context[CONTEXT_FORM] = form |
|
24 | context[CONTEXT_FORM] = form | |
23 | context[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags() |
|
25 | context[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags() | |
24 |
|
26 | |||
25 | # TODO Use dict here |
|
27 | # TODO Use dict here | |
26 | return render(request, 'boards/settings.html', context_instance=context) |
|
28 | return render(request, 'boards/settings.html', context_instance=context) | |
27 |
|
29 | |||
28 | def post(self, request): |
|
30 | def post(self, request): | |
29 | settings_manager = get_settings_manager(request) |
|
31 | settings_manager = get_settings_manager(request) | |
30 |
|
32 | |||
31 | with transaction.atomic(): |
|
33 | with transaction.atomic(): | |
32 | form = SettingsForm(request.POST, error_class=PlainErrorList) |
|
34 | form = SettingsForm(request.POST, error_class=PlainErrorList) | |
33 |
|
35 | |||
34 | if form.is_valid(): |
|
36 | if form.is_valid(): | |
35 |
selected_theme = form.cleaned_data[ |
|
37 | selected_theme = form.cleaned_data[FORM_THEME] | |
36 |
|
38 | |||
37 | settings_manager.set_theme(selected_theme) |
|
39 | settings_manager.set_theme(selected_theme) | |
38 |
|
40 | |||
39 | return redirect('settings') |
|
41 | return redirect('settings') |
@@ -1,92 +1,95 b'' | |||||
1 | from django.shortcuts import get_object_or_404 |
|
1 | from django.shortcuts import get_object_or_404 | |
2 |
|
2 | |||
3 | from boards.abstracts.settingsmanager import get_settings_manager |
|
3 | from boards.abstracts.settingsmanager import get_settings_manager | |
4 | from boards.models import Tag |
|
4 | from boards.models import Tag | |
5 | from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE |
|
5 | from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE | |
6 | from boards.views.mixins import DispatcherMixin, RedirectNextMixin |
|
6 | from boards.views.mixins import DispatcherMixin, RedirectNextMixin | |
7 | from boards.forms import ThreadForm, PlainErrorList |
|
7 | from boards.forms import ThreadForm, PlainErrorList | |
8 |
|
8 | |||
|
9 | PARAM_HIDDEN_TAGS = 'hidden_tags' | |||
|
10 | PARAM_FAV_TAGS = 'fav_tags' | |||
|
11 | PARAM_TAG = 'tag' | |||
9 |
|
12 | |||
10 | __author__ = 'neko259' |
|
13 | __author__ = 'neko259' | |
11 |
|
14 | |||
12 |
|
15 | |||
13 | class TagView(AllThreadsView, DispatcherMixin, RedirectNextMixin): |
|
16 | class TagView(AllThreadsView, DispatcherMixin, RedirectNextMixin): | |
14 |
|
17 | |||
15 | tag_name = None |
|
18 | tag_name = None | |
16 |
|
19 | |||
17 | def get_threads(self): |
|
20 | def get_threads(self): | |
18 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
21 | tag = get_object_or_404(Tag, name=self.tag_name) | |
19 |
|
22 | |||
20 | return tag.threads.all().order_by('-bump_time') |
|
23 | return tag.threads.all().order_by('-bump_time') | |
21 |
|
24 | |||
22 | def get_context_data(self, **kwargs): |
|
25 | def get_context_data(self, **kwargs): | |
23 | context = super(TagView, self).get_context_data(**kwargs) |
|
26 | context = super(TagView, self).get_context_data(**kwargs) | |
24 |
|
27 | |||
25 | settings_manager = get_settings_manager(kwargs['request']) |
|
28 | settings_manager = get_settings_manager(kwargs['request']) | |
26 |
|
29 | |||
27 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
30 | tag = get_object_or_404(Tag, name=self.tag_name) | |
28 |
context[ |
|
31 | context[PARAM_TAG] = tag | |
29 |
|
32 | |||
30 |
context[ |
|
33 | context[PARAM_FAV_TAGS] = settings_manager.get_fav_tags() | |
31 |
context[ |
|
34 | context[PARAM_HIDDEN_TAGS] = settings_manager.get_hidden_tags() | |
32 |
|
35 | |||
33 | return context |
|
36 | return context | |
34 |
|
37 | |||
35 | def get(self, request, tag_name, page=DEFAULT_PAGE, form=None): |
|
38 | def get(self, request, tag_name, page=DEFAULT_PAGE, form=None): | |
36 | self.tag_name = tag_name |
|
39 | self.tag_name = tag_name | |
37 |
|
40 | |||
38 | dispatch_result = self.dispatch_method(request) |
|
41 | dispatch_result = self.dispatch_method(request) | |
39 | if dispatch_result: |
|
42 | if dispatch_result: | |
40 | return dispatch_result |
|
43 | return dispatch_result | |
41 | else: |
|
44 | else: | |
42 | return super(TagView, self).get(request, page, form) |
|
45 | return super(TagView, self).get(request, page, form) | |
43 |
|
46 | |||
44 | def post(self, request, tag_name, page=DEFAULT_PAGE): |
|
47 | def post(self, request, tag_name, page=DEFAULT_PAGE): | |
45 | form = ThreadForm(request.POST, request.FILES, |
|
48 | form = ThreadForm(request.POST, request.FILES, | |
46 | error_class=PlainErrorList) |
|
49 | error_class=PlainErrorList) | |
47 | form.session = request.session |
|
50 | form.session = request.session | |
48 |
|
51 | |||
49 | if form.is_valid(): |
|
52 | if form.is_valid(): | |
50 | return self.create_thread(request, form) |
|
53 | return self.create_thread(request, form) | |
51 | if form.need_to_ban: |
|
54 | if form.need_to_ban: | |
52 | # Ban user because he is suspected to be a bot |
|
55 | # Ban user because he is suspected to be a bot | |
53 | self._ban_current_user(request) |
|
56 | self._ban_current_user(request) | |
54 |
|
57 | |||
55 | return self.get(request, tag_name, page, form) |
|
58 | return self.get(request, tag_name, page, form) | |
56 |
|
59 | |||
57 | def subscribe(self, request): |
|
60 | def subscribe(self, request): | |
58 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
61 | tag = get_object_or_404(Tag, name=self.tag_name) | |
59 |
|
62 | |||
60 | settings_manager = get_settings_manager(request) |
|
63 | settings_manager = get_settings_manager(request) | |
61 | settings_manager.add_fav_tag(tag) |
|
64 | settings_manager.add_fav_tag(tag) | |
62 |
|
65 | |||
63 | return self.redirect_to_next(request) |
|
66 | return self.redirect_to_next(request) | |
64 |
|
67 | |||
65 | def unsubscribe(self, request): |
|
68 | def unsubscribe(self, request): | |
66 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
69 | tag = get_object_or_404(Tag, name=self.tag_name) | |
67 |
|
70 | |||
68 | settings_manager = get_settings_manager(request) |
|
71 | settings_manager = get_settings_manager(request) | |
69 | settings_manager.del_fav_tag(tag) |
|
72 | settings_manager.del_fav_tag(tag) | |
70 |
|
73 | |||
71 | return self.redirect_to_next(request) |
|
74 | return self.redirect_to_next(request) | |
72 |
|
75 | |||
73 | def hide(self, request): |
|
76 | def hide(self, request): | |
74 | """ |
|
77 | """ | |
75 | Adds tag to user's hidden tags. Threads with this tag will not be |
|
78 | Adds tag to user's hidden tags. Threads with this tag will not be | |
76 | shown. |
|
79 | shown. | |
77 | """ |
|
80 | """ | |
78 |
|
81 | |||
79 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
82 | tag = get_object_or_404(Tag, name=self.tag_name) | |
80 |
|
83 | |||
81 | settings_manager = get_settings_manager(request) |
|
84 | settings_manager = get_settings_manager(request) | |
82 | settings_manager.add_hidden_tag(tag) |
|
85 | settings_manager.add_hidden_tag(tag) | |
83 |
|
86 | |||
84 | def unhide(self, request): |
|
87 | def unhide(self, request): | |
85 | """ |
|
88 | """ | |
86 | Removed tag from user's hidden tags. |
|
89 | Removed tag from user's hidden tags. | |
87 | """ |
|
90 | """ | |
88 |
|
91 | |||
89 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
92 | tag = get_object_or_404(Tag, name=self.tag_name) | |
90 |
|
93 | |||
91 | settings_manager = get_settings_manager(request) |
|
94 | settings_manager = get_settings_manager(request) | |
92 | settings_manager.del_hidden_tag(tag) |
|
95 | settings_manager.del_hidden_tag(tag) |
@@ -1,157 +1,150 b'' | |||||
1 | from django.core.urlresolvers import reverse |
|
1 | from django.core.urlresolvers import reverse | |
2 | from django.db import transaction |
|
2 | from django.db import transaction | |
3 | from django.http import Http404 |
|
3 | from django.http import Http404 | |
4 | from django.shortcuts import get_object_or_404, render, redirect |
|
4 | from django.shortcuts import get_object_or_404, render, redirect | |
5 | from django.views.generic.edit import FormMixin |
|
5 | from django.views.generic.edit import FormMixin | |
6 |
|
6 | |||
7 | from boards import utils, settings |
|
7 | from boards import utils, settings | |
8 | from boards.forms import PostForm, PlainErrorList |
|
8 | from boards.forms import PostForm, PlainErrorList | |
9 | from boards.models import Post, Ban |
|
9 | from boards.models import Post, Ban | |
10 | from boards.views.banned import BannedView |
|
10 | from boards.views.banned import BannedView | |
11 | from boards.views.base import BaseBoardView, CONTEXT_FORM |
|
11 | from boards.views.base import BaseBoardView, CONTEXT_FORM | |
12 | from boards.views.posting_mixin import PostMixin |
|
12 | from boards.views.posting_mixin import PostMixin | |
13 | import neboard |
|
13 | import neboard | |
14 |
|
14 | |||
15 | TEMPLATE_GALLERY = 'boards/thread_gallery.html' |
|
15 | TEMPLATE_GALLERY = 'boards/thread_gallery.html' | |
16 | TEMPLATE_NORMAL = 'boards/thread.html' |
|
16 | TEMPLATE_NORMAL = 'boards/thread.html' | |
17 |
|
17 | |||
18 | CONTEXT_POSTS = 'posts' |
|
18 | CONTEXT_POSTS = 'posts' | |
19 | CONTEXT_OP = 'opening_post' |
|
19 | CONTEXT_OP = 'opening_post' | |
20 | CONTEXT_BUMPLIMIT_PRG = 'bumplimit_progress' |
|
20 | CONTEXT_BUMPLIMIT_PRG = 'bumplimit_progress' | |
21 | CONTEXT_POSTS_LEFT = 'posts_left' |
|
21 | CONTEXT_POSTS_LEFT = 'posts_left' | |
22 | CONTEXT_LASTUPDATE = "last_update" |
|
22 | CONTEXT_LASTUPDATE = "last_update" | |
23 | CONTEXT_MAX_REPLIES = 'max_replies' |
|
23 | CONTEXT_MAX_REPLIES = 'max_replies' | |
24 | CONTEXT_THREAD = 'thread' |
|
24 | CONTEXT_THREAD = 'thread' | |
25 | CONTEXT_BUMPABLE = 'bumpable' |
|
25 | CONTEXT_BUMPABLE = 'bumpable' | |
26 | CONTEXT_WS_TOKEN = 'ws_token' |
|
26 | CONTEXT_WS_TOKEN = 'ws_token' | |
27 | CONTEXT_WS_PROJECT = 'ws_project' |
|
27 | CONTEXT_WS_PROJECT = 'ws_project' | |
28 | CONTEXT_WS_HOST = 'ws_host' |
|
28 | CONTEXT_WS_HOST = 'ws_host' | |
29 | CONTEXT_WS_PORT = 'ws_port' |
|
29 | CONTEXT_WS_PORT = 'ws_port' | |
30 |
|
30 | |||
31 | FORM_TITLE = 'title' |
|
31 | FORM_TITLE = 'title' | |
32 | FORM_TEXT = 'text' |
|
32 | FORM_TEXT = 'text' | |
33 | FORM_IMAGE = 'image' |
|
33 | FORM_IMAGE = 'image' | |
34 |
|
34 | |||
35 | MODE_GALLERY = 'gallery' |
|
35 | MODE_GALLERY = 'gallery' | |
36 | MODE_NORMAL = 'normal' |
|
36 | MODE_NORMAL = 'normal' | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | class ThreadView(BaseBoardView, PostMixin, FormMixin): |
|
39 | class ThreadView(BaseBoardView, PostMixin, FormMixin): | |
40 |
|
40 | |||
41 | def get(self, request, post_id, mode=MODE_NORMAL, form=None): |
|
41 | def get(self, request, post_id, mode=MODE_NORMAL, form=None): | |
42 | try: |
|
42 | try: | |
43 | opening_post = Post.objects.filter(id=post_id).only('thread_new')[0] |
|
43 | opening_post = Post.objects.filter(id=post_id).only('thread_new')[0] | |
44 | except IndexError: |
|
44 | except IndexError: | |
45 | raise Http404 |
|
45 | raise Http404 | |
46 |
|
46 | |||
47 | # If this is not OP, don't show it as it is |
|
47 | # If this is not OP, don't show it as it is | |
48 | if not opening_post or not opening_post.is_opening(): |
|
48 | if not opening_post or not opening_post.is_opening(): | |
49 | raise Http404 |
|
49 | raise Http404 | |
50 |
|
50 | |||
51 | if not form: |
|
51 | if not form: | |
52 | form = PostForm(error_class=PlainErrorList) |
|
52 | form = PostForm(error_class=PlainErrorList) | |
53 |
|
53 | |||
54 | thread_to_show = opening_post.get_thread() |
|
54 | thread_to_show = opening_post.get_thread() | |
55 |
|
55 | |||
56 | context = self.get_context_data(request=request) |
|
56 | context = self.get_context_data(request=request) | |
57 |
|
57 | |||
58 | context[CONTEXT_FORM] = form |
|
58 | context[CONTEXT_FORM] = form | |
59 | context[CONTEXT_LASTUPDATE] = str(utils.datetime_to_epoch( |
|
59 | context[CONTEXT_LASTUPDATE] = str(utils.datetime_to_epoch( | |
60 | thread_to_show.last_edit_time)) |
|
60 | thread_to_show.last_edit_time)) | |
61 | context[CONTEXT_THREAD] = thread_to_show |
|
61 | context[CONTEXT_THREAD] = thread_to_show | |
62 | context[CONTEXT_MAX_REPLIES] = settings.MAX_POSTS_PER_THREAD |
|
62 | context[CONTEXT_MAX_REPLIES] = settings.MAX_POSTS_PER_THREAD | |
63 |
|
63 | |||
64 | if settings.WEBSOCKETS_ENABLED: |
|
64 | if settings.WEBSOCKETS_ENABLED: | |
65 | context[CONTEXT_WS_TOKEN] = utils.get_websocket_token( |
|
65 | context[CONTEXT_WS_TOKEN] = utils.get_websocket_token( | |
66 | timestamp=context[CONTEXT_LASTUPDATE]) |
|
66 | timestamp=context[CONTEXT_LASTUPDATE]) | |
67 | context[CONTEXT_WS_PROJECT] = neboard.settings.CENTRIFUGE_PROJECT_ID |
|
67 | context[CONTEXT_WS_PROJECT] = neboard.settings.CENTRIFUGE_PROJECT_ID | |
68 | context[CONTEXT_WS_HOST] = request.get_host().split(':')[0] |
|
68 | context[CONTEXT_WS_HOST] = request.get_host().split(':')[0] | |
69 | context[CONTEXT_WS_PORT] = neboard.settings.CENTRIFUGE_PORT |
|
69 | context[CONTEXT_WS_PORT] = neboard.settings.CENTRIFUGE_PORT | |
70 |
|
70 | |||
71 | # TODO Move this to subclasses: NormalThreadView, GalleryThreadView etc |
|
71 | # TODO Move this to subclasses: NormalThreadView, GalleryThreadView etc | |
72 | if MODE_NORMAL == mode: |
|
72 | if MODE_NORMAL == mode: | |
73 | bumpable = thread_to_show.can_bump() |
|
73 | bumpable = thread_to_show.can_bump() | |
74 | context[CONTEXT_BUMPABLE] = bumpable |
|
74 | context[CONTEXT_BUMPABLE] = bumpable | |
75 | if bumpable: |
|
75 | if bumpable: | |
76 | left_posts = settings.MAX_POSTS_PER_THREAD \ |
|
76 | left_posts = settings.MAX_POSTS_PER_THREAD \ | |
77 | - thread_to_show.get_reply_count() |
|
77 | - thread_to_show.get_reply_count() | |
78 | context[CONTEXT_POSTS_LEFT] = left_posts |
|
78 | context[CONTEXT_POSTS_LEFT] = left_posts | |
79 | context[CONTEXT_BUMPLIMIT_PRG] = str( |
|
79 | context[CONTEXT_BUMPLIMIT_PRG] = str( | |
80 | float(left_posts) / settings.MAX_POSTS_PER_THREAD * 100) |
|
80 | float(left_posts) / settings.MAX_POSTS_PER_THREAD * 100) | |
81 |
|
81 | |||
82 | context[CONTEXT_OP] = opening_post |
|
82 | context[CONTEXT_OP] = opening_post | |
83 |
|
83 | |||
84 | document = TEMPLATE_NORMAL |
|
84 | document = TEMPLATE_NORMAL | |
85 | elif MODE_GALLERY == mode: |
|
85 | elif MODE_GALLERY == mode: | |
86 | context[CONTEXT_POSTS] = thread_to_show.get_replies_with_images( |
|
86 | context[CONTEXT_POSTS] = thread_to_show.get_replies_with_images( | |
87 | view_fields_only=True) |
|
87 | view_fields_only=True) | |
88 |
|
88 | |||
89 | document = TEMPLATE_GALLERY |
|
89 | document = TEMPLATE_GALLERY | |
90 | else: |
|
90 | else: | |
91 | raise Http404 |
|
91 | raise Http404 | |
92 |
|
92 | |||
93 | # TODO Use dict here |
|
93 | # TODO Use dict here | |
94 | return render(request, document, context_instance=context) |
|
94 | return render(request, document, context_instance=context) | |
95 |
|
95 | |||
96 | def post(self, request, post_id, mode=MODE_NORMAL): |
|
96 | def post(self, request, post_id, mode=MODE_NORMAL): | |
97 | opening_post = get_object_or_404(Post, id=post_id) |
|
97 | opening_post = get_object_or_404(Post, id=post_id) | |
98 |
|
98 | |||
99 | # If this is not OP, don't show it as it is |
|
99 | # If this is not OP, don't show it as it is | |
100 | if not opening_post.is_opening(): |
|
100 | if not opening_post.is_opening(): | |
101 | raise Http404 |
|
101 | raise Http404 | |
102 |
|
102 | |||
103 | if not opening_post.get_thread().archived: |
|
103 | if not opening_post.get_thread().archived: | |
104 | form = PostForm(request.POST, request.FILES, |
|
104 | form = PostForm(request.POST, request.FILES, | |
105 | error_class=PlainErrorList) |
|
105 | error_class=PlainErrorList) | |
106 | form.session = request.session |
|
106 | form.session = request.session | |
107 |
|
107 | |||
108 | if form.is_valid(): |
|
108 | if form.is_valid(): | |
109 | return self.new_post(request, form, opening_post) |
|
109 | return self.new_post(request, form, opening_post) | |
110 | if form.need_to_ban: |
|
110 | if form.need_to_ban: | |
111 | # Ban user because he is suspected to be a bot |
|
111 | # Ban user because he is suspected to be a bot | |
112 | self._ban_current_user(request) |
|
112 | self._ban_current_user(request) | |
113 |
|
113 | |||
114 | return self.get(request, post_id, mode, form) |
|
114 | return self.get(request, post_id, mode, form) | |
115 |
|
115 | |||
116 | @transaction.atomic |
|
116 | @transaction.atomic | |
117 | def new_post(self, request, form, opening_post=None, html_response=True): |
|
117 | def new_post(self, request, form, opening_post=None, html_response=True): | |
118 | """Add a new post (in thread or as a reply).""" |
|
118 | """Add a new post (in thread or as a reply).""" | |
119 |
|
119 | |||
120 | ip = utils.get_client_ip(request) |
|
120 | ip = utils.get_client_ip(request) | |
121 | is_banned = Ban.objects.filter(ip=ip).exists() |
|
|||
122 |
|
||||
123 | if is_banned: |
|
|||
124 | if html_response: |
|
|||
125 | return redirect(BannedView().as_view()) |
|
|||
126 | else: |
|
|||
127 | return None |
|
|||
128 |
|
121 | |||
129 | data = form.cleaned_data |
|
122 | data = form.cleaned_data | |
130 |
|
123 | |||
131 | title = data[FORM_TITLE] |
|
124 | title = data[FORM_TITLE] | |
132 | text = data[FORM_TEXT] |
|
125 | text = data[FORM_TEXT] | |
133 |
|
126 | |||
134 | text = self._remove_invalid_links(text) |
|
127 | text = self._remove_invalid_links(text) | |
135 |
|
128 | |||
136 | if FORM_IMAGE in list(data.keys()): |
|
129 | if FORM_IMAGE in list(data.keys()): | |
137 | image = data[FORM_IMAGE] |
|
130 | image = data[FORM_IMAGE] | |
138 | else: |
|
131 | else: | |
139 | image = None |
|
132 | image = None | |
140 |
|
133 | |||
141 | tags = [] |
|
134 | tags = [] | |
142 |
|
135 | |||
143 | post_thread = opening_post.get_thread() |
|
136 | post_thread = opening_post.get_thread() | |
144 |
|
137 | |||
145 | post = Post.objects.create_post(title=title, text=text, image=image, |
|
138 | post = Post.objects.create_post(title=title, text=text, image=image, | |
146 | thread=post_thread, ip=ip, tags=tags) |
|
139 | thread=post_thread, ip=ip, tags=tags) | |
147 | post.send_to_websocket(request, recursive=False) |
|
140 | post.send_to_websocket(request, recursive=False) | |
148 |
|
141 | |||
149 | thread_to_show = (opening_post.id if opening_post else post.id) |
|
142 | thread_to_show = (opening_post.id if opening_post else post.id) | |
150 |
|
143 | |||
151 | if html_response: |
|
144 | if html_response: | |
152 | if opening_post: |
|
145 | if opening_post: | |
153 | return redirect( |
|
146 | return redirect( | |
154 | reverse('thread', kwargs={'post_id': thread_to_show}) |
|
147 | reverse('thread', kwargs={'post_id': thread_to_show}) | |
155 | + '#' + str(post.id)) |
|
148 | + '#' + str(post.id)) | |
156 | else: |
|
149 | else: | |
157 | return post |
|
150 | return post |
General Comments 0
You need to be logged in to leave comments.
Login now