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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
|
"""
:synopsis: user-centric views for askbot
This module includes all views that are specific to a given user - his or her profile,
and other views showing profile-related information.
Also this module includes the view listing all forum users.
"""
import calendar
import collections
import functools
import datetime
import logging
import operator
from django.db.models import Count
from django.conf import settings as django_settings
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, HttpResponseForbidden
from django.http import HttpResponseRedirect, Http404
from django.utils.translation import ugettext as _
from django.utils import simplejson
from django.views.decorators import csrf
from askbot.utils.slug import slugify
from askbot.utils.html import sanitize_html
from askbot.mail import send_mail
from askbot.utils.http import get_request_info
from askbot.utils import functions
from askbot import forms
from askbot import const
from askbot.conf import settings as askbot_settings
from askbot import models
from askbot import exceptions
from askbot.models.badges import award_badges_signal
from askbot.models.tag import get_global_group
from askbot.models.tag import get_groups
from askbot.skins.loaders import render_into_skin
from askbot.search.state_manager import SearchState
from askbot.utils import url_utils
from askbot.utils.loading import load_module
def owner_or_moderator_required(f):
@functools.wraps(f)
def wrapped_func(request, profile_owner, context):
if profile_owner == request.user:
pass
elif request.user.is_authenticated() and request.user.can_moderate_user(profile_owner):
pass
else:
params = '?next=%s' % request.path
return HttpResponseRedirect(url_utils.get_login_url() + params)
return f(request, profile_owner, context)
return wrapped_func
def show_users(request, by_group=False, group_id=None, group_slug=None):
"""Users view, including listing of users by group"""
if askbot_settings.GROUPS_ENABLED and not by_group:
default_group = get_global_group()
group_slug = slugify(default_group.name)
new_url = reverse('users_by_group',
kwargs={'group_id': default_group.id,
'group_slug': group_slug})
return HttpResponseRedirect(new_url)
users = models.User.objects.exclude(status = 'b')
group = None
group_email_moderation_enabled = False
user_can_join_group = False
user_is_group_member = False
if by_group == True:
if askbot_settings.GROUPS_ENABLED == False:
raise Http404
if group_id:
if all((group_id, group_slug)) == False:
return HttpResponseRedirect('groups')
else:
try:
group = models.Tag.group_tags.get(id = group_id)
group_email_moderation_enabled = \
(
askbot_settings.GROUP_EMAIL_ADDRESSES_ENABLED \
and askbot_settings.ENABLE_CONTENT_MODERATION
)
user_can_join_group = group.group_profile.can_accept_user(request.user)
except models.Tag.DoesNotExist:
raise Http404
if group_slug == slugify(group.name):
users = users.filter(
group_memberships__group__id = group_id
)
if request.user.is_authenticated():
user_is_group_member = bool(
users.filter(
id = request.user.id
).count()
)
else:
group_page_url = reverse(
'users_by_group',
kwargs = {
'group_id': group.id,
'group_slug': slugify(group.name)
}
)
return HttpResponseRedirect(group_page_url)
is_paginated = True
sortby = request.GET.get('sort', 'reputation')
if askbot_settings.KARMA_MODE == 'private' and sortby == 'reputation':
sortby = 'newest'
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
search_query = request.REQUEST.get('query', "")
if search_query == "":
if sortby == "newest":
order_by_parameter = '-date_joined'
elif sortby == "last":
order_by_parameter = 'date_joined'
elif sortby == "user":
order_by_parameter = 'username'
else:
# default
order_by_parameter = '-reputation'
objects_list = Paginator(
users.order_by(order_by_parameter),
const.USERS_PAGE_SIZE
)
base_url = request.path + '?sort=%s&' % sortby
else:
sortby = "reputation"
matching_users = models.get_users_by_text_query(search_query, users)
objects_list = Paginator(
matching_users.order_by('-reputation'),
const.USERS_PAGE_SIZE
)
base_url = request.path + '?name=%s&sort=%s&' % (search_query, sortby)
try:
users_page = objects_list.page(page)
except (EmptyPage, InvalidPage):
users_page = objects_list.page(objects_list.num_pages)
paginator_data = {
'is_paginated' : is_paginated,
'pages': objects_list.num_pages,
'page': page,
'has_previous': users_page.has_previous(),
'has_next': users_page.has_next(),
'previous': users_page.previous_page_number(),
'next': users_page.next_page_number(),
'base_url' : base_url
}
paginator_context = functions.setup_paginator(paginator_data) #
if askbot_settings.GROUPS_ENABLED:
#todo: cleanup this branched code after groups are migrated to auth_group
user_groups = get_groups().exclude(name__startswith='_internal_')
if len(user_groups) <= 1:
assert(user_groups[0].name == askbot_settings.GLOBAL_GROUP_NAME)
user_groups = None
else:
user_groups = None
data = {
'active_tab': 'users',
'page_class': 'users-page',
'users' : users_page,
'group': group,
'search_query' : search_query,
'tab_id' : sortby,
'paginator_context' : paginator_context,
'group_email_moderation_enabled': group_email_moderation_enabled,
'user_can_join_group': user_can_join_group,
'user_is_group_member': user_is_group_member,
'user_groups': user_groups
}
return render_into_skin('users.html', data, request)
@csrf.csrf_protect
def user_moderate(request, subject, context):
"""user subview for moderation
"""
moderator = request.user
if moderator.is_authenticated() and not moderator.can_moderate_user(subject):
raise Http404
user_rep_changed = False
user_status_changed = False
message_sent = False
email_error_message = None
user_rep_form = forms.ChangeUserReputationForm()
send_message_form = forms.SendMessageForm()
if request.method == 'POST':
if 'change_status' in request.POST:
user_status_form = forms.ChangeUserStatusForm(
request.POST,
moderator = moderator,
subject = subject
)
if user_status_form.is_valid():
subject.set_status( user_status_form.cleaned_data['user_status'] )
user_status_changed = True
elif 'send_message' in request.POST:
send_message_form = forms.SendMessageForm(request.POST)
if send_message_form.is_valid():
subject_line = send_message_form.cleaned_data['subject_line']
body_text = send_message_form.cleaned_data['body_text']
try:
send_mail(
subject_line = subject_line,
body_text = body_text,
recipient_list = [subject.email],
headers={'Reply-to':moderator.email},
raise_on_failure = True
)
message_sent = True
except exceptions.EmailNotSent, e:
email_error_message = unicode(e)
send_message_form = forms.SendMessageForm()
else:
reputation_change_type = None
if 'subtract_reputation' in request.POST:
rep_change_type = 'subtract'
elif 'add_reputation' in request.POST:
rep_change_type = 'add'
else:
raise Http404
user_rep_form = forms.ChangeUserReputationForm(request.POST)
if user_rep_form.is_valid():
rep_delta = user_rep_form.cleaned_data['user_reputation_delta']
comment = user_rep_form.cleaned_data['comment']
if rep_change_type == 'subtract':
rep_delta = -1 * rep_delta
moderator.moderate_user_reputation(
user = subject,
reputation_change = rep_delta,
comment = comment,
timestamp = datetime.datetime.now(),
)
#reset form to preclude accidentally repeating submission
user_rep_form = forms.ChangeUserReputationForm()
user_rep_changed = True
#need to re-initialize the form even if it was posted, because
#initial values will most likely be different from the previous
user_status_form = forms.ChangeUserStatusForm(
moderator = moderator,
subject = subject
)
data = {
'active_tab': 'users',
'page_class': 'user-profile-page',
'tab_name': 'moderation',
'tab_description': _('moderate this user'),
'page_title': _('moderate user'),
'change_user_status_form': user_status_form,
'change_user_reputation_form': user_rep_form,
'send_message_form': send_message_form,
'message_sent': message_sent,
'email_error_message': email_error_message,
'user_rep_changed': user_rep_changed,
'user_status_changed': user_status_changed
}
context.update(data)
return render_into_skin('user_profile/user_moderate.html', context, request)
#non-view function
def set_new_email(user, new_email, nomessage=False):
if new_email != user.email:
user.email = new_email
user.email_isvalid = False
user.save()
#if askbot_settings.EMAIL_VALIDATION == True:
# send_new_email_key(user,nomessage=nomessage)
@login_required
@csrf.csrf_protect
def edit_user(request, id):
"""View that allows to edit user profile.
This view is accessible to profile owners or site administrators
"""
user = get_object_or_404(models.User, id=id)
if not(request.user == user or request.user.is_superuser):
raise Http404
if request.method == "POST":
form = forms.EditUserForm(user, request.POST)
if form.is_valid():
new_email = sanitize_html(form.cleaned_data['email'])
set_new_email(user, new_email)
if askbot_settings.EDITABLE_SCREEN_NAME:
user.username = sanitize_html(form.cleaned_data['username'])
user.real_name = sanitize_html(form.cleaned_data['realname'])
user.website = sanitize_html(form.cleaned_data['website'])
user.location = sanitize_html(form.cleaned_data['city'])
user.date_of_birth = form.cleaned_data.get('birthday', None)
user.about = sanitize_html(form.cleaned_data['about'])
user.country = form.cleaned_data['country']
user.show_country = form.cleaned_data['show_country']
user.show_marked_tags = form.cleaned_data['show_marked_tags']
user.save()
# send user updated signal if full fields have been updated
award_badges_signal.send(None,
event = 'update_user_profile',
actor = user,
context_object = user
)
return HttpResponseRedirect(user.get_profile_url())
else:
form = forms.EditUserForm(user)
data = {
'active_tab': 'users',
'page_class': 'user-profile-edit-page',
'form' : form,
'marked_tags_setting': askbot_settings.MARKED_TAGS_ARE_PUBLIC_WHEN,
'support_custom_avatars': ('avatar' in django_settings.INSTALLED_APPS),
'view_user': user,
}
return render_into_skin('user_profile/user_edit.html', data, request)
def user_stats(request, user, context):
question_filter = {}
if request.user != user:
question_filter['is_anonymous'] = False
if askbot_settings.ENABLE_CONTENT_MODERATION:
question_filter['approved'] = True
#
# Questions
#
questions = user.posts.get_questions().filter(**question_filter).\
order_by('-score', '-thread__last_activity_at').\
select_related('thread', 'thread__last_activity_by')[:100]
#added this if to avoid another query if questions is less than 100
if len(questions) < 100:
question_count = len(questions)
else:
question_count = user.posts.get_questions().filter(**question_filter).count()
#
# Top answers
#
top_answers = user.posts.get_answers(
request.user
).filter(
deleted=False,
thread__posts__deleted=False,
thread__posts__post_type='question',
).select_related(
'thread'
).order_by(
'-score', '-added_at'
)[:100]
top_answer_count = len(top_answers)
#
# Votes
#
up_votes = models.Vote.objects.get_up_vote_count_from_user(user)
down_votes = models.Vote.objects.get_down_vote_count_from_user(user)
votes_today = models.Vote.objects.get_votes_count_today_from_user(user)
votes_total = askbot_settings.MAX_VOTES_PER_USER_PER_DAY
#
# Tags
#
# INFO: There's bug in Django that makes the following query kind of broken (GROUP BY clause is problematic):
# http://stackoverflow.com/questions/7973461/django-aggregation-does-excessive-group-by-clauses
# Fortunately it looks like it returns correct results for the test data
user_tags = models.Tag.objects.filter(threads__posts__author=user).distinct().\
annotate(user_tag_usage_count=Count('threads')).\
order_by('-user_tag_usage_count')[:const.USER_VIEW_DATA_SIZE]
user_tags = list(user_tags) # evaluate
when = askbot_settings.MARKED_TAGS_ARE_PUBLIC_WHEN
if when == 'always' or \
(when == 'when-user-wants' and user.show_marked_tags == True):
#refactor into: user.get_marked_tag_names('good'/'bad'/'subscribed')
interesting_tag_names = user.get_marked_tag_names('good')
ignored_tag_names = user.get_marked_tag_names('bad')
subscribed_tag_names = user.get_marked_tag_names('subscribed')
else:
interesting_tag_names = None
ignored_tag_names = None
subscribed_tag_names = None
# tags = models.Post.objects.filter(author=user).values('id', 'thread', 'thread__tags')
# post_ids = set()
# thread_ids = set()
# tag_ids = set()
# for t in tags:
# post_ids.add(t['id'])
# thread_ids.add(t['thread'])
# tag_ids.add(t['thread__tags'])
# if t['thread__tags'] == 11:
# print t['thread'], t['id']
# import ipdb; ipdb.set_trace()
#
# Badges/Awards (TODO: refactor into Managers/QuerySets when a pattern emerges; Simplify when we get rid of Question&Answer models)
#
post_type = ContentType.objects.get_for_model(models.Post)
user_awards = models.Award.objects.filter(user=user).select_related('badge')
awarded_post_ids = []
for award in user_awards:
if award.content_type_id == post_type.id:
awarded_post_ids.append(award.object_id)
awarded_posts = models.Post.objects.filter(id__in=awarded_post_ids)\
.select_related('thread') # select related to avoid additional queries in Post.get_absolute_url()
awarded_posts_map = {}
for post in awarded_posts:
awarded_posts_map[post.id] = post
badges_dict = collections.defaultdict(list)
for award in user_awards:
# Fetch content object
if award.content_type_id == post_type.id:
#here we go around a possibility of awards
#losing the content objects when the content
#objects are deleted for some reason
awarded_post = awarded_posts_map.get(award.object_id, None)
if awarded_post is not None:
#protect from awards that are associated with deleted posts
award.content_object = awarded_post
award.content_object_is_post = True
else:
award.content_object_is_post = False
else:
award.content_object_is_post = False
# "Assign" to its Badge
badges_dict[award.badge].append(award)
badges = badges_dict.items()
badges.sort(key=operator.itemgetter(1), reverse=True)
user_groups = models.Tag.group_tags.get_for_user(user = user)
user_groups = user_groups.exclude(name__startswith='_internal_')
global_group = get_global_group()
user_groups = user_groups.exclude(name=global_group.name)
if request.user == user:
groups_membership_info = user.get_groups_membership_info(user_groups)
else:
groups_membership_info = collections.defaultdict()
data = {
'active_tab':'users',
'page_class': 'user-profile-page',
'support_custom_avatars': ('avatar' in django_settings.INSTALLED_APPS),
'tab_name' : 'stats',
'tab_description' : _('user profile'),
'page_title' : _('user profile overview'),
'user_status_for_display': user.get_status_display(soft = True),
'questions' : questions,
'question_count': question_count,
'top_answers': top_answers,
'top_answer_count': top_answer_count,
'up_votes' : up_votes,
'down_votes' : down_votes,
'total_votes': up_votes + down_votes,
'votes_today_left': votes_total - votes_today,
'votes_total_per_day': votes_total,
'user_tags' : user_tags,
'user_groups': user_groups,
'groups_membership_info': groups_membership_info,
'interesting_tag_names': interesting_tag_names,
'ignored_tag_names': ignored_tag_names,
'subscribed_tag_names': subscribed_tag_names,
'badges': badges,
'total_badges' : len(badges),
}
context.update(data)
return render_into_skin('user_profile/user_stats.html', context, request)
def user_recent(request, user, context):
def get_type_name(type_id):
for item in const.TYPE_ACTIVITY:
if type_id in item:
return item[1]
class Event(object):
is_badge = False
def __init__(self, time, type, title, summary, answer_id, question_id):
self.time = time
self.type = get_type_name(type)
self.type_id = type
self.title = title
self.summary = summary
slug_title = slugify(title)
self.title_link = reverse(
'question',
kwargs={'id':question_id}
) + u'%s' % slug_title
if int(answer_id) > 0:
self.title_link += '#%s' % answer_id
class AwardEvent(object):
is_badge = True
def __init__(self, time, type, content_object, badge):
self.time = time
self.type = get_type_name(type)
self.content_object = content_object
self.badge = badge
activities = []
# TODO: Don't process all activities here for the user, only a subset ([:const.USER_VIEW_DATA_SIZE])
for activity in models.Activity.objects.filter(user=user):
# TODO: multi-if means that we have here a construct for which a design pattern should be used
# ask questions
if activity.activity_type == const.TYPE_ACTIVITY_ASK_QUESTION:
q = activity.content_object
if q.deleted:
activities.append(Event(
time=activity.active_at,
type=activity.activity_type,
title=q.thread.title,
summary='', #q.summary, # TODO: was set to '' before, but that was probably wrong
answer_id=0,
question_id=q.id
))
elif activity.activity_type == const.TYPE_ACTIVITY_ANSWER:
ans = activity.content_object
question = ans.thread._question_post()
if not ans.deleted and not question.deleted:
activities.append(Event(
time=activity.active_at,
type=activity.activity_type,
title=ans.thread.title,
summary=question.summary,
answer_id=ans.id,
question_id=question.id
))
elif activity.activity_type == const.TYPE_ACTIVITY_COMMENT_QUESTION:
cm = activity.content_object
q = cm.parent
assert q.is_question()
if not q.deleted:
activities.append(Event(
time=cm.added_at,
type=activity.activity_type,
title=q.thread.title,
summary='',
answer_id=0,
question_id=q.id
))
elif activity.activity_type == const.TYPE_ACTIVITY_COMMENT_ANSWER:
cm = activity.content_object
ans = cm.parent
assert ans.is_answer()
question = ans.thread._question_post()
if not ans.deleted and not question.deleted:
activities.append(Event(
time=cm.added_at,
type=activity.activity_type,
title=ans.thread.title,
summary='',
answer_id=ans.id,
question_id=question.id
))
elif activity.activity_type == const.TYPE_ACTIVITY_UPDATE_QUESTION:
q = activity.content_object
if not q.deleted:
activities.append(Event(
time=activity.active_at,
type=activity.activity_type,
title=q.thread.title,
summary=q.summary,
answer_id=0,
question_id=q.id
))
elif activity.activity_type == const.TYPE_ACTIVITY_UPDATE_ANSWER:
ans = activity.content_object
question = ans.thread._question_post()
if not ans.deleted and not question.deleted:
activities.append(Event(
time=activity.active_at,
type=activity.activity_type,
title=ans.thread.title,
summary=ans.summary,
answer_id=ans.id,
question_id=question.id
))
elif activity.activity_type == const.TYPE_ACTIVITY_MARK_ANSWER:
ans = activity.content_object
question = ans.thread._question_post()
if not ans.deleted and not question.deleted:
activities.append(Event(
time=activity.active_at,
type=activity.activity_type,
title=ans.thread.title,
summary='',
answer_id=0,
question_id=question.id
))
elif activity.activity_type == const.TYPE_ACTIVITY_PRIZE:
award = activity.content_object
if award is not None:#todo: work around halfa$$ comment deletion
activities.append(AwardEvent(
time=award.awarded_at,
type=activity.activity_type,
content_object=award.content_object,
badge=award.badge,
))
activities.sort(key=operator.attrgetter('time'), reverse=True)
data = {
'active_tab': 'users',
'page_class': 'user-profile-page',
'tab_name' : 'recent',
'tab_description' : _('recent user activity'),
'page_title' : _('profile - recent activity'),
'activities' : activities[:const.USER_VIEW_DATA_SIZE]
}
context.update(data)
return render_into_skin('user_profile/user_recent.html', context, request)
@owner_or_moderator_required
def user_responses(request, user, context):
"""
We list answers for question, comments, and
answer accepted by others for this user.
as well as mentions of the user
user - the profile owner
the view has two sub-views - "forum" - i.e. responses
and "flags" - moderation items for mods only
"""
#1) select activity types according to section
section = request.GET.get('section', 'forum')
if section == 'flags' and not\
(request.user.is_moderator() or request.user.is_administrator()):
raise Http404
if section == 'forum':
activity_types = const.RESPONSE_ACTIVITY_TYPES_FOR_DISPLAY
activity_types += (const.TYPE_ACTIVITY_MENTION,)
elif section == 'flags':
activity_types = (const.TYPE_ACTIVITY_MARK_OFFENSIVE,)
if askbot_settings.ENABLE_CONTENT_MODERATION:
activity_types += (
const.TYPE_ACTIVITY_MODERATED_NEW_POST,
const.TYPE_ACTIVITY_MODERATED_POST_EDIT
)
else:
raise Http404
#2) load the activity notifications according to activity types
#todo: insert pagination code here
memo_set = models.ActivityAuditStatus.objects.filter(
user = request.user,
activity__activity_type__in = activity_types
).select_related(
'activity',
'activity__content_type',
'activity__question__thread',
'activity__user',
'activity__user__gravatar',
).order_by(
'-activity__active_at'
)[:const.USER_VIEW_DATA_SIZE]
#3) "package" data for the output
response_list = list()
for memo in memo_set:
if memo.activity.content_object is None:
continue#a temp plug due to bug in the comment deletion
response = {
'id': memo.id,
'timestamp': memo.activity.active_at,
'user': memo.activity.user,
'is_new': memo.is_new(),
'response_url': memo.activity.get_absolute_url(),
'response_snippet': memo.activity.get_snippet(),
'response_title': memo.activity.question.thread.title,
'response_type': memo.activity.get_activity_type_display(),
'response_id': memo.activity.question.id,
'nested_responses': [],
'response_content': memo.activity.content_object.html,
}
response_list.append(response)
#4) sort by response id
response_list.sort(lambda x,y: cmp(y['response_id'], x['response_id']))
#5) group responses by thread (response_id is really the question post id)
last_response_id = None #flag to know if the response id is different
filtered_response_list = list()
for i, response in enumerate(response_list):
#todo: group responses by the user as well
if response['response_id'] == last_response_id:
original_response = dict.copy(filtered_response_list[len(filtered_response_list)-1])
original_response['nested_responses'].append(response)
filtered_response_list[len(filtered_response_list)-1] = original_response
else:
filtered_response_list.append(response)
last_response_id = response['response_id']
#6) sort responses by time
filtered_response_list.sort(lambda x,y: cmp(y['timestamp'], x['timestamp']))
reject_reasons = models.PostFlagReason.objects.all().order_by('title')
data = {
'active_tab':'users',
'page_class': 'user-profile-page',
'tab_name' : 'inbox',
'inbox_section':section,
'tab_description' : _('comments and answers to others questions'),
'page_title' : _('profile - responses'),
'post_reject_reasons': reject_reasons,
'responses' : filtered_response_list,
}
context.update(data)
return render_into_skin('user_profile/user_inbox.html', context, request)
def user_network(request, user, context):
if 'followit' not in django_settings.INSTALLED_APPS:
raise Http404
data = {
'tab_name': 'network',
'followed_users': user.get_followed_users(),
'followers': user.get_followers(),
}
context.update(data)
return render_into_skin('user_profile/user_network.html', context, request)
@owner_or_moderator_required
def user_votes(request, user, context):
all_votes = list(models.Vote.objects.filter(user=user))
votes = []
for vote in all_votes:
post = vote.voted_post
if post.is_question():
vote.title = post.thread.title
vote.question_id = post.id
vote.answer_id = 0
votes.append(vote)
elif post.is_answer():
vote.title = post.thread.title
vote.question_id = post.thread._question_post().id
vote.answer_id = post.id
votes.append(vote)
votes.sort(key=operator.attrgetter('id'), reverse=True)
data = {
'active_tab':'users',
'page_class': 'user-profile-page',
'tab_name' : 'votes',
'tab_description' : _('user vote record'),
'page_title' : _('profile - votes'),
'votes' : votes[:const.USER_VIEW_DATA_SIZE]
}
context.update(data)
return render_into_skin('user_profile/user_votes.html', context, request)
def user_reputation(request, user, context):
reputes = models.Repute.objects.filter(user=user).select_related('question', 'question__thread', 'user').order_by('-reputed_at')
# prepare data for the graph - last values go in first
rep_list = ['[%s,%s]' % (calendar.timegm(datetime.datetime.now().timetuple()) * 1000, user.reputation)]
for rep in reputes:
rep_list.append('[%s,%s]' % (calendar.timegm(rep.reputed_at.timetuple()) * 1000, rep.reputation))
reps = ','.join(rep_list)
reps = '[%s]' % reps
data = {
'active_tab':'users',
'page_class': 'user-profile-page',
'tab_name': 'reputation',
'tab_description': _('user karma'),
'page_title': _("Profile - User's Karma"),
'reputation': reputes,
'reps': reps
}
context.update(data)
return render_into_skin('user_profile/user_reputation.html', context, request)
def user_favorites(request, user, context):
favorite_threads = user.user_favorite_questions.values_list('thread', flat=True)
questions = models.Post.objects.filter(post_type='question', thread__in=favorite_threads)\
.select_related('thread', 'thread__last_activity_by')\
.order_by('-score', '-thread__last_activity_at')[:const.USER_VIEW_DATA_SIZE]
data = {
'active_tab':'users',
'page_class': 'user-profile-page',
'tab_name' : 'favorites',
'tab_description' : _('users favorite questions'),
'page_title' : _('profile - favorite questions'),
'questions' : questions,
}
context.update(data)
return render_into_skin('user_profile/user_favorites.html', context, request)
@owner_or_moderator_required
@csrf.csrf_protect
def user_email_subscriptions(request, user, context):
logging.debug(get_request_info(request))
if request.method == 'POST':
email_feeds_form = forms.EditUserEmailFeedsForm(request.POST)
tag_filter_form = forms.TagFilterSelectionForm(request.POST, instance=user)
if email_feeds_form.is_valid() and tag_filter_form.is_valid():
action_status = None
tag_filter_saved = tag_filter_form.save()
if tag_filter_saved:
action_status = _('changes saved')
if 'save' in request.POST:
feeds_saved = email_feeds_form.save(user)
if feeds_saved:
action_status = _('changes saved')
elif 'stop_email' in request.POST:
email_stopped = email_feeds_form.reset().save(user)
initial_values = forms.EditUserEmailFeedsForm.NO_EMAIL_INITIAL
email_feeds_form = forms.EditUserEmailFeedsForm(initial=initial_values)
if email_stopped:
action_status = _('email updates canceled')
else:
#user may have been created by some app that does not know
#about the email subscriptions, in that case the call below
#will add any subscription settings that are missing
#using the default frequencies
user.add_missing_askbot_subscriptions()
#initialize the form
email_feeds_form = forms.EditUserEmailFeedsForm()
email_feeds_form.set_initial_values(user)
tag_filter_form = forms.TagFilterSelectionForm(instance=user)
action_status = None
data = {
'active_tab': 'users',
'page_class': 'user-profile-page',
'tab_name': 'email_subscriptions',
'tab_description': _('email subscription settings'),
'page_title': _('profile - email subscriptions'),
'email_feeds_form': email_feeds_form,
'tag_filter_selection_form': tag_filter_form,
'action_status': action_status,
}
context.update(data)
return render_into_skin(
'user_profile/user_email_subscriptions.html',
context,
request
)
@csrf.csrf_protect
def user_custom_tab(request, user, context):
"""works only if `ASKBOT_CUSTOM_USER_PROFILE_TAB`
setting in the ``settings.py`` is properly configured"""
tab_settings = django_settings.ASKBOT_CUSTOM_USER_PROFILE_TAB
module_path = tab_settings['CONTENT_GENERATOR']
content_generator = load_module(module_path)
page_title = _('profile - %(section)s') % \
{'section': tab_settings['NAME']}
context.update({
'custom_tab_content': content_generator(request, user),
'tab_name': tab_settings['SLUG'],
'page_title': page_title
})
return render_into_skin('user_profile/custom_tab.html', context, request)
USER_VIEW_CALL_TABLE = {
'stats': user_stats,
'recent': user_recent,
'inbox': user_responses,
'network': user_network,
'reputation': user_reputation,
'favorites': user_favorites,
'votes': user_votes,
'email_subscriptions': user_email_subscriptions,
'moderation': user_moderate,
}
CUSTOM_TAB = getattr(django_settings, 'ASKBOT_CUSTOM_USER_PROFILE_TAB', None)
if CUSTOM_TAB:
CUSTOM_SLUG = CUSTOM_TAB['SLUG']
USER_VIEW_CALL_TABLE[CUSTOM_SLUG] = user_custom_tab
#todo: rename this function - variable named user is everywhere
def user(request, id, slug=None, tab_name=None):
"""Main user view function that works as a switchboard
id - id of the profile owner
todo: decide what to do with slug - it is not used
in the code in any way
"""
profile_owner = get_object_or_404(models.User, id = id)
if not tab_name:
tab_name = request.GET.get('sort', 'stats')
if askbot_settings.KARMA_MODE == 'public':
can_show_karma = True
elif askbot_settings.KARMA_MODE == 'hidden':
can_show_karma = False
else:
if request.user.is_anonymous():
can_show_karma = False
elif request.user.is_administrator_or_moderator():
can_show_karma = True
elif request.user == profile_owner:
can_show_karma = True
else:
can_show_karma = False
if can_show_karma == False and tab_name == 'reputation':
raise Http404
user_view_func = USER_VIEW_CALL_TABLE.get(tab_name, user_stats)
search_state = SearchState( # Non-default SearchState with user data set
scope=None,
sort=None,
query=None,
tags=None,
author=profile_owner.id,
page=None,
user_logged_in=profile_owner.is_authenticated(),
)
context = {
'view_user': profile_owner,
'can_show_karma': can_show_karma,
'search_state': search_state,
'user_follow_feature_on': ('followit' in django_settings.INSTALLED_APPS),
}
if CUSTOM_TAB:
context['custom_tab_name'] = CUSTOM_TAB['NAME']
context['custom_tab_slug'] = CUSTOM_TAB['SLUG']
return user_view_func(request, profile_owner, context)
@csrf.csrf_exempt
def update_has_custom_avatar(request):
"""updates current avatar type data for the user
"""
if request.is_ajax() and request.user.is_authenticated():
if request.user.avatar_type in ('n', 'g'):
request.user.update_avatar_type()
request.session['avatar_data_updated_at'] = datetime.datetime.now()
return HttpResponse(simplejson.dumps({'status':'ok'}), mimetype='application/json')
return HttpResponseForbidden()
def groups(request, id = None, slug = None):
"""output groups page
"""
if askbot_settings.GROUPS_ENABLED == False:
raise Http404
#6 lines of input cleaning code
if request.user.is_authenticated():
scope = request.GET.get('sort', 'all-groups')
if scope not in ('all-groups', 'my-groups'):
scope = 'all-groups'
else:
scope = 'all-groups'
if scope == 'all-groups':
groups = models.Tag.group_tags.get_all()
else:
groups = models.Tag.group_tags.get_for_user(
user = request.user
)
groups = groups.exclude(name__startswith='_internal_')
groups = groups.annotate(users_count=Count('user_memberships'))
groups = groups.select_related('group_profile')
user_can_add_groups = request.user.is_authenticated() and \
request.user.is_administrator_or_moderator()
groups_membership_info = collections.defaultdict()
if request.user.is_authenticated():
#collect group memberhship information
groups_membership_info = request.user.get_groups_membership_info(groups)
data = {
'groups': groups,
'groups_membership_info': groups_membership_info,
'user_can_add_groups': user_can_add_groups,
'active_tab': 'groups',#todo vars active_tab and tab_name are too similar
'tab_name': scope,
'page_class': 'groups-page'
}
return render_into_skin('groups.html', data, request)
|