Example #1
0
 def procces_voting_action(cls, action, new):
     if action.node.node_type == 'answer':
         answer = action.node
         question = answer.parent
         if not question.author.is_superuser:
             return
         user = action.node.author
         for name in question.tagname_list():
             try:
                 badge = CustomBadge.objects.get(tag__name=name)
             except CustomBadge.DoesNotExist:
                 continue
             if badge.is_peer_given:
                 continue
             if badge.min_required_votes < 1 or answer.score < badge.min_required_votes:
                 continue
             from forum.badges.base import award_badge
             award_badge(badge.ondb, user, action, False)
Example #2
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))