def likes_update(request, pk=None):

    topic = get_object_or_404(Topic, id=pk)

    like = None
    if topic.topic_likes.filter(topic=topic, author=request.user).count() == 1:
        like = topic.topic_likes.get(topic=topic, author=request.user)

    if like == None:
        like = Like()
        like.author = request.user
        like.topic = topic
        like.save()
        return HttpResponse("Дизлайк")

    else:
        if like.is_archive:
            like.is_archive = False
            like.save()
            return HttpResponse("Лайк")

        else:
            like.is_archive = True
            like.save()
            return HttpResponse("Дизлайк")
Beispiel #2
0
 def post(self, *args, **kwargs):
     like = self.post_object.likes.filter(author=self.request.user).first()
     if like is None:
         like = Like()
         like.author = self.request.user
         like.post = self.post_object
         like.save()
     else:
         like.delete()
     return HttpResponse(Like.objects.filter(post=self.post_object).count())