Ejemplo n.º 1
0
def get_roles(limit=None, offset=None):
    """Get roles"""
    appbuilder = current_app.appbuilder
    session = appbuilder.get_session
    total_entries = session.query(func.count(Role.id)).scalar()
    query = session.query(Role)

    roles = query.order_by(Role.name).offset(offset).limit(limit).all()

    return role_collection_schema.dump(
        RoleCollection(roles=roles, total_entries=total_entries))
def get_roles(limit, order_by='name', offset=None):
    """Get roles"""
    appbuilder = current_app.appbuilder
    session = appbuilder.get_session
    total_entries = session.query(func.count(Role.id)).scalar()
    to_replace = {"role_id": "id"}
    allowed_filter_attrs = ['role_id', 'name']
    query = session.query(Role)
    query = apply_sorting(query, order_by, to_replace, allowed_filter_attrs)
    roles = query.offset(offset).limit(limit).all()

    return role_collection_schema.dump(RoleCollection(roles=roles, total_entries=total_entries))
Ejemplo n.º 3
0
def get_roles(*, order_by: str = "name", limit: int, offset: Optional[int] = None) -> APIResponse:
    """Get roles"""
    appbuilder = current_app.appbuilder
    session = appbuilder.get_session
    total_entries = session.query(func.count(Role.id)).scalar()
    to_replace = {"role_id": "id"}
    allowed_filter_attrs = ["role_id", "name"]
    query = session.query(Role)
    query = apply_sorting(query, order_by, to_replace, allowed_filter_attrs)
    roles = query.offset(offset).limit(limit).all()

    return role_collection_schema.dump(RoleCollection(roles=roles, total_entries=total_entries))
 def test_serialize(self):
     instance = RoleCollection([self.role1, self.role2], total_entries=2)
     deserialized = role_collection_schema.dump(instance)
     assert deserialized == {
         'roles': [
             {
                 'name': 'Test1',
                 'actions': [{'resource': {'name': 'Connections'}, 'action': {'name': 'can_create'}}],
             },
             {
                 'name': 'Test2',
                 'actions': [{'resource': {'name': 'DAGs'}, 'action': {'name': 'can_edit'}}],
             },
         ],
         'total_entries': 2,
     }
Ejemplo n.º 5
0
def get_roles(*, order_by: str = "name", limit: int, offset: Optional[int] = None) -> APIResponse:
    """Get roles"""
    appbuilder = current_app.appbuilder
    session = appbuilder.get_session
    total_entries = session.query(func.count(Role.id)).scalar()
    direction = desc if order_by.startswith("-") else asc
    to_replace = {"role_id": "id"}
    order_param = order_by.strip("-")
    order_param = to_replace.get(order_param, order_param)
    allowed_filter_attrs = ["role_id", "name"]
    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(Role)
    roles = query.order_by(direction(getattr(Role, order_param))).offset(offset).limit(limit).all()

    return role_collection_schema.dump(RoleCollection(roles=roles, total_entries=total_entries))