Пример #1
0
def get_users(*,
              limit: int,
              order_by: str = "id",
              offset: Optional[str] = None) -> APIResponse:
    """Get users"""
    appbuilder = get_airflow_app().appbuilder
    session = appbuilder.get_session
    total_entries = session.query(func.count(User.id)).scalar()
    direction = desc if order_by.startswith("-") else asc
    to_replace = {"user_id": "id"}
    order_param = order_by.strip("-")
    order_param = to_replace.get(order_param, order_param)
    allowed_filter_attrs = [
        'id',
        "first_name",
        "last_name",
        "user_name",
        "email",
        "is_active",
        "role",
    ]
    if order_by not in allowed_filter_attrs:
        raise BadRequest(detail=f"Ordering with '{order_by}' is disallowed or "
                         f"the attribute does not exist on the model")

    query = session.query(User)
    users = query.order_by(direction(getattr(
        User, order_param))).offset(offset).limit(limit).all()

    return user_collection_schema.dump(
        UserCollection(users=users, total_entries=total_entries))
Пример #2
0
def get_users(limit=None, offset=None):
    """Get users"""
    appbuilder = current_app.appbuilder
    session = appbuilder.get_session
    total_entries = session.query(func.count(User.id)).scalar()
    users = session.query(User).order_by(
        User.id).offset(offset).limit(limit).all()

    return user_collection_schema.dump(
        UserCollection(users=users, total_entries=total_entries))
Пример #3
0
def get_users(*, limit: int, order_by: str = "id", offset: Optional[str] = None) -> APIResponse:
    """Get users"""
    appbuilder = current_app.appbuilder
    session = appbuilder.get_session
    total_entries = session.query(func.count(User.id)).scalar()
    to_replace = {"user_id": "id"}
    allowed_filter_attrs = [
        "user_id",
        'id',
        "first_name",
        "last_name",
        "user_name",
        "email",
        "is_active",
        "role",
    ]
    query = session.query(User)
    query = apply_sorting(query, order_by, to_replace, allowed_filter_attrs)
    users = query.offset(offset).limit(limit).all()

    return user_collection_schema.dump(UserCollection(users=users, total_entries=total_entries))