def moderate_user(request, id): """ajax handler of user moderation """ if not auth.can_moderate_users(request.user) or request.method != 'POST': raise Http404 if not request.is_ajax(): return HttpResponseForbidden(mimetype="application/json") user = get_object_or_404(User, id=id) form = ModerateUserForm(request.POST, instance=user) if form.is_valid(): form.save() logging.debug('data saved') response = HttpResponse(simplejson.dumps(''), mimetype="application/json") else: response = HttpResponseForbidden(mimetype="application/json") return response
def can_moderate_users(user): return auth.can_moderate_users(user)
def user_stats(request, user_id, user_view): user = get_object_or_404(User, id=user_id) questions = Question.objects.extra( select={ 'score' : 'question.score', 'favorited_myself' : 'SELECT count(*) FROM favorite_question f WHERE f.user_id = %s AND f.question_id = question.id', 'la_user_id' : 'auth_user.id', 'la_username' : 'auth_user.username', 'la_user_gold' : 'auth_user.gold', 'la_user_silver' : 'auth_user.silver', 'la_user_bronze' : 'auth_user.bronze', 'la_user_reputation' : 'auth_user.reputation' }, select_params=[user_id], tables=['question', 'auth_user'], where=['question.deleted=False AND question.author_id=%s AND question.last_activity_by_id = auth_user.id'], params=[user_id], order_by=['-score', '-last_activity_at'] ).values('score', 'favorited_myself', 'id', 'title', 'author_id', 'added_at', 'answer_accepted', 'answer_count', 'comment_count', 'view_count', 'favourite_count', 'summary', 'tagnames', 'vote_up_count', 'vote_down_count', 'last_activity_at', 'la_user_id', 'la_username', 'la_user_gold', 'la_user_silver', 'la_user_bronze', 'la_user_reputation')[:100] #this is meant for the questions answered by the user (or where answers were edited by him/her?) answered_questions = Question.objects.extra( select={ 'vote_up_count' : 'answer.vote_up_count', 'vote_down_count' : 'answer.vote_down_count', 'answer_id' : 'answer.id', 'answer_accepted' : 'answer.accepted', 'answer_score' : 'answer.score', 'comment_count' : 'answer.comment_count' }, tables=['question', 'answer'], where=['answer.deleted=False AND question.deleted=False AND answer.author_id=%s AND answer.question_id=question.id'], params=[user_id], order_by=['-answer_score', '-answer_id'], select_params=[user_id] ).distinct().values('comment_count', 'id', 'answer_id', 'title', 'author_id', 'answer_accepted', 'answer_score', 'answer_count', 'vote_up_count', 'vote_down_count')[:100] up_votes = Vote.objects.get_up_vote_count_from_user(user) down_votes = Vote.objects.get_down_vote_count_from_user(user) votes_today = Vote.objects.get_votes_count_today_from_user(user) votes_total = auth.VOTE_RULES['scope_votes_per_user_per_day'] question_id_set = set(map(lambda v: v['id'], list(questions))) \ | set(map(lambda v: v['id'], list(answered_questions))) user_tags = Tag.objects.filter(questions__id__in = question_id_set) try: from django.db.models import Count #todo - rewrite template to do the table joins within standard ORM #awards = Award.objects.filter(user=user).order_by('-awarded_at') awards = Award.objects.extra( select={'id': 'badge.id', 'name':'badge.name', 'description': 'badge.description', 'type': 'badge.type'}, tables=['award', 'badge'], order_by=['-awarded_at'], where=['user_id=%s AND badge_id=badge.id'], params=[user.id] ).values('id', 'name', 'description', 'type') total_awards = awards.count() awards = awards.annotate(count = Count('badge__id')) user_tags = user_tags.annotate(user_tag_usage_count=Count('name')) except ImportError: #todo: remove all old django stuff, e.g. with '.group_by = ' pattern awards = Award.objects.extra( select={'id': 'badge.id', 'count': 'count(badge_id)', 'name':'badge.name', 'description': 'badge.description', 'type': 'badge.type'}, tables=['award', 'badge'], order_by=['-awarded_at'], where=['user_id=%s AND badge_id=badge.id'], params=[user.id] ).values('id', 'count', 'name', 'description', 'type') total_awards = awards.count() awards.query.group_by = ['badge_id'] user_tags = user_tags.extra( select={'user_tag_usage_count': 'COUNT(1)',}, order_by=['-user_tag_usage_count'], ) user_tags.query.group_by = ['name'] if auth.can_moderate_users(request.user): moderate_user_form = ModerateUserForm(instance=user) else: moderate_user_form = None return render_to_response(user_view.template_file,{ 'moderate_user_form': moderate_user_form, "tab_name" : user_view.id, "tab_description" : user_view.tab_description, "page_title" : user_view.page_title, "view_user" : user, "questions" : questions, "answered_questions" : answered_questions, "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[:50], "awards": awards, "total_awards" : total_awards, }, context_instance=RequestContext(request))