##// END OF EJS Templates
AJAX-based thread creation
neko259 -
r1998:8255ca3c default
parent child Browse files
Show More
@@ -2,6 +2,8 b" var ITEM_FILE_SOURCE = 'fileSource';"
2 var URL_STICKERS = '/api/stickers';
2 var URL_STICKERS = '/api/stickers';
3 var MIN_INPUT_LENGTH = 3;
3 var MIN_INPUT_LENGTH = 3;
4 var URL_DELIMITER = '\n';
4 var URL_DELIMITER = '\n';
5 // TODO This needs to be the same for attachment download time limit.
6 var POST_AJAX_TIMEOUT = 30000;
5
7
6 var pastedImages = [];
8 var pastedImages = [];
7
9
@@ -128,6 +130,74 b' function addOnImagePaste() {'
128 });
130 });
129 }
131 }
130
132
133 /**
134 * When the form is posted, this method will be run as a callback
135 */
136 function updateOnPost(response, statusText, xhr, form) {
137 var json = $.parseJSON(response);
138 var status = json.status;
139 var url = json.url;
140
141 showAsErrors(form, '');
142 $('.post-form-w').unblock();
143
144 if (status === 'ok') {
145 if (url) {
146 document.location = url;
147 } else {
148 resetForm();
149 getThreadDiff();
150 scrollToBottom();
151 }
152 } else {
153 var errors = json.errors;
154 for (var i = 0; i < errors.length; i++) {
155 var fieldErrors = errors[i];
156
157 var error = fieldErrors.errors;
158
159 showAsErrors(form, error);
160 }
161 }
162 }
163
164 function initAjaxForm(openingPostId) {
165 var form = $('#form');
166
167 var url = '/api/add_post/';
168 if (openingPostId) {
169 url += openingPostId + '/';
170 }
171
172 if (form.length > 0) {
173 var options = {
174 beforeSubmit: function(arr, form, options) {
175 $('.post-form-w').block({ message: gettext('Sending message...') });
176
177 $.each(pastedImages, function(i, blob) {
178 arr.push({
179 name: "file_0",
180 value: blob
181 });
182 });
183 },
184 success: updateOnPost,
185 error: function(xhr, textStatus, errorString) {
186 var errorText = gettext('Server error: ') + textStatus;
187 if (errorString) {
188 errorText += ' / ' + errorString;
189 }
190 showAsErrors(form, errorText);
191 $('.post-form-w').unblock();
192 },
193 url: url,
194 timeout: POST_AJAX_TIMEOUT
195 };
196
197 form.ajaxForm(options);
198 }
199 }
200
131 $(document).ready(function() {
201 $(document).ready(function() {
132 var powDifficulty = parseInt($('body').attr('data-pow-difficulty'));
202 var powDifficulty = parseInt($('body').attr('data-pow-difficulty'));
133 if (powDifficulty > 0 && typeof SharedWorker != 'undefined') {
203 if (powDifficulty > 0 && typeof SharedWorker != 'undefined') {
@@ -40,4 +40,6 b' function extractLast( term ) {'
40 return false;
40 return false;
41 }
41 }
42 });
42 });
43
44 initAjaxForm(null);
43 }); No newline at end of file
45 });
@@ -30,8 +30,6 b' var POST_UPDATED = 1;'
30
30
31 // TODO These need to be syncronized with board settings.
31 // TODO These need to be syncronized with board settings.
32 var JS_AUTOUPDATE_PERIOD = 20000;
32 var JS_AUTOUPDATE_PERIOD = 20000;
33 // TODO This needs to be the same for attachment download time limit.
34 var POST_AJAX_TIMEOUT = 30000;
35 var BLINK_SPEED = 500;
33 var BLINK_SPEED = 500;
36
34
37 var ALLOWED_FOR_PARTIAL_UPDATE = [
35 var ALLOWED_FOR_PARTIAL_UPDATE = [
@@ -264,34 +262,6 b' function showNewPostsTitle(newPostCount)'
264 }
262 }
265 }
263 }
266
264
267
268 /**
269 * When the form is posted, this method will be run as a callback
270 */
271 function updateOnPost(response, statusText, xhr, form) {
272 var json = $.parseJSON(response);
273 var status = json.status;
274
275 showAsErrors(form, '');
276 $('.post-form-w').unblock();
277
278 if (status === 'ok') {
279 resetForm();
280 getThreadDiff();
281 scrollToBottom();
282 } else {
283 var errors = json.errors;
284 for (var i = 0; i < errors.length; i++) {
285 var fieldErrors = errors[i];
286
287 var error = fieldErrors.errors;
288
289 showAsErrors(form, error);
290 }
291 }
292 }
293
294
295 /**
265 /**
296 * Run js methods that are usually run on the document, on the new post
266 * Run js methods that are usually run on the document, on the new post
297 */
267 */
@@ -362,33 +332,8 b' function updateNodeAttr(oldNode, newNode'
362
332
363 var form = $('#form');
333 var form = $('#form');
364
334
335 initAjaxForm(threadId);
365 if (form.length > 0) {
336 if (form.length > 0) {
366 var options = {
367 beforeSubmit: function(arr, form, options) {
368 $('.post-form-w').block({ message: gettext('Sending message...') });
369
370 $.each(pastedImages, function(i, blob) {
371 arr.push({
372 name: "file_0",
373 value: blob
374 });
375 });
376 },
377 success: updateOnPost,
378 error: function(xhr, textStatus, errorString) {
379 var errorText = gettext('Server error: ') + textStatus;
380 if (errorString) {
381 errorText += ' / ' + errorString;
382 }
383 showAsErrors(form, errorText);
384 $('.post-form-w').unblock();
385 },
386 url: '/api/add_post/' + threadId + '/',
387 timeout: POST_AJAX_TIMEOUT
388 };
389
390 form.ajaxForm(options);
391
392 resetForm();
337 resetForm();
393 }
338 }
394 }
339 }
@@ -182,6 +182,7 b''
182 </div>
182 </div>
183
183
184 <script src="{% static 'js/form.js' %}"></script>
184 <script src="{% static 'js/form.js' %}"></script>
185 <script src="{% static 'js/jquery.form.min.js' %}"></script>
185 <script src="{% static 'js/3party/jquery.blockUI.js' %}"></script>
186 <script src="{% static 'js/3party/jquery.blockUI.js' %}"></script>
186 <script src="{% static 'js/thread_create.js' %}"></script>
187 <script src="{% static 'js/thread_create.js' %}"></script>
187
188
@@ -68,6 +68,7 b' urlpatterns = ['
68 name='get_thread'),
68 name='get_thread'),
69 url(r'^api/add_post/(?P<opening_post_id>\w+)/$', api.api_add_post,
69 url(r'^api/add_post/(?P<opening_post_id>\w+)/$', api.api_add_post,
70 name='add_post'),
70 name='add_post'),
71 url(r'^api/add_post/$', api.api_add_post, name='add_post'),
71 url(r'^api/notifications/(?P<username>\w+)/$', api.api_get_notifications,
72 url(r'^api/notifications/(?P<username>\w+)/$', api.api_get_notifications,
72 name='api_notifications'),
73 name='api_notifications'),
73 url(r'^api/preview/$', api.api_get_preview, name='preview'),
74 url(r'^api/preview/$', api.api_get_preview, name='preview'),
@@ -9,7 +9,7 b' from django.shortcuts import get_object_'
9 from django.views.decorators.csrf import csrf_protect
9 from django.views.decorators.csrf import csrf_protect
10
10
11 from boards.abstracts.settingsmanager import get_settings_manager
11 from boards.abstracts.settingsmanager import get_settings_manager
12 from boards.forms import PostForm, PlainErrorList
12 from boards.forms import PostForm, PlainErrorList, ThreadForm
13 from boards.mdx_neboard import Parser
13 from boards.mdx_neboard import Parser
14 from boards.models import Post, Thread, Tag, TagAlias
14 from boards.models import Post, Thread, Tag, TagAlias
15 from boards.models.attachment import AttachmentSticker
15 from boards.models.attachment import AttachmentSticker
@@ -82,20 +82,26 b' def api_get_threaddiff(request):'
82
82
83
83
84 @csrf_protect
84 @csrf_protect
85 def api_add_post(request, opening_post_id):
85 def api_add_post(request, opening_post_id=None):
86 """
86 """
87 Adds a post and return the JSON response for it
87 Adds a post and return the JSON response for it
88 """
88 """
89
89
90 # TODO Allow thread creation here too, without specifying opening post
90 if opening_post_id:
91 opening_post = get_object_or_404(Post, id=opening_post_id)
91 opening_post = get_object_or_404(Post, id=opening_post_id)
92 else:
93 opening_post = None
92
94
93 status = STATUS_OK
95 status = STATUS_OK
94 errors = []
96 errors = []
95
97
96 post = None
98 post = None
97 if request.method == 'POST':
99 if request.method == 'POST':
100 if opening_post:
98 form = PostForm(request.POST, request.FILES, error_class=PlainErrorList)
101 form = PostForm(request.POST, request.FILES, error_class=PlainErrorList)
102 else:
103 form = ThreadForm(request.POST, request.FILES, error_class=PlainErrorList)
104
99 form.session = request.session
105 form.session = request.session
100
106
101 if form.need_to_ban:
107 if form.need_to_ban:
@@ -122,6 +128,10 b' def api_add_post(request, opening_post_i'
122
128
123 if post:
129 if post:
124 response['post_id'] = post.id
130 response['post_id'] = post.id
131 if not opening_post:
132 # FIXME For now we include URL only for threads to navigate to them.
133 # This needs to become something universal, just not yet sure how.
134 response['url'] = post.get_absolute_url()
125
135
126 return HttpResponse(content=json.dumps(response))
136 return HttpResponse(content=json.dumps(response))
127
137
General Comments 0
You need to be logged in to leave comments. Login now