Esempio n. 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