Esempio n. 1
0
def join(group_id):
    group = db_session.query(Group).get(group_id)  # type: Group
    if not group:
        abort(422, "Unknown group")

    user = db_session.query(User).get(request.form["user_id"])  # type: User
    if not user:
        abort(422, "Unknown user")

    user.groups.append(group)
    db_session.add(user)
    db_session.commit()

    return "OK"
Esempio n. 2
0
def get_members(group_id):
    group = db_session.query(Group).get(group_id)  # type: Group
    if not group:
        abort(404, "Unknown group")

    result = list(map(lambda u: u.as_json(), group.users))
    return jsonify(result)
Esempio n. 3
0
def get_groups(user_id):
    user = db_session.query(User).get(user_id)  # type: User
    if not user:
        abort(404, "Unknown user")

    result = list(map(lambda g: g.as_json(), user.groups))
    return jsonify(result)
Esempio n. 4
0
def delete(group_id):
    group = db_session.query(Group).get(group_id)  # type: Group
    if not group:
        abort(422, "Unknown group")

    db_session.delete(group)
    db_session.commit()
    return "OK"
Esempio n. 5
0
def rotate_invite_code(group_id):
    group = db_session.query(Group).get(group_id)  # type: Group
    if not group:
        abort(422, "Unknown group")

    group.invite_code = str(uuid.uuid4())
    db_session.add(group)
    db_session.commit()

    return jsonify(group.as_json())
Esempio n. 6
0
def create():
    admin_id = request.form["admin_id"]
    admin = db_session.query(User).get(admin_id)

    if not admin:
        abort(422, "Unknown admin_id")

    title = request.form["title"]

    group = Group(admin, title)
    db_session.add(group)
    db_session.commit()

    return jsonify(group.as_json())
Esempio n. 7
0
def get_user_by_tg_id():
    user = None

    if "id" in request.args:
        tg_id = request.args["id"]
        user = db_session.query(User).filter_by(tg_id=tg_id).first()
        if user is None:
            user = User()
            user.tg_id = tg_id
    elif "username" in request.args:
        tg_username = request.args["username"]
        user = db_session.query(User).filter_by(tg_username=tg_username).first()

    if user is None:
        abort(404, "Cannot find specified user")

    if "allow_update" in request.args and request.args["allow_update"].lower() == "true":
        if "username" in request.args:
            user.tg_username = request.args["username"]

    db_session.add(user)
    db_session.commit()

    return jsonify(user.as_json())
Esempio n. 8
0
def get_by_invite_code(code):
    group = db_session.query(Group).filter(Group.invite_code == code).first()  # type: Group
    if not group:
        abort(422, "Unknown group")
    return jsonify(group.as_json())
Esempio n. 9
0
def get(group_id):
    group = db_session.query(Group).get(group_id)  # type: Group
    if not group:
        abort(404, "Unknown group")
    return jsonify(group.as_json())
Esempio n. 10
0
def get(user_id):
    user = db_session.query(User).get(user_id)  # type: User
    if not user:
        abort(404, "Unknown user")
    return jsonify(user.as_json())