def get_role_under_user(session: Session, user: UserDB, page_info: PageInfo) -> Pagination: if user.is_superuser: pagination = Pagination(session.query(RoleDB), page_info=page_info) return pagination query = session.query(RoleDB) condition_list = [RoleDB.id.in_(user.role_id_set)] for role_id in user.role_id_set: condition_list.append(RoleDB.grand_id.like(f'%|{role_id}|%')) query = query.filter(or_many_condition(condition_list)) pagination = Pagination(query, page_info=page_info) return pagination
async def permission_list(request: Request, page_info: PageInfo = Depends(PageInfo_), user: UserDB = Depends(get_user), session: Session = Depends(get_session)): pagination = Pagination(session.query(PermissionDB), page_info=page_info) data = [PermissionSerializer.from_orm(m).dict() for m in pagination.items] paginate_info = make_paginate_info(pagination, request) return success_response(data, paginate_info)
def test_pagination(): with get_session_local() as session: # session = TestingSessionLocal() for i in range(10): session.add(generate_role()) session.commit() p1 = Pagination(session.query(RoleDB), 1, 5) assert p1.total == 10 assert len(p1.items) == 5 p2 = Pagination(session.query(RoleDB), 2, 5) assert p2.total == 10 assert len(p2.items) == 5 p3 = Pagination(session.query(RoleDB), 2, 8) assert p3.total == 10 assert len(p3.items) == 2
async def get_all_form(request: Request, page_info: PageInfo = Depends(PageInfo_), manager: UserDB = Depends(get_user), session: Session = Depends(get_session)): query = session.query(FormDB) paginate = Pagination(query, page_info=page_info) paginate_info = make_paginate_info(paginate, request) data = [to_FormDetailSerializer(form) for form in paginate.items] return success_response(data, paginate_info)
def get_users_by_role_id(session: Session, i: int, page_info: PageInfo) -> Pagination: users = session.query(UserDB). \ filter(RoleDB.id == i). \ filter(User2RoleDB.role_id == RoleDB.id). \ filter(User2RoleDB.user_id == UserDB.id) pagination = Pagination(users, page_info=page_info) return pagination
async def search_user_(request: Request, page_info: PageInfo = Depends(PageInfo_), search_condition: CommonlyUsedUserSearch = Depends( CommonlyUsedUserSearch_), manager: UserDB = Depends(get_user), session: Session = Depends(get_session)): query = session.query(UserDB).join(User2RoleDB, User2RoleDB.user_id == UserDB.id) query, error = common_user_search_with_permission_check( manager, query, session, search_condition) if error: return error_response(error) paginate = Pagination(query, page_info=page_info) paginate_info = make_paginate_info(paginate, request) data = [ BaseUserSerializer.from_orm(op_user).dict() for op_user in paginate.items ] return success_response(data, paginate_info)