예제 #1
0
파일: views.py 프로젝트: maynalysa/zds-site
def dislike_post(request):
    """Dislike a post."""

    try:
        post_pk = request.GET["message"]
    except:
        # problem in variable format
        raise Http404
    resp = {}
    post = get_object_or_404(Post, pk=post_pk)
    user = request.user
    if not post.topic.forum.can_read(request.user):
        raise PermissionDenied
    if post.author.pk != request.user.pk:

        # Making sure the user is allowed to do that

        if CommentDislike.objects.filter(user__pk=user.pk,
                                         comments__pk=post_pk).count() == 0:
            dislike = CommentDislike()
            dislike.user = user
            dislike.comments = post
            post.dislike = post.dislike + 1
            post.save()
            dislike.save()
            if CommentLike.objects.filter(user__pk=user.pk,
                                          comments__pk=post_pk).count() > 0:
                CommentLike.objects.filter(user__pk=user.pk,
                                           comments__pk=post_pk).all().delete()
                post.like = post.like - 1
                post.save()
        else:
            CommentDislike.objects.filter(user__pk=user.pk,
                                          comments__pk=post_pk).all().delete()
            post.dislike = post.dislike - 1
            post.save()
    resp["upvotes"] = post.like
    resp["downvotes"] = post.dislike
    if request.is_ajax():
        return HttpResponse(json.dumps(resp))
    else:
        return redirect(post.get_absolute_url())
예제 #2
0
def dislike_reaction(request):
    """Dislike a reaction."""

    try:
        reaction_pk = request.GET['message']
    except KeyError:
        raise Http404
    resp = {}
    reaction = get_object_or_404(Reaction, pk=reaction_pk)
    user = request.user

    if reaction.author.pk != request.user.pk:
        # Making sure the user is allowed to do that
        if CommentDislike.objects.filter(user__pk=user.pk,
                                         comments__pk=reaction_pk) \
                .count() == 0:
            dislike = CommentDislike()
            dislike.user = user
            dislike.comments = reaction
            reaction.dislike = reaction.dislike + 1
            reaction.save()
            dislike.save()
            if CommentLike.objects.filter(user__pk=user.pk,
                                          comments__pk=reaction_pk) \
                    .count() > 0:
                CommentLike.objects.filter(
                    user__pk=user.pk, comments__pk=reaction_pk).all().delete()
                reaction.like = reaction.like - 1
                reaction.save()
        else:
            CommentDislike.objects.filter(
                user__pk=user.pk, comments__pk=reaction_pk).all().delete()
            reaction.dislike = reaction.dislike - 1
            reaction.save()

    resp['upvotes'] = reaction.like
    resp['downvotes'] = reaction.dislike

    if request.is_ajax():
        return HttpResponse(json_writer.dumps(resp))
    else:
        return redirect(reaction.get_absolute_url())