def like_message(request): if request.method == 'POST': #print('Sunt aici') message_id = request.POST.get('id') print(message_id) like_value = bool(int(request.POST.get('like'))) print(like_value) message = get_object_or_404(Message, id=message_id) # daca apas pe like de 2 ori at se sterge like-ul, idem pt dislike try: like = Like.objects.get(user=request.user, message=message) if like.like == like_value: like.delete() else: like.like = like_value like.save() except Like.DoesNotExist: like = Like(user=request.user, message=message, like=like_value) like.save() return JsonResponse({'succes': 'true'})
def post(self, request, *args, **kwargs): message = Message.objects.get(id=request.POST['message_id']) try: like_dislike = Like.objects.get(message=message, user=self.request.user) if int(like_dislike.like) == int(request.POST['value']): like_dislike.delete() else: like_dislike.like = request.POST['value'] like_dislike.save() except ObjectDoesNotExist: like_dislike = Like(user=self.request.user, message=message, like=request.POST['value']) like_dislike.save() return HttpResponse('')
def like_message(request): if request.method == "POST": message_id = request.POST.get('id') print(message_id) like_value = bool(int(request.POST.get('like'))) print(like_value) message = get_object_or_404(Message, id=message_id) try: like = Like.objects.get(user=request.user, message=message) if like.like == like_value: like.delete() else: like.like = like_value like.save() except Like.DoesNotExist: like = Like(user=request.user, message=message, like=like_value) like.save() return JsonResponse({'success': 'true'})