def like(self, status): """ user: UserProfile - User liking the status Returns True if the action was successful """ # Check if the user already liked it if Like.get_or_none(user=self.user, status=status): return False # First we create the like object Like.create( user=self.user, status=status, ) # Update the counter in the object Status.update({ Status.favourites_count: Status.favourites_count + 1 }).where(Status.id == status.id) # Create a redis notification like_status(status, self.user) return True
def create_like(): try: # gets data from the client - contains a post id data = request.get_json() # tries to get the post by its id post_id = Post.get(Post.id == data['postId']) try: # checks if a like for that post and user already exists like = Like.get(Like.post == post_id, Like.user == current_user.id) # if the user has not already likes the post except DoesNotExist: # creates a new like for the post like = Like.create(post=post_id, user=current_user.id) # convert like to dictionary and remove both users password like_dict = model_to_dict(like) del like_dict['user']['password'] del like_dict['post']['user']['password'] return jsonify(data=like_dict, status={ 'code': 201, 'message': 'User successfully liked post.' }) # exception thrown if the post doesnt exist except DoesNotExist: return jsonify(data={}, status={ 'code': 404, 'message': 'Failure to get resource.' })