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 delete_post(cls, post_id, user_id): error_msg = None posts = Post.all().order('-created').fetch(limit=10) updated_post_list = posts post = Post.get_by_id(post_id) if user_id == post.created_by: # delete comments of the post comments = Comment.all().filter('post_id =', post_id).fetch(limit=10) for c in comments: Comment.delete(c) # delete likes of the post likes = Like.all().filter('post_id =', post_id).fetch(limit=10) for l in likes: Like.delete(l) # delete the post updated_post_list = cls.update_list_on_delete( object_list=posts, object_id=post_id ) Post.delete(post) else: error_msg = 'You can delete only your own posts' return updated_post_list, error_msg
def post(self, post_id): """ handles post request """ action = self.request.get("like-button") user = self.logged_in_user() post = Post.post_by_id(post_id) if action == 'like': like = Like(post=post, user=user) like.put() else: like = user.likes.filter("post =", post).get() like.delete() self.redirect('/' + post_id)
def update_ratings(): body = request.get_json() if 'liked_by' not in body or 'disliked_by' not in body or 'username' not in body or 'course_id' not in body: abort(422) liked_by = body['liked_by'] disliked_by = body['disliked_by'] username = body['username'] course_id = body['course_id'] rating = Rating.query.filter(Rating.user_id == username, Rating.course_id == course_id).first() user = User.query.get(liked_by) course = Course.query.get(course_id) if rating is None or course is None: abort(404) if len(disliked_by) == 0: user = User.query.get(liked_by) if user is None: abort(404) like = Like.query.get((rating.rating_id, liked_by)) if like is not None: abort(422) dislike = DisLike.query.get((rating.rating_id, liked_by)) if dislike is not None: dislike.delete() like = Like(rating_id=rating.rating_id, liked_by=liked_by) like.insert() else: user = User.query.get(disliked_by) if user is None: abort(404) dislike = DisLike.query.get((rating.rating_id, disliked_by)) if dislike is not None: abort(422) like = Like.query.get((rating.rating_id, disliked_by)) if like is not None: like.delete() dislike = DisLike(rating_id=rating.rating_id, disliked_by=disliked_by) dislike.insert() return jsonify({'ratings': [rating.format() for rating in course.ratings]})