def delete_request(idol_id): identity = get_jwt_identity() fan = User.get(username=identity) idol = User.get_by_id(idol_id) request = Follow.get(fan=fan, idol=idol, is_approve=False) if request.delete_instance(): return jsonify({'message': 'success'}) else: return jsonify({'message': 'failed'})
def accept_request(fan_id): identity = get_jwt_identity() fan = User.get_by_id(fan_id) idol = User.get(username=identity) request = Follow.get(fan=fan, idol=idol, is_approve=False) if request: request.is_approve = True if request.save(): return jsonify({'message': 'success'}) else: return jsonify({'message': 'failed'}) else: return jsonify({'message': 'Request Not Found'})
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.' })
def unfollow_route(request, follow_id): """ Remove a follow. Must be current user's own follow. """ current_user = get_current_user(request) if not current_user: return abort(401) follow = Follow.get(id=follow_id) if not follow: return abort(404) if follow['user_id'] != current_user['id']: return abort(403) follow, errors = follow.delete() if errors: return 400, {'errors': errors, 'ref': 'iGmpx8UwoFcKNmSKq9Aocy1a'} return 200, {}
def unfollow_route(request, follow_id): """ Remove a follow. Must be current user's own follow. """ db_conn = request["db_conn"] current_user = get_current_user(request) if not current_user: return abort(401) follow = Follow.get(db_conn, id=follow_id) if not follow: return abort(404) if follow["user_id"] != current_user["id"]: return abort(403) follow, errors = follow.delete(db_conn) if errors: return 400, {"errors": errors, "ref": "iGmpx8UwoFcKNmSKq9Aocy1a"} return 200, {}
def unfollow_route(request, follow_id): """ Remove a follow. Must be current user's own follow. """ current_user = get_current_user(request) if not current_user: return abort(401) follow = Follow.get(id=follow_id) if not follow: return abort(404) if follow['user_id'] != current_user['id']: return abort(403) follow, errors = follow.delete() if errors: return 400, { 'errors': errors, 'ref': 'iGmpx8UwoFcKNmSKq9Aocy1a' } return 200, {}