コード例 #1
0
ファイル: __init__.py プロジェクト: zctyhj/kitsune
def answervote(**kwargs):
    defaults = dict(created=datetime.now(), helpful=False)
    defaults.update(kwargs)
    if 'creator' not in kwargs and 'creator_id' not in kwargs:
        defaults['creator'] = profile().user
    if 'answer' not in kwargs and 'answer_id' not in kwargs:
        defaults['answer'] = answer(save=True)
    return AnswerVote(**defaults)
コード例 #2
0
ファイル: api.py プロジェクト: yoyo2011/kitsune
    def helpful(self, request, pk=None):
        answer = self.get_object()

        if not answer.question.editable:
            raise GenericAPIException(403, 'Answer not editable')
        if answer.has_voted(request):
            raise GenericAPIException(409, 'Cannot vote twice')

        AnswerVote(answer=answer, creator=request.user, helpful=True).save()
        num_helpful_votes = AnswerVote.objects.filter(answer=answer, helpful=True).count()
        num_unhelpful_votes = AnswerVote.objects.filter(answer=answer, helpful=False).count()
        return Response({
            'num_helpful_votes': num_helpful_votes,
            'num_unhelpful_votes': num_unhelpful_votes,
        })
コード例 #3
0
ファイル: views.py プロジェクト: willoughbyrm/kitsune
def answer_vote(request, question_id, answer_id):
    """Vote for Helpful/Not Helpful answers"""
    answer = get_object_or_404(
        Answer,
        pk=answer_id,
        question=question_id,
        is_spam=False,
        question__is_spam=False,
    )

    if not answer.question.editable:
        raise PermissionDenied

    if request.limited:
        if request.is_ajax():
            return HttpResponse(json.dumps({"ignored": True}))
        else:
            return HttpResponseRedirect(answer.get_absolute_url())

    if not answer.has_voted(request):
        vote = AnswerVote(answer=answer)

        if "helpful" in request.POST:
            vote.helpful = True
            message = _("Glad to hear it!")
        else:
            message = _("Sorry to hear that.")

        if request.user.is_authenticated:
            vote.creator = request.user
        else:
            vote.anonymous_id = request.anonymous.anonymous_id

        vote.save()

        if "referrer" in request.GET:
            referrer = request.GET.get("referrer")
            vote.add_metadata("referrer", referrer)

            if referrer == "search" and "query" in request.GET:
                vote.add_metadata("query", request.GET.get("query"))

        ua = request.META.get("HTTP_USER_AGENT")
        if ua:
            vote.add_metadata("ua", ua)
    else:
        message = _("You already voted on this reply.")

    if request.is_ajax():
        return HttpResponse(json.dumps({"message": message}))

    return HttpResponseRedirect(answer.get_absolute_url())