def follow(self, following): from models.follow import Follow if Follow.get_or_none(Follow.follower == self.id, Follow.following == following.id): return False else: if following.is_private: Follow.create(follower=self.id, following=following.id, is_approved=False) else: Follow.create(follower=self.id, following=following.id, is_approved=True) return True
def follow_user(): try: # gets data from client - contains the user the current user wants to follow data = request.get_json() # gets the user the current user is going to follow user_to_follow = User.get(User.id == data['user_followed'], User.soft_delete == False) try: # checks if the current user is already following this user is_following = Follow.get(Follow.followed_by == current_user.id, Follow.followed == user_to_follow) # exception thrown if the current_user is not already followng the user except DoesNotExist: # create follow model - current user now follows the user follow = Follow.create(followed_by=current_user.id, followed=user_to_follow) # convert follow to dictionary and remove both users password follow_dict = model_to_dict(follow) del follow_dict['followed_by']['password'] del follow_dict['followed']['password'] return jsonify(data=follow_dict, status={ 'code': 201, 'message': 'User is now following {}.'.format( follow_dict['followed']['email']) }) # exception thrown if the user_to_follow doesnt exist except DoesNotExist: return jsonify(data={}, status={ 'code': 404, 'message': 'Failure getting resource.' })