Esempio n. 1
0
def like_view(request, slug):
    if request.method == "POST":
        post = Post.objects.get(slug=slug)
        account = Account.objects.get(user=request.user)
        qs = Like.objects.filter(post=post).filter(account=account)
        if not qs.exists():
            like = Like()
            like.account = account
            like.post = post
            like.save()
            post.likes += 1
            post.save()
            if like.account != post.account:
                notf = Notification()
                notf.account = post.account
                notf.post = post
                content = like.account.user.first_name + ' ' + like.account.user.last_name + ' liked  your post.'
                notf.content = content
                notf.save()
        else:
            like = Like.objects.get(post=post, account=account)
            like.delete()
            post.likes -= 1
            post.save()
        return HttpResponseRedirect(post.get_absolute_url)
    return HttpResponseRedirect('/posts/')
Esempio n. 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())