-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathviews.py
More file actions
executable file
·1090 lines (934 loc) · 41.4 KB
/
views.py
File metadata and controls
executable file
·1090 lines (934 loc) · 41.4 KB
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
import json
import requests
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden
from django.shortcuts import render, get_object_or_404
from django.template.context_processors import csrf
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.db.models import Q, OuterRef, Subquery, Max, Count
from django.core.mail import EmailMultiAlternatives
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth import get_user_model
from django.core.cache import cache
from website.models import Question, Answer, Notification, AnswerComment
from spoken_auth.models import TutorialDetails, TutorialResources
from website.forms import NewQuestionForm, AnswerQuesitionForm
from website.helpers import get_video_info, prettify, clean_user_data, get_similar_questions
from django.conf import settings
from website.templatetags.permission_tags import can_edit, can_hide_delete
from spoken_auth.models import FossCategory, Participant
from .sortable import SortableHeader, get_sorted_list, get_field_index
from forums.views import user_logout
from website.permissions import is_administrator, is_forumsadmin
User = get_user_model()
HOME_CACHE_TIMEOUT = 3600
def _get_home_categories():
cache_key = 'home:categories'
categories = cache.get(cache_key)
if categories is not None:
return categories
trs = TutorialResources.objects.filter(
Q(status=1) | Q(status=2),
tutorial_detail__foss__show_on_homepage__lt=2,
language__name='English',
)
trs = trs.values_list('tutorial_detail__foss__foss', flat=True).order_by('tutorial_detail__foss__foss').distinct()
categories = list(trs)
cache.set(cache_key, categories, HOME_CACHE_TIMEOUT)
return categories
def _get_home_questions(base_queryset):
cache_key = 'home:recent_questions'
questions = cache.get(cache_key)
if questions is not None:
return questions
questions = list(base_queryset.filter(status=1).order_by('-date_created')[:100])
cache.set(cache_key, questions, HOME_CACHE_TIMEOUT)
return questions
def _get_home_active_questions(base_queryset):
cache_key = 'home:active_questions'
questions = cache.get(cache_key)
if questions is not None:
return questions
questions = list(
base_queryset.filter(status=1, last_active__isnull=False).order_by('-last_active')[:100]
)
cache.set(cache_key, questions, HOME_CACHE_TIMEOUT)
return questions
def _get_home_slider_questions(base_queryset):
cache_key = 'home:slider_questions'
slider_questions = cache.get(cache_key)
if slider_questions is not None:
return slider_questions
subquery = (
Question.objects.filter(category=OuterRef('category'), status=1)
.values('category')
.annotate(max_date=Max('date_created'))
.values('max_date')
)
slider_questions = list(
base_queryset.filter(date_created=Subquery(subquery), status=1).order_by('category')
)
cache.set(cache_key, slider_questions, HOME_CACHE_TIMEOUT)
return slider_questions
def _get_home_spam_questions(base_queryset):
cache_key = 'home:spam_questions'
spam_questions = cache.get(cache_key)
if spam_questions is not None:
return spam_questions
spam_questions = list(base_queryset.filter(status=2).order_by('-last_active')[:100])
cache.set(cache_key, spam_questions, HOME_CACHE_TIMEOUT)
return spam_questions
def _get_home_category_question_map(categories, slider_questions):
cache_key = 'home:category_question_map'
category_question_map = cache.get(cache_key)
if category_question_map is not None:
return category_question_map
category_fosses = {val.replace(" ", "-"): val for val in categories}
all_eligible_categories = list(category_fosses.keys())
category_question_map = {}
for question in slider_questions:
if question.category in all_eligible_categories:
foss = category_fosses.get(question.category)
category_question_map[foss] = {
"id": question.id,
"question": question.title,
"foss_url": question.category,
}
for category in category_fosses.keys():
foss = category_fosses.get(category)
if foss not in category_question_map:
category_question_map[foss] = None
category_question_map = dict(
sorted(category_question_map.items(), key=lambda item: item[0].lower())
)
cache.set(cache_key, category_question_map, HOME_CACHE_TIMEOUT)
return category_question_map
def home(request):
base_queryset = Question.objects.annotate(answer_count=Count('answer'))
questions_full = _get_home_questions(base_queryset)
active_questions_full = _get_home_active_questions(base_queryset)
slider_questions = _get_home_slider_questions(base_queryset)
spam_questions = None
show_spam_list = is_administrator(request.user) or is_forumsadmin(request.user)
if show_spam_list:
spam_questions_full = _get_home_spam_questions(base_queryset)
# spam paginator
spam_paginator = Paginator(spam_questions_full, 10)
spam_page_number = request.GET.get('spam_page')
spam_questions = spam_paginator.get_page(spam_page_number)
# paginate active questions
recent_paginator = Paginator(questions_full, 10)
active_paginator = Paginator(active_questions_full, 10)
recent_page_number = request.GET.get('recent_page')
active_page_number = request.GET.get('active_page')
questions = recent_paginator.get_page(recent_page_number)
active_questions = active_paginator.get_page(active_page_number)
if spam_questions is None:
all_questions = list(questions.object_list) + list(active_questions.object_list) + list(slider_questions)
else:
all_questions = list(questions.object_list) + list(active_questions.object_list) + list(slider_questions) + list(spam_questions.object_list)
uids = set()
for q in all_questions:
uids.add(q.uid)
if q.last_post_by:
uids.add(q.last_post_by)
users = {u.id: u.username for u in User.objects.filter(id__in=uids)}
# Attach usernames to question objects so templates don't trigger queries
for q in all_questions:
q.cached_user = users.get(q.uid, "Unknown User")
q.cached_last_post_user = users.get(q.last_post_by, "Unknown User") if q.last_post_by else "Unknown User"
categories = _get_home_categories()
category_question_map = _get_home_category_question_map(categories, slider_questions)
context = {
'questions': questions,
'active_questions': active_questions,
'spam_questions': spam_questions,
'category_question_map': category_question_map,
'show_spam_list': show_spam_list,
'current_tab': request.GET.get('tab', 'recent_question'),
}
return render(request, "website/templates/index.html", context)
def questions(request):
questions = Question.objects.filter(status=1).annotate(total_answers=Count('answer')).order_by('category', 'tutorial')
raw_get_data = request.GET.get('o', None)
header = {
1: SortableHeader('category', True, 'Foss'),
2: SortableHeader('tutorial', True, 'Tutorial Name'),
3: SortableHeader('minute_range', True, 'Mins'),
4: SortableHeader('second_range', True, 'Secs'),
5: SortableHeader('title', True, 'Title'),
6: SortableHeader('date_created', True, 'Date'),
7: SortableHeader('views', True, 'Views'),
8: SortableHeader('total_answers', 'True', 'Answers'),
9: SortableHeader('username', False, 'User')
}
tmp_recs = get_sorted_list(request, questions, header, raw_get_data)
ordering = get_field_index(raw_get_data)
paginator = Paginator(tmp_recs, 20)
page = request.GET.get('page')
try:
questions = paginator.page(page)
except PageNotAnInteger:
questions = paginator.page(1)
except EmptyPage:
questions = paginator.page(paginator.num_pages)
# Attach cached usernames to avoid N+1 in templates
uids = {q.uid for q in questions}
users = {u.id: u.username for u in User.objects.filter(id__in=uids)}
for q in questions:
q.cached_user = users.get(q.uid, "Unknown User")
context = {
'questions': questions,
'header': header,
'ordering': ordering,
}
return render(request, 'website/templates/questions.html', context)
def hidden_questions(request):
questions = Question.objects.filter(status=0).annotate(total_answers=Count('answer')).order_by('-date_created')
paginator = Paginator(questions, 20)
page = request.GET.get('page')
try:
questions = paginator.page(page)
except PageNotAnInteger:
questions = paginator.page(1)
except EmptyPage:
questions = paginator.page(paginator.num_pages)
# Attach cached usernames similar to questions() view
uids = {q.uid for q in questions}
users = {u.id: u.username for u in User.objects.filter(id__in=uids)}
for q in questions:
q.cached_user = users.get(q.uid, "Unknown User")
context = {
'questions': questions,
}
return render(request, 'website/templates/questions.html', context)
def get_question(request, question_id=None, pretty_url=None):
question = get_object_or_404(Question, id=question_id)
pretty_title = prettify(question.title)
category = FossCategory.objects.all().order_by('foss')
if pretty_url != pretty_title:
return HttpResponseRedirect('/question/' + question_id + '/' + pretty_title)
# Prefetch answers and their comments to avoid N+1 queries
answers = (
question.answer_set.all()
.prefetch_related('answercomment_set')
.order_by('date_created')
)
form = AnswerQuesitionForm()
if question.status in (0,2):
label = "Show"
else:
label = "Hide"
# Cache usernames for question, answers and comments
uids = {question.uid}
for answer in answers:
uids.add(answer.uid)
for comment in answer.answercomment_set.all():
uids.add(comment.uid)
users = {u.id: u.username for u in User.objects.filter(id__in=uids)}
question.cached_user = users.get(question.uid, "Unknown User")
for answer in answers:
answer.cached_user = users.get(answer.uid, "Unknown User")
for comment in answer.answercomment_set.all():
comment.cached_user = users.get(comment.uid, "Unknown User")
context = {
'question': question,
'answers': answers,
'category': category,
'form': form,
'label': label,
}
user_has_role = has_role(request.user)
context['require_recaptcha'] = not user_has_role
context['recaptcha_site_key'] = settings.RECAPTCHA_SITE_KEY
context.update(csrf(request))
# updating views count
question.views += 1
question.save()
return render(request, 'website/templates/get-question.html', context)
@login_required
def question_answer(request):
if request.method == 'POST':
form = AnswerQuesitionForm(request.POST)
context = {}
if form.is_valid():
cleaned_data = form.cleaned_data
qid = cleaned_data['question']
user_has_role = has_role(request.user)
# only require captcha for users without a role
if not user_has_role:
recaptcha_response = request.POST.get('g-recaptcha-response', '')
if not recaptcha_response:
messages.error(request, "Please complete the reCAPTCHA verification.")
question = get_object_or_404(Question, id=qid)
answers = question.answer_set.all()
if question.status in (0,2):
label = "Show"
else:
label = "Hide"
context = {
'question': question,
'answers': answers,
'form': form,
'label': label,
'require_recaptcha': True,
'recaptcha_site_key': settings.RECAPTCHA_SITE_KEY
}
context.update(csrf(request))
return render(request, 'website/templates/get-question.html', context)
# verify with google
recaptcha_verification_url = "https://www.google.com/recaptcha/api/siteverify"
recaptcha_data = {
'secret': settings.RECAPTCHA_SECRET_KEY,
'response': recaptcha_response
}
try:
recaptcha_result = requests.post(recaptcha_verification_url, data=recaptcha_data, timeout=5)
recaptcha_result.raise_for_status()
recaptcha_json = recaptcha_result.json()
except requests.RequestException as e:
messages.error(request, "Error verifying reCAPTCHA. Please try again.")
question = get_object_or_404(Question, id=qid)
answers = question.answer_set.all()
if question.status in (0,2):
label = "Show"
else:
label = "Hide"
context = {
'question': question,
'answers': answers,
'form': form,
'label': label,
'require_recaptcha': True,
'recaptcha_site_key': settings.RECAPTCHA_SITE_KEY
}
context.update(csrf(request))
return render(request, 'website/templates/get-question.html', context)
# check if verification was successful
if not recaptcha_json.get('success', False):
messages.error(request, "reCAPTCHA verification failed. Please try again.")
question = get_object_or_404(Question, id=qid)
answers = question.answer_set.all()
if question.status in (0,2):
label = "Show"
else:
label = "Hide"
context = {
'question': question,
'answers': answers,
'form': form,
'label': label,
'require_recaptcha': True,
'recaptcha_site_key': settings.RECAPTCHA_SITE_KEY
}
context.update(csrf(request))
return render(request, 'website/templates/get-question.html', context)
qid = cleaned_data['question']
body = cleaned_data['body']
question = get_object_or_404(Question, id=qid)
answer = Answer()
answer.uid = request.user.id
answer.question = question
answer.body = body
answer.save()
if question.uid != request.user.id:
notification = Notification()
notification.uid = question.uid
notification.pid = request.user.id
notification.qid = qid
notification.aid = answer.id
notification.save()
# Sending email when an answer is posted and user answering the question has verified role in spoken
try:
ans_user = User.objects.get(id=answer.uid)
user_has_role = has_role(ans_user)
if user_has_role:
user = User.objects.get(id=question.uid)
subject = 'Question has been answered'
message = """
Dear {0}<br><br>
Your question titled <b>"{1}"</b> has been answered.<br>
Link: {2}<br><br>
Regards,<br>
Spoken Tutorial Forums
""".format(
user.username,
question.title,
'http://forums.spoken-tutorial.org/question/' + str(question.id) + "#answer" + str(answer.id)
)
email = EmailMultiAlternatives(
subject, '', 'forums',
[user.email],
headers={"Content-type": "text/html;charset=iso-8859-1"}
)
email.attach_alternative(message, "text/html")
email.send(fail_silently=True)
# End of email send
except User.DoesNotExist as e:
pass
return HttpResponseRedirect('/question/' + str(qid) + "#answer" + str(answer.id))
return HttpResponseRedirect('/')
@login_required
def answer_comment(request):
if request.method == 'POST':
answer_id = request.POST['answer_id']
body = request.POST['body']
answer = get_object_or_404(Answer, pk=answer_id)
comment = AnswerComment()
comment.uid = request.user.id
comment.answer = answer
comment.body = body
comment.save()
# notifying the answer owner
if answer.uid != request.user.id:
notification = Notification()
notification.uid = answer.uid
notification.pid = request.user.id
notification.qid = answer.question.id
notification.aid = answer.id
notification.cid = comment.id
notification.save()
user = User.objects.get(id=answer.uid)
subject = 'Comment for your answer'
message = """
Dear {0}<br><br>
A comment has been posted on your answer.<br>
Link: {1}<br><br>
Regards,<br>
Spoken Tutorial Forums
""".format(
user.username,
"http://forums.spoken-tutorial.org/question/" + str(answer.question.id) + "#answer" + str(answer.id)
)
forums_mail(user.email, subject, message)
# notifying other users in the comment thread
uids = answer.answercomment_set.filter(answer=answer).values_list('uid', flat=True)
# getting distinct uids
uids = set(uids)
uids.remove(request.user.id)
for uid in uids:
notification = Notification()
notification.uid = uid
notification.pid = request.user.id
notification.qid = answer.question.id
notification.aid = answer.id
notification.cid = comment.id
notification.save()
user = User.objects.get(id=uid)
subject = 'Comment has a reply'
message = """
Dear {0}<br><br>
A reply has been posted on your comment.<br>
Link: {1}<br><br>
Regards,<br>
Spoken Tutorial Forums
""".format(
user.username,
"http://forums.spoken-tutorial.org/question/" + str(answer.question.id) + "#answer" + str(answer.id)
)
forums_mail(user.email, subject, message)
return HttpResponseRedirect("/question/" + str(answer.question.id) + "#")
def filter(request, category=None, tutorial=None, minute_range=None, second_range=None):
context = {
'category': category,
'tutorial': tutorial,
'minute_range': minute_range,
'second_range': second_range
}
if category and tutorial and minute_range and second_range:
questions = Question.objects.filter(category=category).filter(tutorial=tutorial).filter(
minute_range=minute_range).filter(second_range=second_range, status=1)
elif tutorial is None:
questions = Question.objects.filter(category=category, status=1)
elif minute_range is None:
questions = Question.objects.filter(category=category).filter(tutorial=tutorial, status=1)
else: # second_range is None
questions = Question.objects.filter(category=category).filter(
tutorial=tutorial).filter(minute_range=minute_range, status=1)
if 'qid' in request.GET:
context['qid'] = int(request.GET['qid'])
#context['questions'] = questions.order_by('category', 'tutorial', 'minute_range', 'second_range')
questions = questions.annotate(total_answers=Count('answer'))
questions = questions.order_by('-date_created', 'category', 'tutorial')
raw_get_data = request.GET.get('o', None)
header = {
1: SortableHeader('category', True, 'Foss'),
2: SortableHeader('tutorial', True, 'Tutorial Name'),
3: SortableHeader('minute_range', True, 'Mins'),
4: SortableHeader('second_range', True, 'Secs'),
5: SortableHeader('title', True, 'Title'),
6: SortableHeader('date_created', True, 'Date'),
7: SortableHeader('views', True, 'Views'),
8: SortableHeader('total_answers', 'True', 'Answers'),
9: SortableHeader('username', False, 'User')
}
tmp_recs = get_sorted_list(request, questions, header, raw_get_data)
ordering = get_field_index(raw_get_data)
paginator = Paginator(tmp_recs, 20)
page = request.GET.get('page')
try:
questions = paginator.page(page)
except PageNotAnInteger:
questions = paginator.page(1)
except EmptyPage:
questions = paginator.page(paginator.num_pages)
context = {
'questions': questions,
'header': header,
'ordering': ordering
}
return render(request, 'website/templates/filter.html', context)
def has_role(user):
flag = user.is_authenticated and user.groups.exists()
if not flag:
flag = Participant.objects.filter(user_id=user.id).exists()
return flag
@login_required
def new_question(request):
context = {}
if request.method == 'POST':
# check if user has a role
# user_has_role = request.user.is_authenticated and request.user.groups.exists()
user_has_role = has_role(request.user)
# only require captcha for users without a role
if not user_has_role:
recaptcha_response = request.POST.get('g-recaptcha-response', '')
if not recaptcha_response:
messages.error(request, "Please complete the reCAPTCHA verification.")
form = NewQuestionForm(request.POST)
context['form'] = form
context['recaptcha_site_key'] = settings.RECAPTCHA_SITE_KEY
context['require_recaptcha'] = True
context.update(csrf(request))
return render(request, 'website/templates/new-question.html', context)
# verify with google
recaptcha_verification_url = "https://www.google.com/recaptcha/api/siteverify"
recaptcha_data = {
'secret': settings.RECAPTCHA_SECRET_KEY,
'response': recaptcha_response
}
try:
recaptcha_result = requests.post(recaptcha_verification_url, data=recaptcha_data, timeout=5)
recaptcha_result.raise_for_status()
recaptcha_json = recaptcha_result.json()
except requests.RequestException as e:
messages.error(request, "Error verifying reCAPTCHA. Please try again.")
form = NewQuestionForm(request.POST)
context['form'] = form
context['recaptcha_site_key'] = settings.RECAPTCHA_SITE_KEY
context['require_recaptcha'] = True
context.update(csrf(request))
return render(request, 'website/templates/new-question.html', context)
# check if verification was successful
if not recaptcha_json.get('success', False):
messages.error(request, "reCAPTCHA verification failed. Please try again.")
form = NewQuestionForm(request.POST)
context['form'] = form
context['recaptcha_site_key'] = settings.RECAPTCHA_SITE_KEY
context['require_recaptcha'] = True
context.update(csrf(request))
return render(request, 'website/templates/new-question.html', context)
form = NewQuestionForm(request.POST)
if form.is_valid():
cleaned_data = form.cleaned_data
question = Question()
question.uid = request.user.id
question.category = cleaned_data['category'].replace(' ', '-')
question.tutorial = cleaned_data['tutorial'].replace(' ', '-')
question.minute_range = cleaned_data['minute_range']
question.second_range = cleaned_data['second_range']
question.title = cleaned_data['title']
question.body = cleaned_data['body']
question.views = 1
if not user_has_role:
question.status = 2 # mark as spam by default for non verified users
question.save()
#send update mail only for verified users, to avoid spam mail
if user_has_role:
subject = 'New Forum Question'
message = f"""
The following new question has been posted in the Spoken Tutorial Forum: <br>
Title: <b>{question.title}</b><br>
Category: <b>{question.category}</b><br>
Tutorial: <b>{question.tutorial}</b><br>
Link: <a href="http://forums.spoken-tutorial.org/question/{question.id}">
http://forums.spoken-tutorial.org/question/{question.id}
</a><br>
Question: <b>{question.body}</b><br>
"""
email = EmailMultiAlternatives(
subject, '', 'forums',
['team@spoken-tutorial.org', 'team@fossee.in'],
headers={"Content-type": "text/html;charset=iso-8859-1"}
)
email.attach_alternative(message, "text/html")
email.send(fail_silently=True)
return HttpResponseRedirect('/')
# If form not valid -> re-render with errors
context['form'] = form
context['recaptcha_site_key'] = settings.RECAPTCHA_SITE_KEY
context['require_recaptcha'] = not has_role(request.user)
context.update(csrf(request))
return render(request, 'website/templates/new-question.html', context)
else:
# GET request -> render empty form
category = request.GET.get('category', None)
tutorial = request.GET.get('tutorial', None)
minute_range = request.GET.get('minute_range', None)
second_range = request.GET.get('second_range', None)
# pass minute_range and second_range value to NewQuestionForm to populate on select
form = NewQuestionForm(category=category, tutorial=tutorial,
minute_range=minute_range, second_range=second_range)
context['category'] = category
context['form'] = form
context['recaptcha_site_key'] = settings.RECAPTCHA_SITE_KEY
# check if user needs to complete captcha
# user_has_role = request.user.is_authenticated and request.user.groups.exists()
user_has_role = has_role(request.user)
context['require_recaptcha'] = not user_has_role
context.update(csrf(request))
return render(request, 'website/templates/new-question.html', context)
# Notification Section
@login_required
def user_questions(request, user_id):
marker = 0
if 'marker' in request.GET:
marker = int(request.GET['marker'])
if str(user_id) == str(request.user.id):
total = Question.objects.filter(uid=user_id).count()
total = int(total - (total % 10 - 10))
questions = Question.objects.filter(uid=user_id).order_by('date_created').reverse()[marker:marker + 10]
context = {
'questions': questions,
'total': total,
'marker': marker
}
return render(request, 'website/templates/user-questions.html', context)
return HttpResponse("go away")
@login_required
def user_answers(request, user_id):
marker = 0
if 'marker' in request.GET:
marker = int(request.GET['marker'])
if str(user_id) == str(request.user.id):
total = Answer.objects.filter(uid=user_id).count()
total = int(total - (total % 10 - 10))
answers = (
Answer.objects.filter(uid=user_id)
.select_related('question')
.order_by('-date_created')[marker:marker + 10]
)
context = {
'answers': answers,
'total': total,
'marker': marker
}
return render(request, 'website/templates/user-answers.html', context)
return HttpResponse("go away")
@login_required
def user_notifications(request, user_id):
if str(user_id) == str(request.user.id):
notifications = list(
Notification.objects.filter(uid=user_id).order_by('-date_created')
)
# Prefetch related questions, answers and posters in bulk
qids = {n.qid for n in notifications if n.qid}
aids = {n.aid for n in notifications if n.aid}
pids = {n.pid for n in notifications if n.pid}
questions = {q.id: q for q in Question.objects.filter(id__in=qids)}
answers = {a.id: a for a in Answer.objects.filter(id__in=aids)}
users = {u.id: u.username for u in User.objects.filter(id__in=pids)}
for n in notifications:
n.cached_question = questions.get(n.qid)
n.cached_answer = answers.get(n.aid)
n.cached_poster = users.get(n.pid, "Unknown User")
context = {
'notifications': notifications,
}
return render(request, 'website/templates/notifications.html', context)
return HttpResponse("go away ...")
@login_required
def clear_notifications(request):
Notification.objects.filter(uid=request.user.id).delete()
return HttpResponseRedirect("/user/{0}/notifications/".format(request.user.id))
def search(request):
categories = _get_home_categories()
context = {
'categories': categories
}
return render(request, 'website/templates/search.html', context)
# Ajax Section
# All the ajax views go below
def ajax_category(request):
categories = _get_home_categories()
context = {
'categories': categories
}
return render(request, 'website/templates/ajax_categories.html', context)
def ajax_tutorials(request):
if request.method == 'POST':
category = request.POST.get('category')
tutorials = TutorialDetails.objects.using('spoken').filter(
foss__foss=category).order_by('level', 'order')
context = {
'tutorials': tutorials
}
return render(request, 'website/templates/ajax-tutorials.html', context)
def ajax_duration(request):
if request.method == 'POST':
category = request.POST['category']
tutorial = request.POST['tutorial']
video_detail = TutorialDetails.objects.using('spoken').get(
Q(foss__foss=category),
Q(tutorial=tutorial)
)
video_resource = TutorialResources.objects.using('spoken').get(
Q(tutorial_detail_id=video_detail.id),
Q(language__name='English')
)
video_path = '{0}/{1}/{2}/{3}'.format(
settings.VIDEO_PATH,
str(video_detail.foss_id),
str(video_detail.id),
video_resource.video
)
video_info = get_video_info(video_path)
# convert minutes to 1 if less than 0
# convert seconds to nearest upper 10th number eg(23->30)
minutes = video_info['minutes']
seconds = video_info['seconds']
if minutes < 0:
minutes = 1
seconds = int(seconds - (seconds % 10 - 10))
seconds = 60
context = {
'minutes': minutes,
'seconds': seconds,
}
return render(request, 'website/templates/ajax-duration.html', context)
@login_required
def ajax_question_update(request):
if request.method == 'POST':
qid = request.POST['question_id']
title = request.POST['question_title']
body = request.POST['question_body']
question = get_object_or_404(Question, pk=qid)
if can_edit(user=request.user, obj=question) or can_hide_delete(user=request.user, obj=question):
question.title = title
question.body = body
question.save()
return HttpResponse("saved")
return HttpResponseForbidden("Not Authorised")
@login_required
def ajax_details_update(request):
if request.method == 'POST':
qid = request.POST['qid']
category = request.POST['category']
category = category.replace(' ', '-')
tutorial = request.POST['tutorial']
tutorial = tutorial.replace(' ', '-')
minute_range = request.POST['minute_range']
second_range = request.POST['second_range']
question = get_object_or_404(Question, pk=qid)
if can_edit(user=request.user, obj=question) or can_hide_delete(user=request.user, obj=question):
question.category = category
question.tutorial = tutorial
question.minute_range = minute_range
question.second_range = second_range
question.save()
return HttpResponse("saved")
return HttpResponseForbidden("Not Authorised")
@login_required
def ajax_answer_update(request):
if request.method == 'POST':
aid = request.POST['answer_id']
body = request.POST['answer_body']
answer = get_object_or_404(Answer, pk=aid)
if can_edit(user=request.user, obj=answer):
answer.body = body
answer.save()
return HttpResponse("saved")
return HttpResponseForbidden("Not Authorised")
@login_required
def ajax_answer_delete(request):
if request.method == 'POST':
aid = request.POST['answer_id']
answer = get_object_or_404(Answer, pk=aid)
if can_edit(user=request.user, obj=answer):
answer.delete()
return HttpResponse("deleted")
return HttpResponseForbidden("Not Authorised")
@login_required
def ajax_answer_comment_update(request):
if request.method == "POST":
comment_id = request.POST["comment_id"]
comment_body = request.POST["comment_body"]
comment = get_object_or_404(AnswerComment, pk=comment_id)
if can_edit(user=request.user, obj=comment):
comment.body = comment_body
comment.save()
return HttpResponse("saved")
return HttpResponseForbidden("Not Authorised")
def ajax_similar_questions(request):
if request.method == 'POST':
category = request.POST['category'].replace(' ','-')
tutorial = request.POST['tutorial'].replace(' ','-')
title = request.POST['title']
user_title = clean_user_data(title)
# Increase the threshold as the Forums questions increase
THRESHOLD = 0.3
MAX_CANDIDATES = 200
MAX_RESULTS = 20
top_ques = []
# Limit number of candidate questions to keep CPU usage bounded
questions = Question.objects.filter(
category=category,
tutorial=tutorial,
).order_by('-date_created')[:MAX_CANDIDATES]
for question in questions:
question.similarity = get_similar_questions(user_title, question.title)
if question.similarity >= THRESHOLD:
top_ques.append(question)
top_ques = sorted(top_ques, key=lambda x: x.similarity, reverse=True)[:MAX_RESULTS]
context = {
'questions': top_ques,
'questions_count':len(top_ques)
}
return render(request, 'website/templates/ajax-similar-questions.html', context)
@login_required
def ajax_notification_remove(request):
if request.method == "POST":
nid = request.POST["notification_id"]
notification = get_object_or_404(Notification, pk=nid)
if notification.uid == request.user.id:
notification.delete()
return HttpResponse("removed")
return HttpResponseForbidden("failed")
@login_required
def ajax_delete_question(request):
result = False
if request.method == "POST":
key = request.POST['question_id']
question = get_object_or_404(Question, pk=key)
if can_edit(user=request.user, obj=question) or can_hide_delete(user=request.user, obj=question):
question.delete()
result = True
return HttpResponse(json.dumps(result), mimetype='application/json')
@login_required
def ajax_hide_question(request):
result = False
if request.method == "POST":
key = request.POST['question_id']
question = get_object_or_404(Question, pk=key)
if can_edit(user=request.user, obj=question) or can_hide_delete(user=request.user, obj=question):
question.status = 0
if request.POST['status'] in ('0', '2'):
question.status = 1
question.save()
result = True
# return HttpResponse(json.dumps(result), mimetype='application/json')
return HttpResponse(json.dumps(result))
def ajax_keyword_search(request):
if request.method == "POST":
key = request.POST['key']
questions = Question.objects.filter(
Q(title__icontains=key) | Q(category__icontains=key) |
Q(tutorial__icontains=key) | Q(body__icontains=key), status=1
).annotate(total_answers=Count('answer')).order_by('-date_created')
paginator = Paginator(questions, 20)
page = request.POST.get('page')
try:
questions = paginator.page(page)
except PageNotAnInteger:
questions = paginator.page(1)
except EmptyPage:
questions = paginator.page(paginator.num_pages)
# Attach cached usernames for the current page
uids = {q.uid for q in questions}
users = {u.id: u.username for u in User.objects.filter(id__in=uids)}
for q in questions:
q.cached_user = users.get(q.uid, "Unknown User")
context = {
'questions': questions
}
return render(request, 'website/templates/ajax-keyword-search.html', context)
def ajax_time_search(request):