Example #1
0
def get_leases_by_user(token: str, res: Response, with_closed: bool = False):
    token_user, error, code = get_user_by_token(token)
    if error:
        res.status_code = code
        return error

    l = [
        x.to_dict()
        for x in select(
            u
            for u in Lease
            if u.user_id.id == token_user.id and u.is_returned in (with_closed, False)
        )[:]
    ]

    res = []
    for lease in l:
        t = Token.get(lease_id=lease["id"])
        if t is None:
            res.status_code = status.HTTP_400_BAD_REQUEST
            return {"err": f"Lease without token with id {lease['id']}"}
        try:
            c = Cell[lease["cell_id"]]
        except RowNotFound:
            c = None
        if c is None:
            res.status_code = status.HTTP_400_BAD_REQUEST
            return {"err": f"Lease without cell with id {lease['id']}"}
        current = lease
        current["token"] = t.value
        current["cell_type"] = c.cell_type_id
        res.append(current)

    return res
Example #2
0
def get_passes(token: str, res: Response, user_id: int = None):
    token_user, error, code = get_user_by_token(token)
    if error:
        res.status_code = code
        return error

    if not token_user.is_admin and user_id != token_user.id:
        res.status_code = status.HTTP_400_BAD_REQUEST
        return {"err": "Only admins can view passes of other users!"}

    if user_id is not None:
        check_id = user_id
    else:
        check_id = token_user.id

    types = [
        PassType(name=t.name, id=t.id) for t in select(c for c in Pass_Type)[:]
    ]
    passes = [
        x.to_dict()
        for x in select(t for t in Pass if t.user_id.id == check_id)
    ]
    res = []
    for p in passes:
        curr = p.copy()
        curr["type_name"] = types[curr["pass_type"] - 1].name
        res.append(curr)

    return res
Example #3
0
def get_all_passes(token: str, res: Response):
    token_user, error, code = get_user_by_token(token)
    if error:
        res.status_code = code
        return error

    if not token_user.is_admin:
        res.status_code = status.HTTP_400_BAD_REQUEST
        return {"err": "Token user is not admin!"}

    passes = [x.to_dict() for x in select(t for t in Pass)]
    types = [
        PassType(name=t.name, id=t.id) for t in select(c for c in Pass_Type)[:]
    ]
    res = []
    for p in passes:
        curr = p.copy()
        curr["type_name"] = types[curr["pass_type"] - 1].name
        res.append(curr)

    return res
Example #4
0
def current_types(token: str, res: Response):
    token_user, error, code = get_user_by_token(token)
    if error:
        res.status_code = code
        return error
    cells = [
        x.to_dict() for x in select(c for c in Cell if c.is_taken == False)[:]
    ]
    free_types = set()
    res = []
    for cell in cells:
        if cell["cell_type_id"] not in free_types:
            free_types.add(cell["cell_type_id"])
            t = dict()
            t["id"] = cell["cell_type_id"]
            try:
                t["name"] = Cell_Type[cell["cell_type_id"]].name
            except RowNotFound:
                t["name"] = None
            res.append(t)

    return res
async def users_list(response: Response):
    response = []
    response.append(UserOut(id=1, name='hoge', email='*****@*****.**'))
    response.append(UserOut(id=2, name='fuga', email='*****@*****.**'))
    return response