예제 #1
0
def get_user(id):

    with store.get_session() as s:
        user = s.query(User).filter(User.id == id).first()

    if not user:
        return error(404)

    return jsonify(user_schema.dump(user))
예제 #2
0
def get_poet(id):

    with store.get_session() as s:
        poet = s.query(Poet).filter(Poet.id == id).first()

    if not poet:
        return error(404)

    return jsonify(poet_schema.dump(poet))
예제 #3
0
def delete_user(id):

    with store.get_session() as s:
        user = s.query(User).filter(User.id == id).first()

        if not user:
            return error(404)

        s.delete(user)
        s.commit()

    return "", 204
예제 #4
0
def delete_poet(id):

    with store.get_session() as s:
        poet = s.query(Poet).filter(Poet.id == id).first()

        if not poet:
            return error(404)

        s.delete(poet)
        s.commit()

    return "", 204
예제 #5
0
파일: quote.py 프로젝트: eriol/poetrybot
def quote(update: Update, context: CallbackContext) -> None:
    """Get a poem."""
    with store.get_session() as s:

        user_id = update.effective_user.id
        if not is_user_in_allow_list(s, user_id=user_id):
            logger.warning("Telegram user with id '{}' and username"
                           " '{}' tried to get a quote.".format(
                               user_id, update.effective_user.username))
            return

        author, argument = parse_quote(update.message.text)
        poem = get_a_random_poem(s, author=author, argument=argument)

        reply = f"{poem.verses}\n\n_{poem.author.name}_" if poem else "No quote found!"

    context.bot.send_message(chat_id=update.effective_chat.id,
                             text=reply,
                             parse_mode=ParseMode.MARKDOWN)
예제 #6
0
def create_poem():
    data = request.get_json(silent=True) or {}

    try:
        data = poem_schema.load(data)
    except ValidationError as err:
        return error(400, err.messages)

    created = None
    with store.get_session() as s:

        poem = Poem(**data)
        s.add(poem)
        s.commit()

        created = poem_schema.dump(poem)

    response = jsonify(created)
    response.status_code = 201
    return response
예제 #7
0
def update_user(id):
    data = request.get_json() or {}

    try:
        data = user_edit_schema.load(data)
    except ValidationError as err:
        return error(400, err.messages)

    updated = None
    with store.get_session() as s:
        user = s.query(User).filter(User.id == id).first()

        if not user:
            return error(404)

        user.name = data["name"]
        s.commit()

        updated = user_schema.dump(user)

    return jsonify(updated)
예제 #8
0
def update_poet(id):
    data = request.get_json() or {}

    try:
        data = poet_schema.load(data)
    except ValidationError as err:
        return error(400, err.messages)

    updated = None
    with store.get_session() as s:
        poet = s.query(Poet).filter(Poet.id == id).first()

        if not poet:
            return error(404)

        poet.name = data["name"]
        s.commit()

        updated = poet_schema.dump(poet)

    return jsonify(updated)
예제 #9
0
def create_user():
    data = request.get_json(silent=True) or {}

    try:
        data = user_schema.load(data)
    except ValidationError as err:
        return error(400, err.messages)

    created = None
    with store.get_session() as s:
        if s.query(User).filter(User.id == data["id"]).first():
            return error(400, "user with the specified id already exists")

        user = User(**data)
        s.add(user)
        s.commit()

        created = user_schema.dump(user)

    response = jsonify(created)
    response.status_code = 201
    return response
예제 #10
0
def update_poem(id):
    data = request.get_json(silent=True) or {}

    try:
        data = poem_schema.load(data)
    except ValidationError as err:
        return error(400, err.messages)

    updated = None
    with store.get_session() as s:
        poem = s.query(Poem).filter(Poem.id == id).first()

        if not poem:
            return error(404)

        for key in data:
            setattr(poem, key, data[key])
        s.commit()

        updated = poem_schema.dump(poem)

    return jsonify(updated)
예제 #11
0
def create_poet():
    data = request.get_json() or {}

    try:
        data = poet_schema.load(data)
    except ValidationError as err:
        return error(400, err.messages)

    created = None
    with store.get_session() as s:

        if s.query(Poet).filter(Poet.name == data["name"]).first():
            return error(400, "this poet is already present")

        poet = Poet(name=data["name"])
        s.add(poet)
        s.commit()

        created = poet_schema.dump(poet)

    response = jsonify(created)
    response.status_code = 201
    return response
예제 #12
0
def get_users():

    with store.get_session() as s:
        users = s.query(User).all()

    return jsonify(users_schema.dump(users))
예제 #13
0
def get_poets():

    with store.get_session() as s:
        poets = s.query(Poet).all()

    return jsonify(poets_schema.dump(poets))