def get_followers(username):
    reader = Reader.query.filter_by(username=username).first()
    if not reader:
        raise ResourceNotFound("A user with the specified ID does not exist")

    followers = reader.followers
    return jsonify(readers_schema.dump(followers))
def usersearch():
    search_term = request.json.get("search")
    search_term = f"%{search_term}%"

    users = Reader.query.filter(
        Reader.username.ilike(search_term)
        & Reader.roles.contains("user")).all()

    return jsonify(readers_schema.dump(users))
def delete_reader(id_):
    if not isinstance(id_, int) and not id_.isdigit():
        raise InvalidRequest(
            r"Id should be an integer or a string interpretable as an integer",
        )
    reader = Reader.query.filter_by(id=id_).first()

    # Check user exists
    if not reader:
        raise ResourceNotFound("A user with the specified ID does not exist")

    # Check we are not deleting an admin
    if "admin" in reader.roles:
        raise ForbiddenResource("Cannot delete an admin")

    # Delete the user
    db.session.delete(reader)
    db.session.commit()

    # Return the new state of all users in the db
    users = Reader.query.all()
    return jsonify(readers_schema.dump(users))
def get_readers():
    readers = Reader.query.all()
    return jsonify(readers_schema.dump(readers))