Show More
@@ -1,461 +1,461 b'' | |||
|
1 | 1 | import hashlib |
|
2 | 2 | import string |
|
3 | 3 | from django.core import serializers |
|
4 | 4 | from django.core.urlresolvers import reverse |
|
5 | 5 | from django.http import HttpResponseRedirect |
|
6 | 6 | from django.http.response import HttpResponse |
|
7 | 7 | from django.template import RequestContext |
|
8 | 8 | from django.shortcuts import render, redirect, get_object_or_404 |
|
9 | 9 | from django.utils import timezone |
|
10 | 10 | |
|
11 | 11 | from boards import forms |
|
12 | 12 | import boards |
|
13 | 13 | from boards import utils |
|
14 | 14 | from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \ |
|
15 | 15 | ThreadCaptchaForm, PostCaptchaForm, LoginForm, ModeratorSettingsForm |
|
16 | 16 | |
|
17 | 17 | from boards.models import Post, Tag, Ban, User, RANK_USER, SETTING_MODERATE |
|
18 | 18 | from boards import authors |
|
19 | 19 | from boards.utils import get_client_ip |
|
20 | 20 | import neboard |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | def index(request, page=0): |
|
24 | 24 | context = _init_default_context(request) |
|
25 | 25 | |
|
26 | 26 | if utils.need_include_captcha(request): |
|
27 | 27 | threadFormClass = ThreadCaptchaForm |
|
28 | 28 | kwargs = {'request': request} |
|
29 | 29 | else: |
|
30 | 30 | threadFormClass = ThreadForm |
|
31 | 31 | kwargs = {} |
|
32 | 32 | |
|
33 | 33 | if request.method == 'POST': |
|
34 | 34 | form = threadFormClass(request.POST, request.FILES, |
|
35 | 35 | error_class=PlainErrorList, **kwargs) |
|
36 | 36 | form.session = request.session |
|
37 | 37 | |
|
38 | 38 | if form.is_valid(): |
|
39 | 39 | return _new_post(request, form) |
|
40 | 40 | if form.need_to_ban: |
|
41 | 41 | # Ban user because he is suspected to be a bot |
|
42 | 42 | _ban_current_user(request) |
|
43 | 43 | else: |
|
44 | 44 | form = threadFormClass(error_class=PlainErrorList, **kwargs) |
|
45 | 45 | |
|
46 | 46 | threads = [] |
|
47 | 47 | for thread in Post.objects.get_threads(page=int(page)): |
|
48 | 48 | threads.append({'thread': thread, |
|
49 | 49 | 'bumpable': thread.can_bump()}) |
|
50 | 50 | |
|
51 | 51 | context['threads'] = None if len(threads) == 0 else threads |
|
52 | 52 | context['form'] = form |
|
53 | 53 | context['pages'] = range(Post.objects.get_thread_page_count()) |
|
54 | 54 | |
|
55 | 55 | return render(request, 'boards/posting_general.html', |
|
56 | 56 | context) |
|
57 | 57 | |
|
58 | 58 | |
|
59 | 59 | def _new_post(request, form, thread_id=boards.models.NO_PARENT): |
|
60 | 60 | """Add a new post (in thread or as a reply).""" |
|
61 | 61 | |
|
62 | 62 | ip = get_client_ip(request) |
|
63 | 63 | is_banned = Ban.objects.filter(ip=ip).exists() |
|
64 | 64 | |
|
65 | 65 | if is_banned: |
|
66 | 66 | return redirect(you_are_banned) |
|
67 | 67 | |
|
68 | 68 | data = form.cleaned_data |
|
69 | 69 | |
|
70 | 70 | title = data['title'] |
|
71 | 71 | text = data['text'] |
|
72 | 72 | |
|
73 | 73 | if 'image' in data.keys(): |
|
74 | 74 | image = data['image'] |
|
75 | 75 | else: |
|
76 | 76 | image = None |
|
77 | 77 | |
|
78 | 78 | tags = [] |
|
79 | 79 | |
|
80 | 80 | new_thread = thread_id == boards.models.NO_PARENT |
|
81 | 81 | if new_thread: |
|
82 | 82 | tag_strings = data['tags'] |
|
83 | 83 | |
|
84 | 84 | if tag_strings: |
|
85 | 85 | tag_strings = tag_strings.split(' ') |
|
86 | 86 | for tag_name in tag_strings: |
|
87 | 87 | tag_name = string.lower(tag_name.strip()) |
|
88 | 88 | if len(tag_name) > 0: |
|
89 | 89 | tag, created = Tag.objects.get_or_create(name=tag_name) |
|
90 | 90 | tags.append(tag) |
|
91 | 91 | |
|
92 | 92 | linked_tags = tag.get_linked_tags() |
|
93 | 93 | if len(linked_tags) > 0: |
|
94 | 94 | tags.extend(linked_tags) |
|
95 | 95 | |
|
96 | 96 | op = None if thread_id == boards.models.NO_PARENT else \ |
|
97 | 97 | get_object_or_404(Post, id=thread_id) |
|
98 | 98 | post = Post.objects.create_post(title=title, text=text, ip=ip, |
|
99 | 99 | thread=op, image=image, |
|
100 | 100 | tags=tags, user=_get_user(request)) |
|
101 | 101 | |
|
102 | 102 | thread_to_show = (post.id if new_thread else thread_id) |
|
103 | 103 | |
|
104 | 104 | if new_thread: |
|
105 | 105 | return redirect(thread, post_id=thread_to_show) |
|
106 | 106 | else: |
|
107 | 107 | return redirect(reverse(thread, kwargs={'post_id': thread_to_show}) + |
|
108 | 108 | '#' + str(post.id)) |
|
109 | 109 | |
|
110 | 110 | |
|
111 | 111 | def tag(request, tag_name, page=0): |
|
112 | 112 | """ |
|
113 | 113 | Get all tag threads. Threads are split in pages, so some page is |
|
114 | 114 | requested. Default page is 0. |
|
115 | 115 | """ |
|
116 | 116 | |
|
117 | 117 | tag = get_object_or_404(Tag, name=tag_name) |
|
118 | 118 | threads = [] |
|
119 | 119 | for thread in Post.objects.get_threads(tag=tag, page=int(page)): |
|
120 | 120 | threads.append({'thread': thread, |
|
121 | 121 | 'bumpable': thread.can_bump()}) |
|
122 | 122 | |
|
123 | 123 | if request.method == 'POST': |
|
124 | 124 | form = ThreadForm(request.POST, request.FILES, |
|
125 | 125 | error_class=PlainErrorList) |
|
126 | 126 | form.session = request.session |
|
127 | 127 | |
|
128 | 128 | if form.is_valid(): |
|
129 | 129 | return _new_post(request, form) |
|
130 | 130 | if form.need_to_ban: |
|
131 | 131 | # Ban user because he is suspected to be a bot |
|
132 | 132 | _ban_current_user(request) |
|
133 | 133 | else: |
|
134 | 134 | form = forms.ThreadForm(initial={'tags': tag_name}, |
|
135 | 135 | error_class=PlainErrorList) |
|
136 | 136 | |
|
137 | 137 | context = _init_default_context(request) |
|
138 | 138 | context['threads'] = None if len(threads) == 0 else threads |
|
139 | 139 | context['tag'] = tag |
|
140 | 140 | context['pages'] = range(Post.objects.get_thread_page_count(tag=tag)) |
|
141 | 141 | |
|
142 | 142 | context['form'] = form |
|
143 | 143 | |
|
144 | 144 | return render(request, 'boards/posting_general.html', |
|
145 | 145 | context) |
|
146 | 146 | |
|
147 | 147 | |
|
148 | 148 | def thread(request, post_id): |
|
149 | 149 | """Get all thread posts""" |
|
150 | 150 | |
|
151 | 151 | if utils.need_include_captcha(request): |
|
152 | 152 | postFormClass = PostCaptchaForm |
|
153 | 153 | kwargs = {'request': request} |
|
154 | 154 | else: |
|
155 | 155 | postFormClass = PostForm |
|
156 | 156 | kwargs = {} |
|
157 | 157 | |
|
158 | 158 | if request.method == 'POST': |
|
159 | 159 | form = postFormClass(request.POST, request.FILES, |
|
160 | 160 | error_class=PlainErrorList, **kwargs) |
|
161 | 161 | form.session = request.session |
|
162 | 162 | |
|
163 | 163 | if form.is_valid(): |
|
164 | 164 | return _new_post(request, form, post_id) |
|
165 | 165 | if form.need_to_ban: |
|
166 | 166 | # Ban user because he is suspected to be a bot |
|
167 | 167 | _ban_current_user(request) |
|
168 | 168 | else: |
|
169 | 169 | form = postFormClass(error_class=PlainErrorList, **kwargs) |
|
170 | 170 | |
|
171 | 171 | posts = Post.objects.get_thread(post_id) |
|
172 | 172 | |
|
173 | 173 | context = _init_default_context(request) |
|
174 | 174 | |
|
175 | 175 | context['posts'] = posts |
|
176 | 176 | context['form'] = form |
|
177 | 177 | context['bumpable'] = posts[0].can_bump() |
|
178 | 178 | if context['bumpable']: |
|
179 | 179 | context['posts_left'] = neboard.settings.MAX_POSTS_PER_THREAD - len( |
|
180 | 180 | posts) |
|
181 |
context['bumplimit_progress'] = |
|
|
181 | context['bumplimit_progress'] = float(context['posts_left']) / | |
|
182 | 182 | neboard |
|
183 | 183 | .settings.MAX_POSTS_PER_THREAD |
|
184 |
* 100 |
|
|
184 | * 100 | |
|
185 | 185 | |
|
186 | 186 | return render(request, 'boards/thread.html', context) |
|
187 | 187 | |
|
188 | 188 | |
|
189 | 189 | def login(request): |
|
190 | 190 | """Log in with user id""" |
|
191 | 191 | |
|
192 | 192 | context = _init_default_context(request) |
|
193 | 193 | |
|
194 | 194 | if request.method == 'POST': |
|
195 | 195 | form = LoginForm(request.POST, request.FILES, |
|
196 | 196 | error_class=PlainErrorList) |
|
197 | 197 | form.session = request.session |
|
198 | 198 | |
|
199 | 199 | if form.is_valid(): |
|
200 | 200 | user = User.objects.get(user_id=form.cleaned_data['user_id']) |
|
201 | 201 | request.session['user_id'] = user.id |
|
202 | 202 | return redirect(index) |
|
203 | 203 | |
|
204 | 204 | else: |
|
205 | 205 | form = LoginForm() |
|
206 | 206 | |
|
207 | 207 | context['form'] = form |
|
208 | 208 | |
|
209 | 209 | return render(request, 'boards/login.html', context) |
|
210 | 210 | |
|
211 | 211 | |
|
212 | 212 | def settings(request): |
|
213 | 213 | """User's settings""" |
|
214 | 214 | |
|
215 | 215 | context = _init_default_context(request) |
|
216 | 216 | user = _get_user(request) |
|
217 | 217 | is_moderator = user.is_moderator() |
|
218 | 218 | |
|
219 | 219 | if request.method == 'POST': |
|
220 | 220 | if is_moderator: |
|
221 | 221 | form = ModeratorSettingsForm(request.POST, |
|
222 | 222 | error_class=PlainErrorList) |
|
223 | 223 | else: |
|
224 | 224 | form = SettingsForm(request.POST, error_class=PlainErrorList) |
|
225 | 225 | |
|
226 | 226 | if form.is_valid(): |
|
227 | 227 | selected_theme = form.cleaned_data['theme'] |
|
228 | 228 | |
|
229 | 229 | user.save_setting('theme', selected_theme) |
|
230 | 230 | |
|
231 | 231 | if is_moderator: |
|
232 | 232 | moderate = form.cleaned_data['moderate'] |
|
233 | 233 | user.save_setting(SETTING_MODERATE, moderate) |
|
234 | 234 | |
|
235 | 235 | return redirect(settings) |
|
236 | 236 | else: |
|
237 | 237 | selected_theme = _get_theme(request) |
|
238 | 238 | |
|
239 | 239 | if is_moderator: |
|
240 | 240 | form = ModeratorSettingsForm(initial={'theme': selected_theme, |
|
241 | 241 | 'moderate': context['moderator']}, |
|
242 | 242 | error_class=PlainErrorList) |
|
243 | 243 | else: |
|
244 | 244 | form = SettingsForm(initial={'theme': selected_theme}, |
|
245 | 245 | error_class=PlainErrorList) |
|
246 | 246 | |
|
247 | 247 | context['form'] = form |
|
248 | 248 | |
|
249 | 249 | return render(request, 'boards/settings.html', context) |
|
250 | 250 | |
|
251 | 251 | |
|
252 | 252 | def all_tags(request): |
|
253 | 253 | """All tags list""" |
|
254 | 254 | |
|
255 | 255 | context = _init_default_context(request) |
|
256 | 256 | context['all_tags'] = Tag.objects.get_not_empty_tags() |
|
257 | 257 | |
|
258 | 258 | return render(request, 'boards/tags.html', context) |
|
259 | 259 | |
|
260 | 260 | |
|
261 | 261 | def jump_to_post(request, post_id): |
|
262 | 262 | """Determine thread in which the requested post is and open it's page""" |
|
263 | 263 | |
|
264 | 264 | post = get_object_or_404(Post, id=post_id) |
|
265 | 265 | |
|
266 | 266 | if not post.thread: |
|
267 | 267 | return redirect(thread, post_id=post.id) |
|
268 | 268 | else: |
|
269 | 269 | return redirect(reverse(thread, kwargs={'post_id': post.thread.id}) |
|
270 | 270 | + '#' + str(post.id)) |
|
271 | 271 | |
|
272 | 272 | |
|
273 | 273 | def authors(request): |
|
274 | 274 | """Show authors list""" |
|
275 | 275 | |
|
276 | 276 | context = _init_default_context(request) |
|
277 | 277 | context['authors'] = boards.authors.authors |
|
278 | 278 | |
|
279 | 279 | return render(request, 'boards/authors.html', context) |
|
280 | 280 | |
|
281 | 281 | |
|
282 | 282 | def delete(request, post_id): |
|
283 | 283 | """Delete post""" |
|
284 | 284 | |
|
285 | 285 | user = _get_user(request) |
|
286 | 286 | post = get_object_or_404(Post, id=post_id) |
|
287 | 287 | |
|
288 | 288 | if user.is_moderator(): |
|
289 | 289 | # TODO Show confirmation page before deletion |
|
290 | 290 | Post.objects.delete_post(post) |
|
291 | 291 | |
|
292 | 292 | if not post.thread: |
|
293 | 293 | return _redirect_to_next(request) |
|
294 | 294 | else: |
|
295 | 295 | return redirect(thread, post_id=post.thread.id) |
|
296 | 296 | |
|
297 | 297 | |
|
298 | 298 | def ban(request, post_id): |
|
299 | 299 | """Ban user""" |
|
300 | 300 | |
|
301 | 301 | user = _get_user(request) |
|
302 | 302 | post = get_object_or_404(Post, id=post_id) |
|
303 | 303 | |
|
304 | 304 | if user.is_moderator(): |
|
305 | 305 | # TODO Show confirmation page before ban |
|
306 | 306 | Ban.objects.get_or_create(ip=post.poster_ip) |
|
307 | 307 | |
|
308 | 308 | return _redirect_to_next(request) |
|
309 | 309 | |
|
310 | 310 | |
|
311 | 311 | def you_are_banned(request): |
|
312 | 312 | """Show the page that notifies that user is banned""" |
|
313 | 313 | |
|
314 | 314 | context = _init_default_context(request) |
|
315 | 315 | return render(request, 'boards/staticpages/banned.html', context) |
|
316 | 316 | |
|
317 | 317 | |
|
318 | 318 | def page_404(request): |
|
319 | 319 | """Show page 404 (not found error)""" |
|
320 | 320 | |
|
321 | 321 | context = _init_default_context(request) |
|
322 | 322 | return render(request, 'boards/404.html', context) |
|
323 | 323 | |
|
324 | 324 | |
|
325 | 325 | def tag_subscribe(request, tag_name): |
|
326 | 326 | """Add tag to favorites""" |
|
327 | 327 | |
|
328 | 328 | user = _get_user(request) |
|
329 | 329 | tag = get_object_or_404(Tag, name=tag_name) |
|
330 | 330 | |
|
331 | 331 | if not tag in user.fav_tags.all(): |
|
332 | 332 | user.fav_tags.add(tag) |
|
333 | 333 | |
|
334 | 334 | return _redirect_to_next(request) |
|
335 | 335 | |
|
336 | 336 | |
|
337 | 337 | def tag_unsubscribe(request, tag_name): |
|
338 | 338 | """Remove tag from favorites""" |
|
339 | 339 | |
|
340 | 340 | user = _get_user(request) |
|
341 | 341 | tag = get_object_or_404(Tag, name=tag_name) |
|
342 | 342 | |
|
343 | 343 | if tag in user.fav_tags.all(): |
|
344 | 344 | user.fav_tags.remove(tag) |
|
345 | 345 | |
|
346 | 346 | return _redirect_to_next(request) |
|
347 | 347 | |
|
348 | 348 | |
|
349 | 349 | def static_page(request, name): |
|
350 | 350 | """Show a static page that needs only tags list and a CSS""" |
|
351 | 351 | |
|
352 | 352 | context = _init_default_context(request) |
|
353 | 353 | return render(request, 'boards/staticpages/' + name + '.html', context) |
|
354 | 354 | |
|
355 | 355 | |
|
356 | 356 | def api_get_post(request, post_id): |
|
357 | 357 | """ |
|
358 | 358 | Get the JSON of a post. This can be |
|
359 | 359 | used as and API for external clients. |
|
360 | 360 | """ |
|
361 | 361 | |
|
362 | 362 | post = get_object_or_404(Post, id=post_id) |
|
363 | 363 | |
|
364 | 364 | json = serializers.serialize("json", [post], fields=( |
|
365 | 365 | "pub_time", "_text_rendered", "title", "text", "image", |
|
366 | 366 | "image_width", "image_height", "replies", "tags" |
|
367 | 367 | )) |
|
368 | 368 | |
|
369 | 369 | return HttpResponse(content=json) |
|
370 | 370 | |
|
371 | 371 | |
|
372 | 372 | def get_post(request, post_id): |
|
373 | 373 | """Get the html of a post. Used for popups.""" |
|
374 | 374 | |
|
375 | 375 | post = get_object_or_404(Post, id=post_id) |
|
376 | 376 | |
|
377 | 377 | context = RequestContext(request) |
|
378 | 378 | context["post"] = post |
|
379 | 379 | |
|
380 | 380 | return render(request, 'boards/post.html', context) |
|
381 | 381 | |
|
382 | 382 | |
|
383 | 383 | def _get_theme(request, user=None): |
|
384 | 384 | """Get user's CSS theme""" |
|
385 | 385 | |
|
386 | 386 | if not user: |
|
387 | 387 | user = _get_user(request) |
|
388 | 388 | theme = user.get_setting('theme') |
|
389 | 389 | if not theme: |
|
390 | 390 | theme = neboard.settings.DEFAULT_THEME |
|
391 | 391 | |
|
392 | 392 | return theme |
|
393 | 393 | |
|
394 | 394 | |
|
395 | 395 | def _init_default_context(request): |
|
396 | 396 | """Create context with default values that are used in most views""" |
|
397 | 397 | |
|
398 | 398 | context = RequestContext(request) |
|
399 | 399 | |
|
400 | 400 | user = _get_user(request) |
|
401 | 401 | context['user'] = user |
|
402 | 402 | context['tags'] = user.get_sorted_fav_tags() |
|
403 | 403 | |
|
404 | 404 | theme = _get_theme(request, user) |
|
405 | 405 | context['theme'] = theme |
|
406 | 406 | context['theme_css'] = 'css/' + theme + '/base_page.css' |
|
407 | 407 | |
|
408 | 408 | # This shows the moderator panel |
|
409 | 409 | moderate = user.get_setting(SETTING_MODERATE) |
|
410 | 410 | if moderate == 'True': |
|
411 | 411 | context['moderator'] = user.is_moderator() |
|
412 | 412 | else: |
|
413 | 413 | context['moderator'] = False |
|
414 | 414 | |
|
415 | 415 | return context |
|
416 | 416 | |
|
417 | 417 | |
|
418 | 418 | def _get_user(request): |
|
419 | 419 | """ |
|
420 | 420 | Get current user from the session. If the user does not exist, create |
|
421 | 421 | a new one. |
|
422 | 422 | """ |
|
423 | 423 | |
|
424 | 424 | session = request.session |
|
425 | 425 | if not 'user_id' in session: |
|
426 | 426 | request.session.save() |
|
427 | 427 | |
|
428 | 428 | md5 = hashlib.md5() |
|
429 | 429 | md5.update(session.session_key) |
|
430 | 430 | new_id = md5.hexdigest() |
|
431 | 431 | |
|
432 | 432 | time_now = timezone.now() |
|
433 | 433 | user = User.objects.create(user_id=new_id, rank=RANK_USER, |
|
434 | 434 | registration_time=time_now) |
|
435 | 435 | |
|
436 | 436 | session['user_id'] = user.id |
|
437 | 437 | else: |
|
438 | 438 | user = User.objects.get(id=session['user_id']) |
|
439 | 439 | |
|
440 | 440 | return user |
|
441 | 441 | |
|
442 | 442 | |
|
443 | 443 | def _redirect_to_next(request): |
|
444 | 444 | """ |
|
445 | 445 | If a 'next' parameter was specified, redirect to the next page. This is |
|
446 | 446 | used when the user is required to return to some page after the current |
|
447 | 447 | view has finished its work. |
|
448 | 448 | """ |
|
449 | 449 | |
|
450 | 450 | if 'next' in request.GET: |
|
451 | 451 | next_page = request.GET['next'] |
|
452 | 452 | return HttpResponseRedirect(next_page) |
|
453 | 453 | else: |
|
454 | 454 | return redirect(index) |
|
455 | 455 | |
|
456 | 456 | |
|
457 | 457 | def _ban_current_user(request): |
|
458 | 458 | """Add current user to the IP ban list""" |
|
459 | 459 | |
|
460 | 460 | ip = utils.get_client_ip(request) |
|
461 | 461 | Ban.objects.get_or_create(ip=ip) |
General Comments 0
You need to be logged in to leave comments.
Login now