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
23 USERS_PAGE_SIZE = 35# refactor - move to some constants file
\r
27 sortby = request.GET.get('sort', 'reputation')
\r
28 suser = request.REQUEST.get('q', "")
\r
30 page = int(request.GET.get('page', '1'))
\r
35 if sortby == "newest":
\r
36 objects_list = Paginator(User.objects.all().order_by('-date_joined'), USERS_PAGE_SIZE)
\r
37 elif sortby == "last":
\r
38 objects_list = Paginator(User.objects.all().order_by('date_joined'), USERS_PAGE_SIZE)
\r
39 elif sortby == "user":
\r
40 objects_list = Paginator(User.objects.all().order_by('username'), USERS_PAGE_SIZE)
\r
43 objects_list = Paginator(User.objects.all().order_by('-reputation'), USERS_PAGE_SIZE)
\r
44 base_url = reverse('users') + '?sort=%s&' % sortby
\r
46 sortby = "reputation"
\r
47 objects_list = Paginator(User.objects.filter(username__icontains=suser).order_by('-reputation'), USERS_PAGE_SIZE)
\r
48 base_url = reverse('users') + '?name=%s&sort=%s&' % (suser, sortby)
\r
51 users = objects_list.page(page)
\r
52 except (EmptyPage, InvalidPage):
\r
53 users = objects_list.page(objects_list.num_pages)
\r
55 return render_to_response('users/users.html', {
\r
61 'is_paginated' : is_paginated,
\r
62 'pages': objects_list.num_pages,
\r
64 'has_previous': users.has_previous(),
\r
65 'has_next': users.has_next(),
\r
66 'previous': users.previous_page_number(),
\r
67 'next': users.next_page_number(),
\r
68 'base_url' : base_url
\r
71 }, context_instance=RequestContext(request))
\r
74 def moderate_user(request, id):
\r
75 """ajax handler of user moderation
\r
77 if not request.user.is_superuser or request.method != 'POST':
\r
79 if not request.is_ajax():
\r
80 return HttpResponseForbidden(mimetype="application/json")
\r
82 user = get_object_or_404(User, id=id)
\r
83 form = ModerateUserForm(request.POST, instance=user)
\r
87 logging.debug('data saved')
\r
88 response = HttpResponse(simplejson.dumps(''), mimetype="application/json")
\r
90 response = HttpResponseForbidden(mimetype="application/json")
\r
93 def set_new_email(user, new_email, nomessage=False):
\r
94 if new_email != user.email:
\r
95 user.email = new_email
\r
96 user.email_isvalid = False
\r
98 #if settings.EMAIL_VALIDATION == 'on':
\r
99 # send_new_email_key(user,nomessage=nomessage)
\r
102 def edit_user(request, id):
\r
103 user = get_object_or_404(User, id=id)
\r
104 if request.user != user:
\r
106 if request.method == "POST":
\r
107 form = EditUserForm(user, request.POST)
\r
108 if form.is_valid():
\r
109 new_email = sanitize_html(form.cleaned_data['email'])
\r
111 set_new_email(user, new_email)
\r
113 #user.username = sanitize_html(form.cleaned_data['username'])
\r
114 user.real_name = sanitize_html(form.cleaned_data['realname'])
\r
115 user.website = sanitize_html(form.cleaned_data['website'])
\r
116 user.location = sanitize_html(form.cleaned_data['city'])
\r
117 user.date_of_birth = sanitize_html(form.cleaned_data['birthday'])
\r
118 if len(user.date_of_birth) == 0:
\r
119 user.date_of_birth = '1900-01-01'
\r
120 user.about = sanitize_html(form.cleaned_data['about'])
\r
123 # send user updated signal if full fields have been updated
\r
124 if user.email and user.real_name and user.website and user.location and \
\r
125 user.date_of_birth and user.about:
\r
126 user_updated.send(sender=user.__class__, instance=user, updated_by=user)
\r
127 return HttpResponseRedirect(user.get_profile_url())
\r
129 form = EditUserForm(user)
\r
130 return render_to_response('users/edit.html', {
\r
132 'gravatar_faq_url' : reverse('faq') + '#gravatar',
\r
133 }, context_instance=RequestContext(request))
\r
137 def user_view(template, tab_name, tab_description, page_title):
\r
139 def decorated(request, id, slug=None):
\r
140 context = fn(request, get_object_or_404(User, id=id))
\r
142 "tab_name" : tab_name,
\r
143 "tab_description" : tab_description,
\r
144 "page_title" : page_title,
\r
146 return render_to_response(template, context, context_instance=RequestContext(request))
\r
151 @user_view('users/stats.html', 'stats', _('user profile'), _('user profile overview'))
\r
152 def user_stats(request, user):
\r
153 questions = Question.objects.filter(author=user, deleted=False).order_by('-added_at')
\r
154 answers = Answer.objects.filter(author=user, deleted=False).order_by('-added_at')
\r
156 up_votes = user.get_up_vote_count()
\r
157 down_votes = user.get_down_vote_count()
\r
158 votes_today = user.get_vote_count_today()
\r
159 votes_total = int(settings.MAX_VOTES_PER_DAY)
\r
161 user_tags = Tag.objects.filter(Q(nodes__author=user) | Q(nodes__children__author=user)) \
\r
162 .annotate(user_tag_usage_count=Count('name')).order_by('-user_tag_usage_count')
\r
164 awards = Badge.objects.filter(award_badge__user=user).annotate(count=Count('name')).order_by('-count')
\r
166 if request.user.is_superuser:
\r
167 moderate_user_form = ModerateUserForm(instance=user)
\r
169 moderate_user_form = None
\r
171 return {'moderate_user_form': moderate_user_form,
\r
172 "view_user" : user,
\r
173 "questions" : questions,
\r
174 "answers" : answers,
\r
175 "up_votes" : up_votes,
\r
176 "down_votes" : down_votes,
\r
177 "total_votes": up_votes + down_votes,
\r
178 "votes_today_left": votes_total-votes_today,
\r
179 "votes_total_per_day": votes_total,
\r
180 "user_tags" : user_tags[:50],
\r
182 "total_awards" : awards.count(),
\r
185 @user_view('users/recent.html', 'recent', _('recent user activity'), _('profile - recent activity'))
\r
186 def user_recent(request, user):
\r
187 activities = Activity.objects.filter(activity_type__in=(TYPE_ACTIVITY_PRIZE,
\r
188 TYPE_ACTIVITY_ASK_QUESTION, TYPE_ACTIVITY_ANSWER,
\r
189 TYPE_ACTIVITY_COMMENT_QUESTION, TYPE_ACTIVITY_COMMENT_ANSWER,
\r
190 TYPE_ACTIVITY_MARK_ANSWER), user=user).order_by('-active_at')[:USERS_PAGE_SIZE]
\r
192 return {"view_user" : user, "activities" : activities}
\r
195 @user_view('users/votes.html', 'votes', _('user vote record'), _('profile - votes'))
\r
196 def user_votes(request, user):
\r
197 votes = user.votes.exclude(node__deleted=True).order_by('-voted_at')[:USERS_PAGE_SIZE]
\r
199 return {"view_user" : user, "votes" : votes}
\r
202 @user_view('users/reputation.html', 'reputation', _('user reputation in the community'), _('profile - user reputation'))
\r
203 def user_reputation(request, user):
\r
204 reputation = user.reputes.order_by('-reputed_at')
\r
206 graph_data = simplejson.dumps([
\r
207 (time.mktime(rep.reputed_at.timetuple()) * 1000, rep.reputation)
\r
208 for rep in reputation
\r
211 return {"view_user": user, "reputation": reputation, "graph_data": graph_data}
\r
213 @user_view('users/questions.html', 'favorites', _('favorite questions'), _('profile - favorite questions'))
\r
214 def user_favorites(request, user):
\r
215 questions = user.favorite_questions.filter(deleted=False)
\r
217 return {"questions" : questions, "view_user" : user}
\r
219 @user_view('users/subscriptions.html', 'subscriptions', _('subscription settings'), _('profile - subscriptions'))
\r
220 def user_subscriptions(request, user):
\r
221 if request.method == 'POST':
\r
222 form = SubscriptionSettingsForm(request.POST)
\r
224 if 'notswitch' in request.POST:
\r
225 user.subscription_settings.enable_notifications = not user.subscription_settings.enable_notifications
\r
226 user.subscription_settings.save()
\r
228 if user.subscription_settings.enable_notifications:
\r
229 request.user.message_set.create(message=_('Notifications are now enabled'))
\r
231 request.user.message_set.create(message=_('Notifications are now disabled'))
\r
234 for k,v in form.cleaned_data.items():
\r
235 setattr(user.subscription_settings, k, v)
\r
237 user.subscription_settings.save()
\r
238 request.user.message_set.create(message=_('New subscription settings are now saved'))
\r
240 form = SubscriptionSettingsForm(user.subscription_settings.__dict__)
\r
242 notificatons_on = user.subscription_settings.enable_notifications
\r
244 return {'view_user':user, 'notificatons_on': notificatons_on, 'form':form}
\r
247 def account_settings(request):
\r
249 msg = request.GET.get('msg', '')
\r
252 return render_to_response('account_settings.html', {
\r
254 'is_openid': is_openid
\r
255 }, context_instance=RequestContext(request))
\r