1 from django.contrib.auth.decorators import login_required
\r
2 from forum.models import User
\r
3 from django.db.models import Q, Count
\r
4 from django.core.paginator import Paginator, EmptyPage, InvalidPage
\r
5 from django.template.defaultfilters import slugify
\r
6 from django.contrib.contenttypes.models import ContentType
\r
7 from django.core.urlresolvers import reverse
\r
8 from django.shortcuts import render_to_response, get_object_or_404
\r
9 from django.template import RequestContext
\r
10 from django.http import HttpResponse, HttpResponseForbidden, HttpResponseRedirect, Http404
\r
11 from django.utils.translation import ugettext as _
\r
12 from django.utils.http import urlquote_plus
\r
13 from django.utils.html import strip_tags
\r
14 from django.utils import simplejson
\r
15 from django.core.urlresolvers import reverse
\r
16 from forum.forms import *
\r
17 from forum.utils.html import sanitize_html
\r
18 from forum.authentication import user_updated
\r
19 from datetime import date
\r
24 USERS_PAGE_SIZE = 35# refactor - move to some constants file
\r
28 sortby = request.GET.get('sort', 'reputation')
\r
29 suser = request.REQUEST.get('q', "")
\r
31 page = int(request.GET.get('page', '1'))
\r
36 if sortby == "newest":
\r
37 objects_list = Paginator(User.objects.all().order_by('-date_joined'), USERS_PAGE_SIZE)
\r
38 elif sortby == "last":
\r
39 objects_list = Paginator(User.objects.all().order_by('date_joined'), USERS_PAGE_SIZE)
\r
40 elif sortby == "user":
\r
41 objects_list = Paginator(User.objects.all().order_by('username'), USERS_PAGE_SIZE)
\r
44 objects_list = Paginator(User.objects.all().order_by('-reputation'), USERS_PAGE_SIZE)
\r
45 base_url = reverse('users') + '?sort=%s&' % sortby
\r
47 sortby = "reputation"
\r
48 objects_list = Paginator(User.objects.filter(username__icontains=suser).order_by('-reputation'), USERS_PAGE_SIZE)
\r
49 base_url = reverse('users') + '?name=%s&sort=%s&' % (suser, sortby)
\r
52 users = objects_list.page(page)
\r
53 except (EmptyPage, InvalidPage):
\r
54 users = objects_list.page(objects_list.num_pages)
\r
56 return render_to_response('users/users.html', {
\r
62 'is_paginated' : is_paginated,
\r
63 'pages': objects_list.num_pages,
\r
65 'has_previous': users.has_previous(),
\r
66 'has_next': users.has_next(),
\r
67 'previous': users.previous_page_number(),
\r
68 'next': users.next_page_number(),
\r
69 'base_url' : base_url
\r
72 }, context_instance=RequestContext(request))
\r
75 def moderate_user(request, id):
\r
76 """ajax handler of user moderation
\r
78 if not request.user.is_superuser or request.method != 'POST':
\r
80 if not request.is_ajax():
\r
81 return HttpResponseForbidden(mimetype="application/json")
\r
83 user = get_object_or_404(User, id=id)
\r
84 form = ModerateUserForm(request.POST, instance=user)
\r
88 logging.debug('data saved')
\r
89 response = HttpResponse(simplejson.dumps(''), mimetype="application/json")
\r
91 response = HttpResponseForbidden(mimetype="application/json")
\r
94 def set_new_email(user, new_email, nomessage=False):
\r
95 if new_email != user.email:
\r
96 user.email = new_email
\r
97 user.email_isvalid = False
\r
99 #if settings.EMAIL_VALIDATION == 'on':
\r
100 # send_new_email_key(user,nomessage=nomessage)
\r
103 def edit_user(request, id):
\r
104 user = get_object_or_404(User, id=id)
\r
105 if request.user != user:
\r
107 if request.method == "POST":
\r
108 form = EditUserForm(user, request.POST)
\r
109 if form.is_valid():
\r
110 new_email = sanitize_html(form.cleaned_data['email'])
\r
112 set_new_email(user, new_email)
\r
114 #user.username = sanitize_html(form.cleaned_data['username'])
\r
115 user.real_name = sanitize_html(form.cleaned_data['realname'])
\r
116 user.website = sanitize_html(form.cleaned_data['website'])
\r
117 user.location = sanitize_html(form.cleaned_data['city'])
\r
118 user.date_of_birth = sanitize_html(form.cleaned_data['birthday'])
\r
119 if user.date_of_birth == "None":
\r
120 user.date_of_birth = '1900-01-01'
\r
121 user.about = sanitize_html(form.cleaned_data['about'])
\r
124 # send user updated signal if full fields have been updated
\r
125 if user.email and user.real_name and user.website and user.location and \
\r
126 user.date_of_birth and user.about:
\r
127 user_updated.send(sender=user.__class__, instance=user, updated_by=user)
\r
128 return HttpResponseRedirect(user.get_profile_url())
\r
130 form = EditUserForm(user)
\r
131 return render_to_response('users/edit.html', {
\r
133 'gravatar_faq_url' : reverse('faq') + '#gravatar',
\r
134 }, context_instance=RequestContext(request))
\r
138 def user_view(template, tab_name, tab_description, page_title):
\r
140 def decorated(request, id, slug=None):
\r
141 context = fn(request, get_object_or_404(User, id=id))
\r
143 "tab_name" : tab_name,
\r
144 "tab_description" : tab_description,
\r
145 "page_title" : page_title,
\r
147 return render_to_response(template, context, context_instance=RequestContext(request))
\r
152 @user_view('users/stats.html', 'stats', _('user profile'), _('user profile overview'))
\r
153 def user_stats(request, user):
\r
154 questions = Question.objects.filter(author=user, deleted=False).order_by('-added_at')
\r
155 answers = Answer.objects.filter(author=user, deleted=False).order_by('-added_at')
\r
157 up_votes = user.get_up_vote_count()
\r
158 down_votes = user.get_down_vote_count()
\r
159 votes_today = user.get_vote_count_today()
\r
160 votes_total = int(settings.MAX_VOTES_PER_DAY)
\r
162 user_tags = Tag.objects.filter(Q(nodes__author=user) | Q(nodes__children__author=user)) \
\r
163 .annotate(user_tag_usage_count=Count('name')).order_by('-user_tag_usage_count')
\r
165 awards = Badge.objects.filter(award_badge__user=user).annotate(count=Count('name')).order_by('-count')
\r
167 if request.user.is_superuser:
\r
168 moderate_user_form = ModerateUserForm(instance=user)
\r
170 moderate_user_form = None
\r
172 return {'moderate_user_form': moderate_user_form,
\r
173 "view_user" : user,
\r
174 "questions" : questions,
\r
175 "answers" : answers,
\r
176 "up_votes" : up_votes,
\r
177 "down_votes" : down_votes,
\r
178 "total_votes": up_votes + down_votes,
\r
179 "votes_today_left": votes_total-votes_today,
\r
180 "votes_total_per_day": votes_total,
\r
181 "user_tags" : user_tags[:50],
\r
183 "total_awards" : awards.count(),
\r
186 @user_view('users/recent.html', 'recent', _('recent user activity'), _('profile - recent activity'))
\r
187 def user_recent(request, user):
\r
188 activities = Activity.objects.filter(activity_type__in=(TYPE_ACTIVITY_PRIZE,
\r
189 TYPE_ACTIVITY_ASK_QUESTION, TYPE_ACTIVITY_ANSWER,
\r
190 TYPE_ACTIVITY_COMMENT_QUESTION, TYPE_ACTIVITY_COMMENT_ANSWER,
\r
191 TYPE_ACTIVITY_MARK_ANSWER), user=user).order_by('-active_at')[:USERS_PAGE_SIZE]
\r
193 return {"view_user" : user, "activities" : activities}
\r
196 @user_view('users/votes.html', 'votes', _('user vote record'), _('profile - votes'))
\r
197 def user_votes(request, user):
\r
198 votes = user.votes.exclude(node__deleted=True).order_by('-voted_at')[:USERS_PAGE_SIZE]
\r
200 return {"view_user" : user, "votes" : votes}
\r
203 @user_view('users/reputation.html', 'reputation', _('user reputation in the community'), _('profile - user reputation'))
\r
204 def user_reputation(request, user):
\r
205 reputation = user.reputes.order_by('-reputed_at')
\r
207 graph_data = simplejson.dumps([
\r
208 (time.mktime(rep.reputed_at.timetuple()) * 1000, rep.reputation)
\r
209 for rep in reputation
\r
212 return {"view_user": user, "reputation": reputation, "graph_data": graph_data}
\r
214 @user_view('users/questions.html', 'favorites', _('favorite questions'), _('profile - favorite questions'))
\r
215 def user_favorites(request, user):
\r
216 questions = user.favorite_questions.filter(deleted=False)
\r
218 return {"questions" : questions, "view_user" : user}
\r
220 @user_view('users/subscriptions.html', 'subscriptions', _('subscription settings'), _('profile - subscriptions'))
\r
221 def user_subscriptions(request, user):
\r
222 if request.method == 'POST':
\r
223 form = SubscriptionSettingsForm(request.POST)
\r
225 if 'notswitch' in request.POST:
\r
226 user.subscription_settings.enable_notifications = not user.subscription_settings.enable_notifications
\r
227 user.subscription_settings.save()
\r
229 if user.subscription_settings.enable_notifications:
\r
230 request.user.message_set.create(message=_('Notifications are now enabled'))
\r
232 request.user.message_set.create(message=_('Notifications are now disabled'))
\r
235 for k,v in form.cleaned_data.items():
\r
236 setattr(user.subscription_settings, k, v)
\r
238 user.subscription_settings.save()
\r
239 request.user.message_set.create(message=_('New subscription settings are now saved'))
\r
241 form = SubscriptionSettingsForm(user.subscription_settings.__dict__)
\r
243 notificatons_on = user.subscription_settings.enable_notifications
\r
245 return {'view_user':user, 'notificatons_on': notificatons_on, 'form':form}
\r
248 def account_settings(request):
\r
250 msg = request.GET.get('msg', '')
\r
253 return render_to_response('account_settings.html', {
\r
255 'is_openid': is_openid
\r
256 }, context_instance=RequestContext(request))
\r