def test_pagination_params_on_first_page_when_next_page_exists(fifteen_numbers): res = get_pagination(fifteen_numbers, 1, 2) params = res.pagination_params assert params.current_page == 1 assert not params.has_prev_page assert params.has_next_page assert params.next_page == 2
def get_worker_from_requests(representative_id: int, request_id: int, substring: str, page: int, size: int): with create_session() as session: request = RequestsDAL._get_request(request_id, session) try: representative = ( session.query(ContractorRepresentative).filter( ContractorRepresentative.id == representative_id).one()) except NoResultFound: raise DALError(HTTPStatus.NOT_FOUND.value) if request not in representative.contractor.requests: raise DALError(HTTPStatus.FORBIDDEN.value) res = [] for worker_in_request in request.workers_in_request: worker = worker_in_request.worker if (substring and substring not in f'{worker.last_name} {worker.first_name} {worker.patronymic}' ): continue res.append( RequestsDAL.serialize_worker_with_request_info( worker, worker_in_request)) return get_pagination(res, page, size)
def test_pagination(size: int, page: int, expected_size: int): count_of_operations: int = 15 objects: List[int] = [None] * count_of_operations for i in range(count_of_operations): objects[i] = i res: ListWithPagination[int] = get_pagination(objects, page=page, size=size) assert len(res.data) == expected_size
def get_representative_request_result(request_id: int, representative_id: int, substring: str, page: int, size: int): with create_session() as session: try: representative = ( session.query(ContractorRepresentative).filter( ContractorRepresentative.id == representative_id).one()) except NoResultFound: raise DALError(HTTPStatus.NOT_FOUND.value) request = RequestsDAL._get_request(request_id, session) if request not in representative.contractor.requests: raise DALError(HTTPStatus.FORBIDDEN.value) if request.status != RequestStatus.WAITING_FOR_READINESS: raise DALError(HTTPStatus.NOT_FOUND.value) workers = map( lambda worker_in_request: worker_in_request.worker, request.workers_in_request, ) res = [ RequestsDAL.serialize_worker(worker) for worker in workers if substring and substring in f'' f'{worker.last_name} {worker.first_name} {worker.patronymic}' or not substring ] return get_pagination(res, page, size)
def get_representative_requests( representative_id: int, substring: str, page: int, size: int, solved: bool = False, ) -> Awaitable[ListWithPagination[RequestForTemplateOut]]: with create_session() as session: try: representative: ContractorRepresentative = session.query( ContractorRepresentative).filter( ContractorRepresentative.id == representative_id).one() except NoResultFound: raise DALError(HTTPStatus.NOT_FOUND.value) res = [] for request in representative.contractor.requests: if substring and substring not in request.object_of_work.data: continue if solved and request.status != RequestStatus.CLOSED: continue if (not solved and request.status != RequestStatus.WAITING_FOR_VERIFICATION): continue res.append(RequestsDAL._serialize_request(request)) return get_pagination(res, page, size)
async def get_items( catalog_type: CatalogType, out_model: Type[T], page: int, substring: str, size: int = 10, ) -> ListWithPagination[T]: catalog = get_catalog(catalog_type, out_model) data = await catalog.get_data(substring) return get_pagination(data, page, size)
def get_users( page: int, size: int, substring: str, out_model: Type[TOutModel], user_type: UserRole, ) -> Awaitable[ListWithPagination[TOutModel]]: with create_session() as session: users = session.query(User).filter( User.type == user_type.value).all() users = UsersDAL._filter_by_substring(substring, users) users = [out_model.from_orm(user) for user in users] return get_pagination(users, page, size) # type : ignore
def get_workers( substring: str, page: int, size: int) -> Awaitable[ListWithPagination[WorkerSimpleOut]]: with create_session() as session: workers = session.query(Worker).all() res = [] for worker in workers: if (substring and substring in f'{worker.last_name} {worker.first_name} {worker.patronymic}' or not substring): res.append(RequestsDAL.serialize_worker(worker)) return get_pagination(res, page, size)
def get_users_to_register( page: int, size: int, substring: str, out_model: Type[TOutModel], user_type: UserRole, ) -> Awaitable[ListWithPagination[TOutModel]]: with create_session() as session: obj = get_db_obj_to_register(user_type) users_to_register = session.query(obj).all() users_to_register = UsersDAL._filter_by_substring( substring, users_to_register) users = [out_model.from_orm(user) for user in users_to_register] return get_pagination(users, page, size) # type: ignore
def get_operators_requests( substring: str, page: int, size: int) -> Awaitable[ListWithPagination[RequestForTemplateOut]]: with create_session() as session: requests = (session.query(Request).filter( Request.status == RequestStatus.WAITING_FOR_VERIFICATION).all()) if substring: requests = [ request.contractor.title for request in requests if substring in request.contractor.title ] serialized_requests = [] for request in requests: serialized_requests.append( RequestsDAL._serialize_request(request)) return get_pagination(serialized_requests, page, size) # type: ignore
def get_representative_workers(representative_id: int, substring: str, page: int, size: int): with create_session() as session: try: representative: ContractorRepresentative = session.query( ContractorRepresentative).filter( ContractorRepresentative.id == representative_id).one() except NoResultFound: raise DALError(HTTPStatus.NOT_FOUND.value) return get_pagination( [ RequestsDAL.serialize_worker(worker) for worker in representative.contractor.workers if substring and substring in f'{worker.last_name} {worker.first_name} {worker.patronymic}' or not substring ], page, size, )
def get_operator_workers_in_request( request_id: int, substring: str, page: int, size: int, is_result: bool = False ) -> Awaitable[ListWithPagination[WorkerInListOut]]: with create_session() as session: request = RequestsDAL._get_request(request_id, session) workers = request.workers_in_request if substring: workers = [ worker for worker in workers if substring in f'{worker.worker.last_name} {worker.worker.first_name} {worker.worker.patronymic}' ] workers_to_out = [] if request.status != RequestStatus.WAITING_FOR_VERIFICATION: raise DALError(HTTPStatus.BAD_REQUEST.value) for worker in workers: if (not is_result and worker.status != WorkerInRequestStatus.WAITING_FOR_VERIFICATION): continue if is_result and not ( worker.status == WorkerInRequestStatus.ACCEPTED or worker.status == WorkerInRequestStatus.CANCELLED): continue workers_to_out.append( WorkerInListOut( last_name=worker.worker.last_name, first_name=worker.worker.first_name, patronymic=worker.worker.patronymic, id=worker.id, profession=worker.worker.profession.data, penalty_points=worker.worker.penalty_points, status=worker.status, reason_of_rejection=worker.reason_for_rejection.data if worker.reason_for_rejection else None, )) return get_pagination(workers_to_out, page, size)
def get_representative_workers_in_request( representative_id: int, request_id: int, substring: str, page: int, size: int, is_result: bool = False, ): with create_session() as session: request = RequestsDAL._get_request(request_id, session) try: representative: ContractorRepresentative = session.query( ContractorRepresentative).filter( ContractorRepresentative.id == representative_id).one() except NoResultFound: raise DALError(HTTPStatus.NOT_FOUND.value) if request not in representative.contractor.requests: raise DALError(HTTPStatus.FORBIDDEN.value) res = [] was_broken: bool = False for worker in representative.contractor.workers: if was_broken: was_broken = False if (substring and substring not in f'{worker.last_name} {worker.first_name} {worker.patronymic}' ): continue for worker_in_request in worker.worker_requests: request_ = worker_in_request.request if (request_.object_of_work_id == request.object_of_work_id and request_.contract_id == request.contract_id and worker_in_request.status != WorkerInRequestStatus.CANCELLED): was_broken = True break if not was_broken: res.append(RequestsDAL.serialize_worker(worker)) return get_pagination(res, page, size)
def test_pagination_params_on_single_page(fifteen_numbers): res = get_pagination(fifteen_numbers, 1, 15) params = res.pagination_params assert not params.has_next_page assert not params.has_prev_page assert params.current_page == 1
def test_pagination_with_invalid_size_and_page(): with pytest.raises(DALError): get_pagination([], 0, 0)
def test_pagination_will_not_raise_error_when_no_elements_in_list(): res = get_pagination([], 1, 10) assert res.data == []