Example #1
0
def user_profile(request, user, **kwargs):
    questions = Question.objects.filter_state(deleted=False).filter(author=user).order_by('-added_at')
    answers = Answer.objects.filter_state(deleted=False).filter(author=user).order_by('-added_at')

    # Check whether the passed slug matches the one for the user object
    slug = kwargs['slug']
    if slug != slugify(smart_unicode(user.username)):
        return HttpResponseRedirect(user.get_absolute_url())

    up_votes = user.vote_up_count
    down_votes = user.vote_down_count
    votes_today = user.get_vote_count_today()
    votes_total = user.can_vote_count_today()

    user_tags = Tag.objects.filter(Q(nodes__author=user) | Q(nodes__children__author=user)) \
        .annotate(user_tag_usage_count=Count('name')).order_by('-user_tag_usage_count')

    awards = [(Badge.objects.get(id=b['id']), b['count']) for b in
              Badge.objects.filter(awards__user=user).values('id').annotate(count=Count('cls')).order_by('-count')]

    return pagination.paginated(request, (
    ('questions', QuestionListPaginatorContext('USER_QUESTION_LIST', _('questions'), default_pagesize=15)),
    ('answers', UserAnswersPaginatorContext())), {
    "view_user" : user,
    "questions" : questions,
    "answers" : answers,
    "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" : len(awards),
    })
Example #2
0
def user_profile(request, user):
    questions = Question.objects.filter_state(deleted=False).filter(author=user).order_by('-added_at')
    answers = Answer.objects.filter_state(deleted=False).filter(author=user).order_by('-added_at')

    up_votes = user.vote_up_count
    down_votes = user.vote_down_count
    votes_today = user.get_vote_count_today()
    votes_total = int(settings.MAX_VOTES_PER_DAY)

    user_tags = Tag.objects.filter(Q(nodes__author=user) | Q(nodes__children__author=user)) \
        .annotate(user_tag_usage_count=Count('name')).order_by('-user_tag_usage_count')

    awards = [(Badge.objects.get(id=b['id']), b['count']) for b in
              Badge.objects.filter(awards__user=user).values('id').annotate(count=Count('cls')).order_by('-count')]

    return pagination.paginated(request, (
    ('questions', QuestionListPaginatorContext('USER_QUESTION_LIST', _('questions'), 15)),
    ('answers', UserAnswersPaginatorContext())), {
    "view_user" : user,
    "questions" : questions,
    "answers" : answers,
    "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" : len(awards),
    })
Example #3
0
def user_preferences(request, user, **kwargs):
    questions = Question.objects.filter_state(deleted=False).filter(
        author=user).order_by('-added_at')
    answers = Answer.objects.filter_state(deleted=False).filter(
        author=user).order_by('-added_at')

    # Check whether the passed slug matches the one for the user object
    slug = kwargs['slug']
    if slug != slugify(smart_unicode(user.username)):
        return HttpResponseRedirect(user.get_absolute_url())

    up_votes = user.vote_up_count
    down_votes = user.vote_down_count
    votes_today = user.get_vote_count_today()
    votes_total = user.can_vote_count_today()

    user_tags = Tag.objects.filter(Q(nodes__author=user) | Q(nodes__children__author=user)) \
        .annotate(user_tag_usage_count=Count('name')).order_by('-user_tag_usage_count')

    awards = [
        (Badge.objects.get(id=b['id']), b['count'])
        for b in Badge.objects.filter(awards__user=user).values('id').annotate(
            count=Count('cls')).order_by('-count')
    ]

    # for the reputation history
    rep = list(user.reputes.order_by('date'))
    values = [r.value for r in rep]
    redux = lambda x, y: x + y

    graph_data = json.dumps([(time.mktime(rep[i].date.timetuple()) * 1000,
                              reduce(redux, values[:i + 1], 0))
                             for i in range(len(values))])

    rep = user.reputes.filter(action__canceled=False).order_by('-date')[0:20]

    return pagination.paginated(
        request,
        (('questions',
          QuestionListPaginatorContext(
              'USER_QUESTION_LIST', _('questions'), default_pagesize=15)),
         ('answers', UserAnswersPaginatorContext())), {
             "view_user": user,
             "questions": questions,
             "answers": answers,
             "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": len(awards),
             "reputation": rep,
             "graph_data": graph_data,
         })
Example #4
0
def badge(request, id, slug):
    badge = Badge.objects.get(id=id)
    award_queryset = Award.objects.filter(badge=badge)
    awards = list(award_queryset.order_by('user', 'awarded_at'))
    award_count = len(awards)

    awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
                    lambda c1, c2: c2['count'] - c1['count'])

    kwargs = {
        'award_count': award_count,
        'awards' : awards,
        'badge' : badge,
        'requires_submitted_work': False,
        'peer_given': False,
    }

    try:
        custom_badge = badge.custombadge_set.get()
        kwargs['long_description'] = custom_badge.long_description
        if custom_badge.is_peer_given:
            kwargs['is_peer_award_restricted'] = custom_badge.is_peer_award_restricted(request.user)
            if request.POST:
                kwargs['award_form'] = AwardBadgeForm(request.POST, user=request.user, custom_badge=custom_badge)
            else:
                kwargs['award_form'] = AwardBadgeForm(user=request.user, custom_badge=custom_badge)
            if request.method == "POST" and kwargs['award_form'].is_valid():
                award_comment = AwardComment(author=request.user, body=kwargs['award_form'].cleaned_data['text'])
                award_comment.save()
                class DummyAction:
                    node = award_comment
                award_badge(badge, kwargs['award_form'].cleaned_data['user'], DummyAction(), False)
                return HttpResponseRedirect(badge.get_absolute_url() + "#%s" % award_comment.id)
            kwargs['peer_given'] = True
            kwargs['award_comments'] = award_queryset
            if request.method == 'GET' and 'user_filter' in request.GET and request.GET['user_filter']:
                filter_form = BadgeFilterForm(request.GET)
                if filter_form.is_valid():
                    kwargs['user_filter'] = filter_form.cleaned_data['user_filter']
                    kwargs['award_comments'] = award_queryset.filter(user=kwargs['user_filter'])
            else:
                filter_form = BadgeFilterForm()
            kwargs['filter_form'] = filter_form
            kwargs = pagination.paginated(request,
                ('award_comments', BadgesAwardCommentsPaginatorContext()), kwargs)
        elif custom_badge.min_required_votes > 0:
            kwargs['requires_submitted_work'] = True
            kwargs['questions'] = Question.objects.filter_state(deleted=False).filter_tag(
                custom_badge.tag_name).order_by('-added_at')
            kwargs['answers'] = Answer.objects.filter_state(deleted=False).filter(
                parent__id__in=[q.id for q in kwargs['questions']]).order_by('-score')
            if request.method == 'GET' and 'user_filter' in request.GET and request.GET['user_filter']:
                filter_form = BadgeFilterForm(request.GET)
                if filter_form.is_valid():
                    kwargs['user_filter'] = filter_form.cleaned_data['user_filter']
                    kwargs['answers'] = kwargs['answers'].filter(author=kwargs['user_filter'])
            else:
                filter_form = BadgeFilterForm()
            kwargs['filter_form'] = filter_form
            kwargs = pagination.paginated(request, (
                ('questions', QuestionListPaginatorContext('USER_QUESTION_LIST', _('questions'), 3)),
                ('answers', BadgesAnswersPaginatorContext())), kwargs)
    except CustomBadge.DoesNotExist:
        pass

    return render_to_response('badge.html', kwargs,
        context_instance=RequestContext(request))