def put(self, request, pk, format=None): try: comment = Comment.objects.get(pk=pk) if request.data['type'] == 'update': if comment.user == request.user: comment_serializer = CommentsSerializer(comment, data=request.data) if comment_serializer.is_valid(): comment_serializer.save() return Response(comment_serializer.data) else: return Response(status.HTTP_400_BAD_REQUEST) else: return Response(status.HTTP_405_METHOD_NOT_ALLOWED) elif request.data['type'] == 'like': already_liked_user = comment.get_users if request.user not in already_liked_user: like = Like() like.comment = comment like.user = request.user like.save() return Response(status.HTTP_201_CREATED) else: return Response(status.HTTP_501_NOT_IMPLEMENTED) elif request.data['type'] == 'unlike': already_liked_user = comment.get_users if request.user in already_liked_user: like = Like.objects.get(user=request.user, comment=comment) like.delete() return Response(status.HTTP_201_CREATED) else: return Response(status.HTTP_501_NOT_IMPLEMENTED) except Comment.DoesNotExist: return Response(status.HTTP_404_NOT_FOUND)
def post(self, request, *args, **kwargs): usr = self.request.user if 'post' in request.data: post_key = request.data['post'] like_obj = Like.objects.filter(post__key=post_key, user=usr) if like_obj is not None and len(like_obj)>0: return Response({"error":"user has already liked the post!"},status = HTTP_400_BAD_REQUEST) else: like_obj = Like() like_obj.user= usr like_obj.post = Post.objects.get(key=post_key) like_obj.save() serializer = LikeSerializer(like_obj) return Response(serializer.data,status=HTTP_201_CREATED) else: return Response({"error":"post key is required!"},status = HTTP_400_BAD_REQUEST)
def likes(request): id1 = request.POST['client_id'] print request.POST loggeduserid=request.user.id myUser = get_object_or_404(User, id=loggeduserid) y = get_object_or_404(MyUser, id=int(id1)) try: likee = Like.objects.get(user = myUser, profile = y) except ObjectDoesNotExist: y.like_number += 1 likee = Like() likee.user = myUser likee.profile = y likee.save() #print like x = y.like_number y.save() response_dict = {} response_dict.update({'server_response': x}) return HttpResponse(json.dumps(response_dict), mimetype='application/javascript')
return LikePostBadRequest( "Attempting go get content-type %r and object PK %r exists raised %s" % \ (escape(ctype), escape(object_pk), e.__class__.__name__)) request_user = request.user # Otherwise create the like like = Like.objects.filter(user=request_user, content_type=ctype, object_pk=object_pk) if like.count() > 0: like = like[0] else: like = Like() like.ip_address = request.META.get("REMOTE_ADDR", None) like.user = request_user like.content_type = ctype like.object_pk = object_pk like.likes = likes like.submit_date = datetime.datetime.now() like.site_id = settings.SITE_ID # Save the like and signal that it was saved like.save() if notification: #notify all concerned users by the object notification.send([request_user], "new_like", { 'like_user': request_user, 'like_object': target, })
return LikePostBadRequest( "Attempting go get content-type %r and object PK %r exists raised %s" % \ (escape(ctype), escape(object_pk), e.__class__.__name__)) request_user = request.user # Otherwise create the like like = Like.objects.filter(user=request_user, content_type=ctype, object_pk=object_pk) if like.count() > 0: like = like[0] else: like = Like() like.ip_address = request.META.get("REMOTE_ADDR", None) like.user = request_user like.content_type = ctype like.object_pk = object_pk like.likes = likes like.submit_date = datetime.datetime.now() like.site_id = settings.SITE_ID # Save the like and signal that it was saved like.save() if notification: #notify all concerned users by the object notification.send([request_user], "new_like", {'like_user': request_user, 'like_object':target,}) # signals.like_was_posted.send( # sender=like.__class__, # like=like,