/
views.py
1196 lines (1032 loc) · 46.5 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from markdown import markdown
from smartypants import smartyPants as smartypants
import bleach
import datetime
import json
import os
# cf. http://meta.osqa.net/question/9722
from django.conf import settings as project_settings
from django.contrib import messages, auth
from django.contrib.auth.decorators import permission_required, login_required
from django.contrib.auth.models import User
from django.contrib.auth.views import login
from django.contrib.sites.models import Site
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.db.utils import DatabaseError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from forum.models import Category, Post, Report,\
Subscription, Thread, UserProfile
from forum.forms import CustomRegistrationForm
from registration import views as registration_views
# views.py TOC:
#
#
## helper constants
## helper errors
## helper functions
#
## home
#
## subscriptions
#
## category
## thread
## post
#
## user
## user_content
#
## add
## create
## reply
## edit
#
## moderate_thread
## merge_thread
## move_thread
## remove
## report
## reports
#
## pm
#
## custom_login
## custom_logout
## custom_register
## settings
#
## site_configuration
#
## saves_and_bookmarks
#
## simple_js
## nonjs
## subscribe
## bookmark
## lock
## sticky
## save
## thank
## agree
MAX_THREAD_TITLE_LENGTH = Thread._meta.get_field("title_plain").max_length
MAX_CATEGORY_TITLE_LENGTH = Category._meta.get_field("title_plain").max_length
POSTS_PER_PAGE = getattr(project_settings, "POSTS_PER_PAGE", 25)
THREADS_PER_PAGE = getattr(project_settings, "THREADS_PER_PAGE", 25)
USER_POSTS_PER_PAGE = getattr(project_settings, "USER_CONTENT_PER_PAGE", 10)
USER_THREADS_PER_PAGE = getattr(project_settings, "USER_CONTENT_PER_PAGE", 25)
BOOKMARKS_PER_PAGE = getattr(project_settings, "BOOKMARKS_PER_PAGE", 35)
SAVES_PER_PAGE = getattr(project_settings, "SAVES_PER_PAGE", 10)
SUBSCRIPTIONS_PER_PAGE = getattr(project_settings, "SUBSCRIPTIONS_PER_PAGE", 25)
long_title_error = "Your chosen title was too long. Keep it under %i characters."
operational_error = """
<p><strong>Mumbo jumbo:</strong> Django threw an \
<code>OperationalError</code> exception.</p>
<p><strong>English:</strong> Either: \
You used characters not supported by the database.</p>
<p>Or: The forum’s database is having an aneurysm.</p>
<p>Regardless: This should not happen, so notify your admin. \
S/he might want to contact the site’s host in return.</p>
<p>These three links will help you get to the bottom of this:</p>
<ul>
<li><a href="http://groups.google.com/group/django-users/\
browse_thread/thread/429447086fca6412?pli=1">\
Django users discussion</a></li>
<li><a href="https://docs.djangoproject.com/en/dev/ref/unicode/">\
Django docs</a></li>
<li><a href="http://rentzsch.tumblr.com/post/9133498042/\
howto-use-utf-8-throughout-your-web-stack">\
How-To guide</a></li>
</ul>
"""
def paginate(request, items, num_items):
"""Create and return a paginator."""
paginator = Paginator(items, num_items)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
items = paginator.page(page)
except (InvalidPage, EmptyPage):
items = paginator.page(paginator.num_pages)
return items
def prettify_title(title):
"""Sanitizes all (ALL) HTML elements in titles while prettifying the quotes
and dashes used in the titles of threads and categories.
"""
return bleach.clean(
smartypants(title, "2"),
tags=[], attributes={})
def sanitized_smartdown(string):
"""Formats a piece of text with SmartyPants, Markdown
and all the specified Markdown extensions. It also
escapes (or sanitizes) all HTML elements that are not explicitly
whitelisted in the `bleach.clean()` function.
Returns:
A reformatted string parsed by markdown() and smartdown() and
sanitized by bleach.clean().
Args:
`bleach.clean()` takes a string and purges all tags, attributes, and
styles that are not whitelisted to be escaped.
These are defined in ALLOWED_TAGS, ALLOWED_ATTRIBUTES, and ..._STYLES
respectively---the two former of which are defined explicitly
in this code for better management and extension.
Read more about `bleach` at https://github.com/jsocol/bleach.
`markdown()` takes a string and an optional list argument containing
all the extensions to be used with it.
`smartypants()` takes a string to be formatted.
NB (SmartyPants and Quotes):
For non-English-language boards, the SmartyPants quotes may
be different from how they are used in your country (e.g. France).
NB (Bleach and Encoding):
Bleach always returns a unicode object, whether you give it a
bytestring or a unicode object, but Bleach does not attempt to detect
incoming character encodings, and will assume UTF-8.
If you are using a different character encoding, you should convert
from a bytestring to unicode before passing the text to Bleach.
"""
ALLOWED_TAGS = [
'a',
'abbr',
'acronym',
'blockquote',
'code',
'del',
'em',
'h1',
'h2',
'h3',
'h4',
'h5',
'i',
'img',
'ins',
'li',
'ol',
'p',
'pre',
'strong',
'sub',
'sup',
'ul',
# Tables:
'table',
'caption',
'thead',
'tbody',
'tfoot',
'tr',
'th',
'td'
]
ALLOWED_ATTRIBUTES = {
'a': ['href', 'title'],
'acronym': ['title'],
'abbr': ['title'],
'h1': ['id'],
'h2': ['id'],
'h3': ['id'],
'h4': ['id'],
'h5': ['id'],
'img': ['alt', 'id', 'src', 'title'],
'th': ['colspan', 'rowspan'],
'td': ['colspan', 'rowspan']
}
MD_EXTENSIONS = [
'attr_list',
'fenced_code',
'tables'
]
return bleach.clean(
smartypants(
markdown(
text=string,
extensions=MD_EXTENSIONS,
#output_format='html5',
#lazy_ol=True,
safe_mode=False),
"2"),
tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES)
def home(request):
"""The landing page displaying categories, new and popular threads
as well as unread threads the user has subscribed to.
"""
subscribed_threads = False
categories = Category.objects.all()
threads = Thread.objects.exclude(is_removed__exact=True)
new_threads = threads[:5]
site = Site.objects.get_current()
if not request.user.is_anonymous() and request.user.subscriptions.all():
subscribed_threads = threads.filter(subscriber__exact=request.user)[:5]
# updates = request.user.subscription_set.all()
# update.last_read < update.thread.latest_reply_date
## or update.last_read == None # (or something)
# link to newest post
# number of new posts
site_config_error, email_config_error = False, False
if request.user.is_staff:
if site.domain == "example.com" or site.name == "example.com":
site_config_error = True
if project_settings.EMAIL_HOST_USER == "myusername@gmail.com":
email_config_error = True
return render(request, 'home.html',
{'categories': categories,
'new_threads': new_threads,
'subs': subscribed_threads,
'site_config_error': site_config_error,
'email_config_error': email_config_error})
@login_required()
def subscriptions(request):
"""Manage and observe all your subscriptions to threads with new posts."""
threads = Thread.objects.exclude(is_removed__exact=True)
new_threads = threads[:5]
objects = threads.filter(subscriber__exact=request.user)
new_subs = objects[:5]
# unread_subs = Replace news_subs with this one
objects = paginate(request, objects, SUBSCRIPTIONS_PER_PAGE)
return render(request, 'subscriptions.html',
{'new_threads': new_threads,
'new_subs': new_subs,
'objects': objects})
def category(request, category_id):
"""Category with threads ordered by latest posts."""
category = get_object_or_404(Category, pk=category_id)
all_threads = category.thread_set.all()
category_threads = all_threads.order_by("-latest_reply_date")\
.exclude(is_sticky__exact=True)
# .only("latest_reply_date")
category_threads = paginate(request, category_threads, THREADS_PER_PAGE)
stickies = all_threads.exclude(is_sticky__exact=False)
return render(request, 'category.html',
{'category': category,
'category_threads': category_threads,
'stickies': stickies})
def thread(request, thread_id, author_id):
"""Thread with all the belonging posts."""
thread = get_object_or_404(Thread, pk=thread_id)
if not thread.is_removed or request.user.has_perm('forum.remove_thread'):
posts = thread.post_set.all().select_related()
if not request.user.has_perm('forum.remove_thread') or not request.user.has_perm('forum.remove_post'):
posts = posts.exclude(is_removed__exact=True)
if author_id != "Everyone": # User has specified an author(_id)
posts = posts.filter(author__exact=author_id)
posts = posts.exclude(is_removed__exact=True)\
.order_by("creation_date")
posts = paginate(request, posts, POSTS_PER_PAGE)
# anchor_number = forloop.counter
anchor_number = '???'
return render(request, 'thread.html',
{'thread_id': thread_id,
'thread': thread,
'posts': posts,
'anchor_number': anchor_number})
else:
messages.info(request, "The thread %s has been removed and no longer available." % thread.title_html)
return HttpResponseRedirect(reverse('forum.views.category', args=(thread.category_id,)))
def post(request, post_id):
"""View a single post object."""
post = get_object_or_404(Post.objects.select_related(), pk=post_id)
thread = post.thread
if request.user.has_perm('forum.remove_thread') or not thread.is_removed:
if (not post.is_removed or
request.user.has_perm('forum.remove_thread') or
request.user.has_perm('forum.remove_post')):
return render(request, 'post.html', {'post': post, 'thread': thread})
else:
messages.info(request, "The post by %s has been removed and no longer available." % post.author)
return HttpResponseRedirect(reverse('forum.views.thread', args=(thread.id,)))
else:
messages.info(request, "The thread %s has been removed and no longer available." % thread.title_html)
return HttpResponseRedirect(reverse('forum.views.category', args=(thread.category_id,)))
def user(request, user_id):
"""Shows another user's profile.
Uses the `person` variable to disambiguate from `user`,
which is used in templates for the user viewing a page.
`latest_posts_amount` determines the amount of latest posts to show.
"""
person = get_object_or_404(User, pk=user_id)
posts = person.post_set.all().select_related()\
.exclude(is_removed__exact=True)\
.exclude(thread__is_removed__exact=True)\
.order_by("-creation_date")[:10]
return render(request, 'user.html', {'person': person, 'posts': posts})
def user_content(request, user_id, object_type):
"""Shows all posts or threads by specific user."""
person = get_object_or_404(User, pk=user_id)
if object_type == "post":
objects = person.post_set.all().select_related()\
.exclude(thread__is_removed__exact=True)
USER_CONTENT_PER_PAGE = USER_POSTS_PER_PAGE
else: # ... == "thread"
objects = person.thread_set.all()\
.exclude(is_removed__exact=True)
USER_CONTENT_PER_PAGE = USER_THREADS_PER_PAGE
objects = objects.order_by("-creation_date")
objects = paginate(request, objects, USER_CONTENT_PER_PAGE)
return render(request, 'user_content.html',
{'type': object_type,
'person': person,
'objects': objects})
@permission_required('forum.add_category')
def add(request):
"""Add a new category."""
title_plain = False
if request.method == 'POST': # Form has been submitted
title_plain = request.POST['title']
title_html = prettify_title(title_plain)
if len(title_plain) > MAX_CATEGORY_TITLE_LENGTH:
messages.error(request, long_title_error % MAX_CATEGORY_TITLE_LENGTH)
else:
Category.objects.create(title_plain=title_plain, title_html=title_html)
request.user.save()
return HttpResponseRedirect("/")
return render(request, 'add.html', {'title': title_plain})
@login_required()
def create(request, category_id):
"""Creation of a new thread from a category id.
Shows a preview of the thread, if the user pressed
the 'Preview' button instead of 'Reply'.
"""
category = get_object_or_404(Category, pk=category_id)
preview_plain = False
preview_html = False
title_plain = False
if request.method == 'POST': # Form has been submitted
title_plain = request.POST['title']
title_html = prettify_title(title_plain)
text_plain = request.POST['content']
if "submit" in request.POST: # "submit" button pressed
if len(title_plain) > MAX_THREAD_TITLE_LENGTH:
messages.error(request, long_title_error % MAX_THREAD_TITLE_LENGTH)
preview_plain = text_plain
preview_html = sanitized_smartdown(text_plain)
else:
user = request.user
now = datetime.datetime.now() # UTC?
text_html = sanitized_smartdown(text_plain)
try:
t = Thread.objects.create(
title_plain=title_plain, title_html=title_html,
author=user, category=category,
creation_date=now, latest_reply_date=now)
Post.objects.create(
thread=t, creation_date=now, author=user,
content_plain=text_plain, content_html=text_html)
t.subscriber.add(user)
except:
pass
else: # After successful submission
category.thread_count += 1
category.post_count += 1
t.post_count += 1
request.user.get_profile().thread_count += 1
request.user.get_profile().post_count += 1
category.save()
t.save()
request.user.get_profile().save()
return HttpResponseRedirect(reverse('forum.views.thread', args=(t.id,)))
elif "preview" in request.POST: # "preview" button pressed
preview_plain = text_plain
preview_html = sanitized_smartdown(text_plain)
return render(request, 'create.html',
{'category': category,
'title': title_plain,
'preview_plain': preview_plain,
'preview_html': preview_html})
@login_required()
def reply(request, thread_id):
"""Grabs the contents of the <textarea> in the reply,
gets the author id, slaps on the time stamp,
and creates the post.
Shows a preview of the post, if the user pressed
the 'Preview' button instead of 'Reply'.
"""
thread = get_object_or_404(Thread, pk=thread_id)
preview_plain = False
preview_html = False
if thread.is_locked:
messages.info(request, "The thread %s has been locked and can not be posted in." % thread.title_html)
return HttpResponseRedirect(reverse('forum.views.thread', args=(thread.id,)))
elif thread.is_removed:
messages.info(request, "The thread %s has been removed and is no longer available." % thread.title_html)
return HttpResponseRedirect(reverse('forum.views.category', args=(thread.category_id,)))
if request.method == 'POST': # Form been submitted
text = request.POST['content']
if "submit" in request.POST: # "submit" button pressed
user = request.user
now = datetime.datetime.now() # UTC?
html = sanitized_smartdown(text)
Post.objects.create(
thread=thread, author=user, creation_date=now,
content_plain=text, content_html=html)
if request.user.get_profile().auto_subscribe:
thread.subscriber.add(request.user)
thread.latest_reply_date = now
thread.category.post_count += 1
thread.post_count += 1
request.user.get_profile().post_count += 1
thread.category.save()
thread.save()
request.user.get_profile().save()
return HttpResponseRedirect(reverse('forum.views.thread', args=(thread.id,)))
elif "preview" in request.POST: # "preview" button pressed
preview_plain = text
preview_html = sanitized_smartdown(text)
return render(request, 'reply.html',
{'thread': thread,
'preview_plain': preview_plain,
'preview_html': preview_html})
@login_required()
def edit(request, post_id):
"""Lets the author edit his or her post."""
post = get_object_or_404(Post, pk=post_id)
preview_plain = False
preview_html = False
if post.thread.is_removed:
messages.info(request, "The thread %s has been removed and is no longer available." % post.thread.title_html)
return HttpResponseRedirect(reverse('forum.views.category', args=(post.thread.category_id,)))
elif post.is_removed:
messages.info(request, "Your post has been removed and is no longer available.")
return HttpResponseRedirect(reverse('forum.views.thread', args=(post.thread_id,)))
if request.method == 'POST': # Form has been submitted
if "submit" in request.POST: # "submit" button pressed
post.content_plain = request.POST['content']
post.content_html = sanitized_smartdown(post.content_plain)
post.save()
return HttpResponseRedirect(reverse('forum.views.thread', args=(post.thread_id,)))
elif "preview" in request.POST: # "preview" button pressed
preview_plain = request.POST['content']
preview_html = sanitized_smartdown(preview_plain)
# The post author or a moderator/admin visits the link
elif request.user == post.author or request.user.has_perm('forum.change_post'):
pass
else: # Someone who is not the author goes to the edit link
return HttpResponseRedirect(reverse('forum.views.post', args=(post.id,)))
return render(request, 'edit.html',
{'post': post,
'preview_plain': preview_plain,
'preview_html': preview_html})
@permission_required('forum.merge_thread')
def merge_thread(request, thread_id):
"""Merge the posts of two threads into one single thread.
1. The posts are ordered chronologically in the new thread.
2. The old threads are locked.
3. A notification post is created in all three threads.
"""
thread = get_object_or_404(Thread, pk=thread_id)
new_title_plain = thread.title_plain
new_title_html = thread.title_html
other_thread = False
if request.method == 'POST' and "cancel" not in request.POST:
other_thread = get_object_or_404(Thread, pk=request.POST['other-thread-id']) # try --- fix
new_title_plain = request.POST['new-thread-title']
new_title_html = prettify_title(new_title_plain)
if request.method == 'POST' and "merge" in request.POST:
if len(new_title_plain) > MAX_THREAD_TITLE_LENGTH:
messages.error(request, long_title_error % MAX_THREAD_TITLE_LENGTH)
elif request.method == 'POST' and "confirm" in request.POST:
now = datetime.datetime.now() # UTC?
user = request.user
t = Thread.objects.create(
title_plain=new_title_plain, title_html=new_title_html,
creation_date=now, author=user, category=thread.category,
latest_reply_date=now)
# Update posts in two threads to point to new thread t
thread.post_set.all().update(thread=t.id)
other_thread.post_set.all().update(thread=t.id)
# Make post notification in ALL threads
# Do not append a redundant full stop
if new_title_plain[-1] not in set([".!?"]) \
and new_title_plain[-3:-1] not in set(['."', '!"', '?"', ".'", "!'", "?'"]):
end = "."
else:
end = ""
message = "(*%s* was merged with *%s* by *%s* into *[%s](%s)*%s)" % (
thread.title_html,
other_thread.title_html,
user.username,
new_title_html,
reverse('forum.views.thread', args=(t.id,)),
end)
html = sanitized_smartdown(message)
Post.objects.bulk_create([
Post(creation_date=now, author=user, thread=t,
content_plain=message, content_html=html),
Post(creation_date=now, author=user, thread=thread,
content_plain=message, content_html=html),
Post(creation_date=now, author=user, thread=other_thread,
content_plain=message, content_html=html)
])
# Lock original threads
thread.is_locked = True
other_thread.is_locked = True
thread.save()
other_thread.save()
return HttpResponseRedirect(reverse('forum.views.thread', args=(t.id,)))
return render(request, 'merge.html',
{'thread': thread,
'other_thread': other_thread,
'new_title': new_title_plain})
@permission_required('forum.change_thread')
def moderate_thread(request, thread_id):
"""Change the title of a thread."""
thread = get_object_or_404(Thread, pk=thread_id)
title_plain = thread.title_plain
if request.method == 'POST': # Form has been submitted
if len(request.POST['title']) > MAX_THREAD_TITLE_LENGTH:
messages.error(request, long_title_error % MAX_THREAD_TITLE_LENGTH)
else:
try:
thread.title_plain = request.POST['title']
thread.title_html = prettify_title(thread.title_plain)
thread.save()
except:
pass
else:
return HttpResponseRedirect(reverse('forum.views.thread', args=(thread.id,)))
return render(request, 'moderate.html',
{'thread': thread,
'title': title_plain})
@permission_required('forum.move_thread')
def move_thread(request, thread_id):
"""Move a thread to another category."""
thread = get_object_or_404(Thread, pk=thread_id)
categories = Category.objects.all()
if request.method == 'POST': # Form has been submitted
thread.category = get_object_or_404(Category, pk=request.POST['category'])
thread.save()
return HttpResponseRedirect(reverse('forum.views.thread', args=(thread.id,)))
else: # Otherwise, show clean, normal page with no populated data
return render(request, 'move.html',
{'thread': thread,
'categories': categories})
@login_required()
def remove(request, object_id, object_type):
"""Lets the permitted user remove a post or thread.
A database deletion is not performed upon removal;
the post or thread is merely hidden in the template.
An actual deletion has to be done manually in the admin settings or Django shell.
"""
if object_type == "post":
obj = get_object_or_404(Post, pk=object_id)
thread = obj.thread
post_obj = obj
if not request.user.has_perm('forum.remove_post'):
return HttpResponseRedirect(reverse('forum.views.post', args=(obj.id,)))
else: # ... == thread
obj = get_object_or_404(Thread, pk=object_id)
thread = obj
post_obj = None
if not request.user.has_perm('forum.remove_thread'):
return HttpResponseRedirect(reverse('forum.views.thread', args=(obj.id,)))
if request.method == 'POST': # Form has been submitted
if 'remove' in request.POST: # Remove command
if obj.is_removed:
messages.info(request, "The %s was already removed." % object_type)
obj.is_removed = True
else: # Restore command
if not obj.is_removed:
messages.info(request, "The %s was already not removed." % object_type)
obj.is_removed = False
obj.save()
return HttpResponseRedirect(reverse('forum.views.thread', args=(thread.id,)))
else:
return render(request, 'simple_action.html',
{'object_type': object_type,
'obj': obj,
'thread': thread,
'post_obj': post_obj,
'action': 'remove'})
@login_required()
def report(request, object_id, object_type):
"""Report a post or thread infraction to the moderators."""
title = False
preview_html = False
if object_type == "thread":
obj = get_object_or_404(Thread, pk=object_id)
thread = obj
post_obj = None
# Check for reasons not to let the user file a report
if obj.is_removed:
messages.info(request, "The thread %s has been removed and is no longer available." % thread.title_html)
return HttpResponseRedirect(reverse('forum.views.category', args=(thread.category_id,)))
elif thread.is_locked:
messages.info(request, "The thread %s has been locked and can not be reported." % thread.title_html)
# Thread already reported AND unaddressed by moderator
elif Report.objects.filter(author__exact=request.user)\
.filter(thread__exact=obj)\
.filter(was_addressed__exact=False):
messages.info(request, "This %s has already been reported by you." % object_type)
return HttpResponseRedirect(reverse('forum.views.thread', args=(thread.id,)))
else: # ... == post
obj = get_object_or_404(Post, pk=object_id)
thread = obj.thread
post_obj = obj
# Check for reasons not to let the user file a report
if obj.is_removed:
messages.info(request, "The post has been removed and is no longer available.")
return HttpResponseRedirect(reverse('forum.views.thread', args=(thread.id,)))
elif request.user == obj.author:
messages.error(request, "You cannot report your own posts, silly goose.")
return HttpResponseRedirect(reverse('forum.views.thread', args=(thread.id,)))
elif obj.author.is_staff:
messages.info(request, "The author of the reported post is an admin. \
You should contact them directly, if you take issue with their content.")
return HttpResponseRedirect(reverse('forum.views.thread', args=(thread.id,)))
# Post already reported by user
elif Report.objects.filter(author__exact=request.user)\
.filter(post__exact=obj):
messages.info(request, "This %s has already been reported by you." % object_type)
return HttpResponseRedirect(reverse('forum.views.thread', args=(thread.id,)))
if request.method == 'POST': # Form has been submitted
title = request.POST['title']
if "content" in request.POST: # Elaboration provided
text_plain = request.POST['content']
text_html = sanitized_smartdown(text_plain)
if "submit" in request.POST: # "submit" button pressed
if len(title) > MAX_THREAD_TITLE_LENGTH:
messages.error(request, long_title_error % MAX_THREAD_TITLE_LENGTH)
preview_html = text_html # buggy if no content?
else:
user = request.user
now = datetime.datetime.now() # UTC?
try:
r = Report.objects.create(
creation_date=now, author=user,
reason_short=title, thread=thread)
if "content" in request.POST:
r.reason_long_plain = text_plain
r.reason_long_html = text_html
if object_type == "post":
r.post = obj
r.save()
except:
pass
else:
# After successful submission
return HttpResponseRedirect(reverse('forum.views.thread',
args=(thread.id,)))
elif "preview" in request.POST: # "preview" button pressed
preview_html = text_html # buggy if no content?
return render(request, 'report.html',
{'obj': obj,
'type': object_type,
'thread': thread,
'post_obj': post_obj,
'title': title,
'preview_html': preview_html})
@permission_required('forum.use_report')
def reports(request):
"""Inspect and act on filed reports on threads and posts."""
# Hide reports that have been addressed
reports = Report.objects.filter(was_addressed__exact=False)
reports = reports.order_by("creation_date")
reports = reports.order_by("thread")
if request.method == 'POST': # Report has been dismissed
report = get_object_or_404(Report, pk=request.POST['report-id'])
report.was_addressed = True
report.addressed_by = request.user
report.date_addressed = datetime.datetime.now() # UTC?
report.save()
return render(request, 'reports.html', {'reports': reports})
def search(request):
return render(request, 'placeholder.html', {})
def custom_login(request, **kwargs):
"""Redirects logged-in users, and allows others to log in."""
if request.user.is_authenticated(): # User logged in
return HttpResponseRedirect(reverse('forum.views.home', args=()))
else:
return login(request, **kwargs)
def custom_logout(request):
"""Logs out the user with a message confirmation."""
if request.user.is_authenticated(): # User logged in
auth.logout(request)
messages.success(request, "Logged out successfully.")
return HttpResponseRedirect(reverse('forum.views.home', args=()))
def custom_register(request, **kwargs):
"""Registers users and redirects those who are already authenticated."""
site = Site.objects.get_current()
if request.user.is_authenticated():
return HttpResponseRedirect(reverse('forum.views.home', args=()))
else:
site_config_error, email_config_error = False, False
if site.domain == "example.com" or site.name == "example.com":
site_config_error = True
if project_settings.EMAIL_HOST_USER == "myusername@gmail.com":
email_config_error = True
return registration_views.register(request,
backend='registration.backends.default.DefaultBackend',
template_name='registration/registration_form.html',
extra_context={
'site_config_error': site_config_error,
'email_config_error': email_config_error,
'max_username_length':
User._meta.get_field("username").max_length},
form_class=CustomRegistrationForm)
@login_required()
def settings(request):
"""Here the user can manage his or her user settings."""
if request.method == "POST": # Changes submitted
profile = request.user.get_profile()
profile.has_dyslexia = request.POST['has_dyslexia']
profile.auto_subscribe = request.POST['auto_subscribe'] == "True"
profile.save()
messages.success(request, "New settings saved.")
return render(request, 'settings.html', {})
@login_required()
def site_configuration(request):
"""Displays all configurable environment variables to admins."""
if not request.user.is_staff:
messages.error(request,
"You need staff status to configure the site.")
return HttpResponseRedirect(reverse('forum.views.home', args=()))
site = Site.objects.get_current()
if request.method == "POST": # Changes submitted
site.name = request.POST['site_name']
site.domain = request.POST['site_domain']
site.save()
messages.success(request, "New settings saved.")
return render(request, 'site_configuration.html', {
'EMAIL_HOST_USER':
project_settings.EMAIL_HOST_USER != "myusername@gmail.com",
'EMAIL_HOST_PASSWORD': project_settings.EMAIL_HOST != "mypassword",
'EMAIL_USE_TLS': project_settings.EMAIL_USE_TLS,
'LOCAL_DEVELOPMENT': project_settings.LOCAL_DEVELOPMENT,
'TIME_ZONE': project_settings.TIME_ZONE,
'LANGUAGE_CODE': project_settings.LANGUAGE_CODE,
'ACCOUNT_ACTIVATION_DAYS': project_settings.ACCOUNT_ACTIVATION_DAYS,
'REGISTRATION_OPEN': project_settings.REGISTRATION_OPEN,
'POSTS_PER_PAGE': project_settings.POSTS_PER_PAGE,
'THREADS_PER_PAGE': project_settings.THREADS_PER_PAGE,
'USER_POSTS_PER_PAGE': project_settings.USER_POSTS_PER_PAGE,
'USER_THREADS_PER_PAGE': project_settings.USER_THREADS_PER_PAGE,
'SUBSCRIPTIONS_PER_PAGE': project_settings.SUBSCRIPTIONS_PER_PAGE,
'BOOKMARKS_PER_PAGE': project_settings.BOOKMARKS_PER_PAGE,
'SAVES_PER_PAGE': project_settings.SAVES_PER_PAGE,
'DEFAULT_FROM_EMAIL': project_settings.DEFAULT_FROM_EMAIL,
'HAS_SITE_NAME': site.name != "example.com",
'HAS_SITE_DOMAIN': site.domain != "example.com",
'SITE_NAME': site.name,
'SITE_DOMAIN': site.domain
})
@login_required()
def saves_and_bookmarks(request, object_type):
"""Shows the user's saved posts and/or bookmarked threads."""
if object_type == "save":
objects = request.user.saves.all().select_related()\
.exclude(thread__is_removed__exact=True)
USER_CONTENT_PER_PAGE = SAVES_PER_PAGE
else: # ... == "bookmark"
objects = request.user.bookmarks.all()
USER_CONTENT_PER_PAGE = BOOKMARKS_PER_PAGE
# objects = objects.order_by("-creation_date") -saved_date
objects = paginate(request, objects, USER_CONTENT_PER_PAGE)
return render(request, 'saves_and_bookmarks.html',
{'type': object_type,
'objects': objects})
def simple_js(request):
"""Lets users do one of the following:
From the user view:
* follow a user
* add a user to a shit list
From the thread or post view:
* bookmark threads
* subscribe to threads
* agree with posts
* thank users for posts
* save posts
or
* bookmark and unbookmark threads from the bookmarks view
* save and unsave posts from the saves view
* subscribe to and unsubscribe from threads in the subscription view
"""
if request.is_ajax() and request.method == "POST":
object_id = request.POST['object_id']
action = request.POST['action'].lower()
href = request.POST['href']
if 'follow' in href or 'ignore' in href:
person = get_object_or_404(User, pk=object_id)
elif 'bookmark' in href or 'subscribe' in href:
thread = get_object_or_404(Thread, pk=object_id)
elif 'save' in href or 'thank' in href or 'agree' in href:
post = get_object_or_404(Post, pk=object_id)
# User JS
if "follow" in href:
if action.startswith("follow"):
request.user.get_profile().follows.add(person)
new_action = "Unfollow user"
else:
request.user.get_profile().follows.remove(person)
new_action = "Follow user"
elif "ignore" in href:
if action.startswith("add"):
request.user.get_profile().ignores.add(person)
new_action = "Remove from shit list"
else:
request.user.get_profile().ignores.remove(person)
new_action = "Add to shit list"
# Thread JS
elif "bookmark" in action:
# Not to be confused with the action related
# to the bookmark view
if action == "bookmark":
thread.bookmarker.add(request.user)
new_action = "Unbookmark"
else:
thread.bookmarker.remove(request.user)
new_action = "Bookmark"
elif "subscribe" in action:
# Not to be confused with the action related
# to the subscriptions view
if action == "subscribe":
thread.subscriber.add(request.user)
new_action = "Unsubscribe"
else:
thread.subscriber.remove(request.user)
new_action = "Subscribe"
elif "agree" in action:
if action == "agree":
post.agrees.add(request.user)
new_action = "Unagree"
else:
post.agrees.remove(request.user)
new_action = "Agree"