def upvote()

in moderator/moderate/views.py [0:0]


def upvote(request, q_id):
    """Upvote question"""

    question = Question.objects.get(pk=q_id)
    event = question.event
    user = request.user
    if not (
        user_can_vote := (
            event.users_can_vote or (event.is_nda and user.userprofile.is_nda_member)
        )
    ):
        msg = "Voting is not allowed for this event."
        messages.warning(request, msg)

    if (
        request.headers.get("x-requested-with") == "XMLHttpRequest"
        and not question.event.archived
        and user_can_vote
    ):
        if not Vote.objects.filter(user=user, question=question).exists():
            Vote.objects.create(user=user, question=question)
        else:
            Vote.objects.filter(user=user, question=question).delete()

        response_dict = {}
        if request.user.is_superuser or event.created_by == request.user:
            response_dict = {"current_vote_count": question.votes.count()}

        return JsonResponse(response_dict)

    return redirect(reverse("event", kwargs={"e_slug": event.slug}))