Show More
@@ -1,165 +1,165 b'' | |||||
1 | from django.contrib.auth.decorators import permission_required |
|
1 | from django.contrib.auth.decorators import permission_required | |
2 |
|
2 | |||
3 | from django.core.exceptions import ObjectDoesNotExist |
|
3 | from django.core.exceptions import ObjectDoesNotExist | |
4 | from django.core.urlresolvers import reverse |
|
4 | from django.core.urlresolvers import reverse | |
5 | from django.http import Http404 |
|
5 | from django.http import Http404 | |
6 | from django.shortcuts import get_object_or_404, render, redirect |
|
6 | from django.shortcuts import get_object_or_404, render, redirect | |
7 | from django.template.context_processors import csrf |
|
7 | from django.template.context_processors import csrf | |
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 | from django.views.generic.edit import FormMixin |
|
10 | from django.views.generic.edit import FormMixin | |
11 | from django.utils import timezone |
|
11 | from django.utils import timezone | |
12 | from django.utils.dateformat import format |
|
12 | from django.utils.dateformat import format | |
13 |
|
13 | |||
14 | from boards import utils, settings |
|
14 | from boards import utils, settings | |
15 | from boards.abstracts.settingsmanager import get_settings_manager |
|
15 | from boards.abstracts.settingsmanager import get_settings_manager | |
16 | from boards.forms import PostForm, PlainErrorList |
|
16 | from boards.forms import PostForm, PlainErrorList | |
17 | from boards.models import Post |
|
17 | from boards.models import Post | |
18 | from boards.views.base import BaseBoardView, CONTEXT_FORM |
|
18 | from boards.views.base import BaseBoardView, CONTEXT_FORM | |
19 | from boards.views.mixins import DispatcherMixin, PARAMETER_METHOD |
|
19 | from boards.views.mixins import DispatcherMixin, PARAMETER_METHOD | |
20 | from boards.views.posting_mixin import PostMixin |
|
20 | from boards.views.posting_mixin import PostMixin | |
21 | import neboard |
|
21 | import neboard | |
22 |
|
22 | |||
23 | REQ_POST_ID = 'post_id' |
|
23 | REQ_POST_ID = 'post_id' | |
24 |
|
24 | |||
25 | CONTEXT_LASTUPDATE = "last_update" |
|
25 | CONTEXT_LASTUPDATE = "last_update" | |
26 | CONTEXT_THREAD = 'thread' |
|
26 | CONTEXT_THREAD = 'thread' | |
27 | CONTEXT_MODE = 'mode' |
|
27 | CONTEXT_MODE = 'mode' | |
28 | CONTEXT_OP = 'opening_post' |
|
28 | CONTEXT_OP = 'opening_post' | |
29 | CONTEXT_FAVORITE = 'is_favorite' |
|
29 | CONTEXT_FAVORITE = 'is_favorite' | |
30 | CONTEXT_RSS_URL = 'rss_url' |
|
30 | CONTEXT_RSS_URL = 'rss_url' | |
31 |
|
31 | |||
32 | FORM_TITLE = 'title' |
|
32 | FORM_TITLE = 'title' | |
33 | FORM_TEXT = 'text' |
|
33 | FORM_TEXT = 'text' | |
34 | FORM_IMAGE = 'image' |
|
34 | FORM_IMAGE = 'image' | |
35 | FORM_THREADS = 'threads' |
|
35 | FORM_THREADS = 'threads' | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | class ThreadView(BaseBoardView, PostMixin, FormMixin, DispatcherMixin): |
|
38 | class ThreadView(BaseBoardView, PostMixin, FormMixin, DispatcherMixin): | |
39 |
|
39 | |||
40 | @method_decorator(csrf_protect) |
|
40 | @method_decorator(csrf_protect) | |
41 | def get(self, request, post_id, form: PostForm=None): |
|
41 | def get(self, request, post_id, form: PostForm=None): | |
42 | try: |
|
42 | try: | |
43 | opening_post = Post.objects.get(id=post_id) |
|
43 | opening_post = Post.objects.get(id=post_id) | |
44 | except ObjectDoesNotExist: |
|
44 | except ObjectDoesNotExist: | |
45 | raise Http404 |
|
45 | raise Http404 | |
46 |
|
46 | |||
47 | # If the tag is favorite, update the counter |
|
47 | # If the tag is favorite, update the counter | |
48 | settings_manager = get_settings_manager(request) |
|
48 | settings_manager = get_settings_manager(request) | |
49 | favorite = settings_manager.thread_is_fav(opening_post) |
|
49 | favorite = settings_manager.thread_is_fav(opening_post) | |
50 | if favorite: |
|
50 | if favorite: | |
51 | settings_manager.add_or_read_fav_thread(opening_post) |
|
51 | settings_manager.add_or_read_fav_thread(opening_post) | |
52 |
|
52 | |||
53 | # If this is not OP, don't show it as it is |
|
53 | # If this is not OP, don't show it as it is | |
54 | if not opening_post.is_opening(): |
|
54 | if not opening_post.is_opening(): | |
55 | return redirect(opening_post.get_thread().get_opening_post() |
|
55 | return redirect('{}#{}'.format(opening_post.get_thread().get_opening_post() | |
56 | .get_absolute_url()) |
|
56 | .get_absolute_url(), opening_post.id)) | |
57 |
|
57 | |||
58 | if not form: |
|
58 | if not form: | |
59 | form = PostForm(error_class=PlainErrorList) |
|
59 | form = PostForm(error_class=PlainErrorList) | |
60 |
|
60 | |||
61 | thread_to_show = opening_post.get_thread() |
|
61 | thread_to_show = opening_post.get_thread() | |
62 |
|
62 | |||
63 | params = dict() |
|
63 | params = dict() | |
64 |
|
64 | |||
65 | params[CONTEXT_FORM] = form |
|
65 | params[CONTEXT_FORM] = form | |
66 | params[CONTEXT_LASTUPDATE] = str(thread_to_show.last_edit_time) |
|
66 | params[CONTEXT_LASTUPDATE] = str(thread_to_show.last_edit_time) | |
67 | params[CONTEXT_THREAD] = thread_to_show |
|
67 | params[CONTEXT_THREAD] = thread_to_show | |
68 | params[CONTEXT_MODE] = self.get_mode() |
|
68 | params[CONTEXT_MODE] = self.get_mode() | |
69 | params[CONTEXT_OP] = opening_post |
|
69 | params[CONTEXT_OP] = opening_post | |
70 | params[CONTEXT_FAVORITE] = favorite |
|
70 | params[CONTEXT_FAVORITE] = favorite | |
71 | params[CONTEXT_RSS_URL] = self.get_rss_url(post_id) |
|
71 | params[CONTEXT_RSS_URL] = self.get_rss_url(post_id) | |
72 |
|
72 | |||
73 | params.update(self.get_data(thread_to_show)) |
|
73 | params.update(self.get_data(thread_to_show)) | |
74 |
|
74 | |||
75 | return render(request, self.get_template(), params) |
|
75 | return render(request, self.get_template(), params) | |
76 |
|
76 | |||
77 | @method_decorator(csrf_protect) |
|
77 | @method_decorator(csrf_protect) | |
78 | def post(self, request, post_id): |
|
78 | def post(self, request, post_id): | |
79 | opening_post = get_object_or_404(Post, id=post_id) |
|
79 | opening_post = get_object_or_404(Post, id=post_id) | |
80 |
|
80 | |||
81 | # If this is not OP, don't show it as it is |
|
81 | # If this is not OP, don't show it as it is | |
82 | if not opening_post.is_opening(): |
|
82 | if not opening_post.is_opening(): | |
83 | raise Http404 |
|
83 | raise Http404 | |
84 |
|
84 | |||
85 | if PARAMETER_METHOD in request.POST: |
|
85 | if PARAMETER_METHOD in request.POST: | |
86 | self.dispatch_method(request, opening_post) |
|
86 | self.dispatch_method(request, opening_post) | |
87 |
|
87 | |||
88 | return redirect('thread', post_id) # FIXME Different for different modes |
|
88 | return redirect('thread', post_id) # FIXME Different for different modes | |
89 |
|
89 | |||
90 | if not opening_post.get_thread().is_archived(): |
|
90 | if not opening_post.get_thread().is_archived(): | |
91 | form = PostForm(request.POST, request.FILES, |
|
91 | form = PostForm(request.POST, request.FILES, | |
92 | error_class=PlainErrorList) |
|
92 | error_class=PlainErrorList) | |
93 | form.session = request.session |
|
93 | form.session = request.session | |
94 |
|
94 | |||
95 | if form.is_valid(): |
|
95 | if form.is_valid(): | |
96 | return self.new_post(request, form, opening_post) |
|
96 | return self.new_post(request, form, opening_post) | |
97 | if form.need_to_ban: |
|
97 | if form.need_to_ban: | |
98 | # Ban user because he is suspected to be a bot |
|
98 | # Ban user because he is suspected to be a bot | |
99 | self._ban_current_user(request) |
|
99 | self._ban_current_user(request) | |
100 |
|
100 | |||
101 | return self.get(request, post_id, form) |
|
101 | return self.get(request, post_id, form) | |
102 |
|
102 | |||
103 | def new_post(self, request, form: PostForm, opening_post: Post=None, |
|
103 | def new_post(self, request, form: PostForm, opening_post: Post=None, | |
104 | html_response=True): |
|
104 | html_response=True): | |
105 | """ |
|
105 | """ | |
106 | Adds a new post (in thread or as a reply). |
|
106 | Adds a new post (in thread or as a reply). | |
107 | """ |
|
107 | """ | |
108 |
|
108 | |||
109 | ip = utils.get_client_ip(request) |
|
109 | ip = utils.get_client_ip(request) | |
110 |
|
110 | |||
111 | data = form.cleaned_data |
|
111 | data = form.cleaned_data | |
112 |
|
112 | |||
113 | title = form.get_title() |
|
113 | title = form.get_title() | |
114 | text = data[FORM_TEXT] |
|
114 | text = data[FORM_TEXT] | |
115 | files = form.get_files() |
|
115 | files = form.get_files() | |
116 | file_urls = form.get_file_urls() |
|
116 | file_urls = form.get_file_urls() | |
117 | images = form.get_images() |
|
117 | images = form.get_images() | |
118 |
|
118 | |||
119 | text = self._remove_invalid_links(text) |
|
119 | text = self._remove_invalid_links(text) | |
120 |
|
120 | |||
121 | post_thread = opening_post.get_thread() |
|
121 | post_thread = opening_post.get_thread() | |
122 |
|
122 | |||
123 | post = Post.objects.create_post(title=title, text=text, files=files, |
|
123 | post = Post.objects.create_post(title=title, text=text, files=files, | |
124 | thread=post_thread, ip=ip, |
|
124 | thread=post_thread, ip=ip, | |
125 | tripcode=form.get_tripcode(), |
|
125 | tripcode=form.get_tripcode(), | |
126 | images=images, file_urls=file_urls) |
|
126 | images=images, file_urls=file_urls) | |
127 |
|
127 | |||
128 | if form.is_subscribe(): |
|
128 | if form.is_subscribe(): | |
129 | settings_manager = get_settings_manager(request) |
|
129 | settings_manager = get_settings_manager(request) | |
130 | settings_manager.add_or_read_fav_thread( |
|
130 | settings_manager.add_or_read_fav_thread( | |
131 | post_thread.get_opening_post()) |
|
131 | post_thread.get_opening_post()) | |
132 |
|
132 | |||
133 | if html_response: |
|
133 | if html_response: | |
134 | if opening_post: |
|
134 | if opening_post: | |
135 | return redirect(post.get_absolute_url()) |
|
135 | return redirect(post.get_absolute_url()) | |
136 | else: |
|
136 | else: | |
137 | return post |
|
137 | return post | |
138 |
|
138 | |||
139 | def get_data(self, thread) -> dict: |
|
139 | def get_data(self, thread) -> dict: | |
140 | """ |
|
140 | """ | |
141 | Returns context params for the view. |
|
141 | Returns context params for the view. | |
142 | """ |
|
142 | """ | |
143 |
|
143 | |||
144 | return dict() |
|
144 | return dict() | |
145 |
|
145 | |||
146 | def get_template(self) -> str: |
|
146 | def get_template(self) -> str: | |
147 | """ |
|
147 | """ | |
148 | Gets template to show the thread mode on. |
|
148 | Gets template to show the thread mode on. | |
149 | """ |
|
149 | """ | |
150 |
|
150 | |||
151 | pass |
|
151 | pass | |
152 |
|
152 | |||
153 | def get_mode(self) -> str: |
|
153 | def get_mode(self) -> str: | |
154 | pass |
|
154 | pass | |
155 |
|
155 | |||
156 | def subscribe(self, request, opening_post): |
|
156 | def subscribe(self, request, opening_post): | |
157 | settings_manager = get_settings_manager(request) |
|
157 | settings_manager = get_settings_manager(request) | |
158 | settings_manager.add_or_read_fav_thread(opening_post) |
|
158 | settings_manager.add_or_read_fav_thread(opening_post) | |
159 |
|
159 | |||
160 | def unsubscribe(self, request, opening_post): |
|
160 | def unsubscribe(self, request, opening_post): | |
161 | settings_manager = get_settings_manager(request) |
|
161 | settings_manager = get_settings_manager(request) | |
162 | settings_manager.del_fav_thread(opening_post) |
|
162 | settings_manager.del_fav_thread(opening_post) | |
163 |
|
163 | |||
164 | def get_rss_url(self, opening_id): |
|
164 | def get_rss_url(self, opening_id): | |
165 | return reverse('thread', kwargs={'post_id': opening_id}) + 'rss/' |
|
165 | return reverse('thread', kwargs={'post_id': opening_id}) + 'rss/' |
General Comments 0
You need to be logged in to leave comments.
Login now