##// END OF EJS Templates
Dispatch thread method on post, not get
neko259 -
r1324:de75b1df default
parent child Browse files
Show More
@@ -1,163 +1,163 b''
1 import hashlib
1 import hashlib
2 from django.core.exceptions import ObjectDoesNotExist
2 from django.core.exceptions import ObjectDoesNotExist
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 from django.utils import timezone
6 from django.utils import timezone
7 from django.utils.dateformat import format
7 from django.utils.dateformat import format
8
8
9 from boards import utils, settings
9 from boards import utils, settings
10 from boards.abstracts.settingsmanager import get_settings_manager
10 from boards.abstracts.settingsmanager import get_settings_manager
11 from boards.forms import PostForm, PlainErrorList
11 from boards.forms import PostForm, PlainErrorList
12 from boards.models import Post
12 from boards.models import Post
13 from boards.views.base import BaseBoardView, CONTEXT_FORM
13 from boards.views.base import BaseBoardView, CONTEXT_FORM
14 from boards.views.mixins import DispatcherMixin
14 from boards.views.mixins import DispatcherMixin
15 from boards.views.posting_mixin import PostMixin
15 from boards.views.posting_mixin import PostMixin
16
16
17 import neboard
17 import neboard
18
18
19
19
20 CONTEXT_LASTUPDATE = "last_update"
20 CONTEXT_LASTUPDATE = "last_update"
21 CONTEXT_THREAD = 'thread'
21 CONTEXT_THREAD = 'thread'
22 CONTEXT_WS_TOKEN = 'ws_token'
22 CONTEXT_WS_TOKEN = 'ws_token'
23 CONTEXT_WS_PROJECT = 'ws_project'
23 CONTEXT_WS_PROJECT = 'ws_project'
24 CONTEXT_WS_HOST = 'ws_host'
24 CONTEXT_WS_HOST = 'ws_host'
25 CONTEXT_WS_PORT = 'ws_port'
25 CONTEXT_WS_PORT = 'ws_port'
26 CONTEXT_WS_TIME = 'ws_token_time'
26 CONTEXT_WS_TIME = 'ws_token_time'
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
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 FORM_THREADS = 'threads'
34 FORM_THREADS = 'threads'
35
35
36
36
37 class ThreadView(BaseBoardView, PostMixin, FormMixin, DispatcherMixin):
37 class ThreadView(BaseBoardView, PostMixin, FormMixin, DispatcherMixin):
38
38
39 def get(self, request, post_id, form: PostForm=None):
39 def get(self, request, post_id, form: PostForm=None):
40 try:
40 try:
41 opening_post = Post.objects.get(id=post_id)
41 opening_post = Post.objects.get(id=post_id)
42 except ObjectDoesNotExist:
42 except ObjectDoesNotExist:
43 raise Http404
43 raise Http404
44
44
45 if 'method' in request.POST:
46 self.dispatch_method(request, opening_post)
47
48 return redirect('thread', post_id) # FIXME Different for different modes
49
50 # If the tag is favorite, update the counter
45 # If the tag is favorite, update the counter
51 settings_manager = get_settings_manager(request)
46 settings_manager = get_settings_manager(request)
52 favorite = settings_manager.thread_is_fav(opening_post)
47 favorite = settings_manager.thread_is_fav(opening_post)
53 if favorite:
48 if favorite:
54 settings_manager.add_or_read_fav_thread(opening_post)
49 settings_manager.add_or_read_fav_thread(opening_post)
55
50
56 # If this is not OP, don't show it as it is
51 # If this is not OP, don't show it as it is
57 if not opening_post.is_opening():
52 if not opening_post.is_opening():
58 return redirect(opening_post.get_thread().get_opening_post()
53 return redirect(opening_post.get_thread().get_opening_post()
59 .get_absolute_url())
54 .get_absolute_url())
60
55
61 if not form:
56 if not form:
62 form = PostForm(error_class=PlainErrorList)
57 form = PostForm(error_class=PlainErrorList)
63
58
64 thread_to_show = opening_post.get_thread()
59 thread_to_show = opening_post.get_thread()
65
60
66 params = dict()
61 params = dict()
67
62
68 params[CONTEXT_FORM] = form
63 params[CONTEXT_FORM] = form
69 params[CONTEXT_LASTUPDATE] = str(thread_to_show.last_edit_time)
64 params[CONTEXT_LASTUPDATE] = str(thread_to_show.last_edit_time)
70 params[CONTEXT_THREAD] = thread_to_show
65 params[CONTEXT_THREAD] = thread_to_show
71 params[CONTEXT_MODE] = self.get_mode()
66 params[CONTEXT_MODE] = self.get_mode()
72 params[CONTEXT_OP] = opening_post
67 params[CONTEXT_OP] = opening_post
73 params[CONTEXT_FAVORITE] = favorite
68 params[CONTEXT_FAVORITE] = favorite
74
69
75 if settings.get_bool('External', 'WebsocketsEnabled'):
70 if settings.get_bool('External', 'WebsocketsEnabled'):
76 token_time = format(timezone.now(), u'U')
71 token_time = format(timezone.now(), u'U')
77
72
78 params[CONTEXT_WS_TIME] = token_time
73 params[CONTEXT_WS_TIME] = token_time
79 params[CONTEXT_WS_TOKEN] = utils.get_websocket_token(
74 params[CONTEXT_WS_TOKEN] = utils.get_websocket_token(
80 timestamp=token_time)
75 timestamp=token_time)
81 params[CONTEXT_WS_PROJECT] = neboard.settings.CENTRIFUGE_PROJECT_ID
76 params[CONTEXT_WS_PROJECT] = neboard.settings.CENTRIFUGE_PROJECT_ID
82 params[CONTEXT_WS_HOST] = request.get_host().split(':')[0]
77 params[CONTEXT_WS_HOST] = request.get_host().split(':')[0]
83 params[CONTEXT_WS_PORT] = neboard.settings.CENTRIFUGE_PORT
78 params[CONTEXT_WS_PORT] = neboard.settings.CENTRIFUGE_PORT
84
79
85 params.update(self.get_data(thread_to_show))
80 params.update(self.get_data(thread_to_show))
86
81
87 return render(request, self.get_template(), params)
82 return render(request, self.get_template(), params)
88
83
89 def post(self, request, post_id):
84 def post(self, request, post_id):
90 opening_post = get_object_or_404(Post, id=post_id)
85 opening_post = get_object_or_404(Post, id=post_id)
91
86
92 # If this is not OP, don't show it as it is
87 # If this is not OP, don't show it as it is
93 if not opening_post.is_opening():
88 if not opening_post.is_opening():
94 raise Http404
89 raise Http404
95
90
91 if 'method' in request.POST:
92 self.dispatch_method(request, opening_post)
93
94 return redirect('thread', post_id) # FIXME Different for different modes
95
96 if not opening_post.get_thread().archived:
96 if not opening_post.get_thread().archived:
97 form = PostForm(request.POST, request.FILES,
97 form = PostForm(request.POST, request.FILES,
98 error_class=PlainErrorList)
98 error_class=PlainErrorList)
99 form.session = request.session
99 form.session = request.session
100
100
101 if form.is_valid():
101 if form.is_valid():
102 return self.new_post(request, form, opening_post)
102 return self.new_post(request, form, opening_post)
103 if form.need_to_ban:
103 if form.need_to_ban:
104 # Ban user because he is suspected to be a bot
104 # Ban user because he is suspected to be a bot
105 self._ban_current_user(request)
105 self._ban_current_user(request)
106
106
107 return self.get(request, post_id, form)
107 return self.get(request, post_id, form)
108
108
109 def new_post(self, request, form: PostForm, opening_post: Post=None,
109 def new_post(self, request, form: PostForm, opening_post: Post=None,
110 html_response=True):
110 html_response=True):
111 """
111 """
112 Adds a new post (in thread or as a reply).
112 Adds a new post (in thread or as a reply).
113 """
113 """
114
114
115 ip = utils.get_client_ip(request)
115 ip = utils.get_client_ip(request)
116
116
117 data = form.cleaned_data
117 data = form.cleaned_data
118
118
119 title = form.get_title()
119 title = form.get_title()
120 text = data[FORM_TEXT]
120 text = data[FORM_TEXT]
121 file = form.get_file()
121 file = form.get_file()
122 threads = data[FORM_THREADS]
122 threads = data[FORM_THREADS]
123
123
124 text = self._remove_invalid_links(text)
124 text = self._remove_invalid_links(text)
125
125
126 post_thread = opening_post.get_thread()
126 post_thread = opening_post.get_thread()
127
127
128 post = Post.objects.create_post(title=title, text=text, file=file,
128 post = Post.objects.create_post(title=title, text=text, file=file,
129 thread=post_thread, ip=ip,
129 thread=post_thread, ip=ip,
130 opening_posts=threads,
130 opening_posts=threads,
131 tripcode=form.get_tripcode())
131 tripcode=form.get_tripcode())
132 post.notify_clients()
132 post.notify_clients()
133
133
134 if html_response:
134 if html_response:
135 if opening_post:
135 if opening_post:
136 return redirect(post.get_absolute_url())
136 return redirect(post.get_absolute_url())
137 else:
137 else:
138 return post
138 return post
139
139
140 def get_data(self, thread) -> dict:
140 def get_data(self, thread) -> dict:
141 """
141 """
142 Returns context params for the view.
142 Returns context params for the view.
143 """
143 """
144
144
145 return dict()
145 return dict()
146
146
147 def get_template(self) -> str:
147 def get_template(self) -> str:
148 """
148 """
149 Gets template to show the thread mode on.
149 Gets template to show the thread mode on.
150 """
150 """
151
151
152 pass
152 pass
153
153
154 def get_mode(self) -> str:
154 def get_mode(self) -> str:
155 pass
155 pass
156
156
157 def subscribe(self, request, opening_post):
157 def subscribe(self, request, opening_post):
158 settings_manager = get_settings_manager(request)
158 settings_manager = get_settings_manager(request)
159 settings_manager.add_or_read_fav_thread(opening_post)
159 settings_manager.add_or_read_fav_thread(opening_post)
160
160
161 def unsubscribe(self, request, opening_post):
161 def unsubscribe(self, request, opening_post):
162 settings_manager = get_settings_manager(request)
162 settings_manager = get_settings_manager(request)
163 settings_manager.del_fav_thread(opening_post)
163 settings_manager.del_fav_thread(opening_post)
General Comments 0
You need to be logged in to leave comments. Login now