def crud_get_users( item_to_dict, enforcer: casbin.Enforcer, db: Session, current_user: Auth0ClaimsPatched, archived: Optional[bool] = None, page: Optional[int] = None, per_page: Optional[int] = None, ) -> List[dict]: return paginatify( items_label='users', items=[ user for user in all_users(db, archived).order_by( User.created_on.desc()) if check_access(enforcer, user=current_user.username, path=f"/user/{str(user.id)}", method="GET") and user and user.current ], item_to_dict=item_to_dict, page=page, per_page=per_page, )
def crud_get_runs( item_to_dict, enforcer: casbin.Enforcer, db: Session, current_user: Auth0ClaimsPatched, protocol: Optional[int] = None, run: Optional[int] = None, plate: Optional[str] = None, reagent: Optional[str] = None, sample: Optional[str] = None, creator: Optional[str] = None, archived: Optional[bool] = None, page: Optional[int] = None, per_page: Optional[int] = None, ) -> List[dict]: runs_queries = [] # Add filter specific queries. These will be intersected later on. if protocol: runs_queries.append( all_runs(db, archived)\ .join(ProtocolVersion, ProtocolVersion.id == Run.protocol_version_id)\ .filter(ProtocolVersion.protocol_id == protocol) ) if run: runs_queries.append( all_runs(db, archived)\ .filter(Run.id == run) ) if plate: run_version_query = all_runs(db, archived)\ .join(RunVersion, RunVersion.id == Run.version_id) runs_subquery = filter_by_plate_label(run_version_query, plate) runs_queries.append(runs_subquery) if reagent: run_version_query = all_runs(db, archived)\ .join(RunVersion, RunVersion.id == Run.version_id) runs_subquery = filter_by_reagent_label(run_version_query, reagent) runs_queries.append(runs_subquery) if sample: run_version_query = all_runs(db, archived)\ .join(RunVersion, RunVersion.id == Run.version_id) runs_subquery = filter_by_sample_label(run_version_query, sample) runs_queries.append(runs_subquery) if creator: runs_queries.append( all_runs(db, archived)\ # .filter(Run.id == run) .filter(Run.created_by == creator) ) # Add a basic non-deleted items query if no filters were specified. if len(runs_queries) == 0: runs_queries.append(all_runs(db, archived)) # Only return the intersection of all queries. runs_query = reduce(lambda a, b: a.intersect(b), runs_queries) return paginatify( items_label='runs', items=[ run for run in runs_query.distinct().order_by(Run.created_on.desc()) if check_access(enforcer, user=current_user.username, path=f"/run/{str(run.id)}", method="GET") and run and run.current ], item_to_dict=lambda run: item_to_dict(fix_plate_markers_run(db, run)), page=page, per_page=per_page, )
def crud_get_run_samples( item_to_dict, enforcer: casbin.Enforcer, db: Session, current_user: Auth0ClaimsPatched, run_id: Optional[int] = None, protocol: Optional[int] = None, plate: Optional[str] = None, reagent: Optional[str] = None, creator: Optional[str] = None, archived: Optional[bool] = None, page: Optional[int] = None, per_page: Optional[int] = None, ) -> List[dict]: if not check_access(enforcer, user=current_user.username, path=f"/run/{str(run_id)}", method="GET"): raise HTTPException(status_code=403, detail='Insufficient Permissions') run = db.query(Run).get(run_id) if not run or run.is_deleted: raise HTTPException(status_code=404, detail='Run Not Found') samples_queries = [] # Add filter specific queries. These will be intersected later on. if protocol: samples_queries.append( all_samples(db, run, archived)\ .join(ProtocolVersion, ProtocolVersion.id == Sample.protocol_version_id)\ .filter(ProtocolVersion.protocol_id == protocol) ) if plate: samples_queries.append( all_samples(db, run, archived)\ .filter(Sample.plate_id == plate) ) if reagent: run_version_query = all_samples(db, run, archived)\ .join(RunVersion, RunVersion.id == Sample.run_version_id) samples_subquery = filter_by_reagent_label(run_version_query, reagent) samples_queries.append(samples_subquery) if creator: samples_queries.append( all_samples(db, run, archived)\ .filter(Sample.created_by == creator) ) # Add a basic non-deleted items query if no filters were specified. if len(samples_queries) == 0: samples_queries.append(all_samples(db, run, archived)) # Only return the intersection of all queries. samples_query = reduce(lambda a, b: a.intersect(b), samples_queries) return paginatify( items_label='samples', items=[ sample for sample in samples_query.distinct().order_by( Sample.sample_id.asc()) ], item_to_dict=item_to_dict, page=page, per_page=per_page, )
def graphql_crud_get_samples( enforcer: casbin.Enforcer, current_user: Auth0CurrentUserPatched, info: ResolveInfo, # Search parameters protocol: Optional[int] = None, run: Optional[int] = None, plate: Optional[str] = None, reagent: Optional[str] = None, sample: Optional[str] = None, creator: Optional[str] = None, archived: Optional[bool] = None, # Paging parameters page: Optional[int] = None, per_page: Optional[int] = None, ): # Calculate which top level fields to remove. top_level_ignore = { 'sample_id', 'plate_id', 'run_version_id', 'protocol_version_id', 'created_by', 'created_on', 'updated_by', 'updated_on', 'run_id', 'protocol_id' } # Flatten `info` parameter into jsonb_query_path statements. select_args = [] top_level = set() for result in graphql_ast_flatten_field(info.field_asts[0], info.fragments, info.return_type, info.schema): result_parts = result.split('.') if len(result_parts) > 3 and result_parts[3] not in top_level_ignore: top_level.add(result_parts[3]) jsonb_fields = [ 'sample_id', 'sampleID', 'plate_id', 'plateID', 'run_version_id', 'protocol_version_id', 'created_by', 'created_on', 'updated_by', 'updated_on', 'run_id', 'runID', 'protocol_id', 'protocolID', ] select_args = [ Sample.sample_id.label('sample_id'), Sample.sample_id.label('sampleID'), Sample.plate_id.label('plate_id'), Sample.plate_id.label('plateID'), Sample.run_version_id.label('run_version_id'), Sample.protocol_version_id.label('protocol_version_id'), Sample.created_by.label('created_by'), Sample.created_on.label('created_on'), SampleVersion.updated_by.label('updated_by'), SampleVersion.updated_on.label('updated_on'), RunVersion.run_id.label('run_id'), RunVersion.run_id.label('runID'), ProtocolVersion.protocol_id.label('protocol_id'), ProtocolVersion.protocol_id.label('protocolID'), ] for field in top_level: jsonb_fields.append(field) select_args.append(SampleVersion.data[field].label(field)) db = get_session(info) # Join with additional tables as necessary for search params. from_tables = OrderedDict() filters = [] if protocol: filters.append(ProtocolVersion.protocol_id == protocol) if run: filters.append(RunVersion.run_id == run) if plate: filters.append(Sample.plate_id.like(f"%{plate}%")) if reagent: filters.append(filter_by_reagent_label_filter(reagent)) if sample: filters.append(Sample.sample_id.like(f"%{sample}%")) if creator: filters.append(Sample.created_by == creator) if archived is None or archived == False: filters.append(Sample.is_deleted == False) query = db.query(*select_args)\ .select_from(Sample)\ .join(SampleVersion, SampleVersion.id == Sample.version_id)\ .join(RunVersion, RunVersion.id == Sample.run_version_id)\ .join(ProtocolVersion, ProtocolVersion.id == Sample.protocol_version_id) for join_cls, join_filter in from_tables.items(): query = query.join(join_cls, join_filter) # Apply search filters. for search_filter in filters: query = query.filter(search_filter) # Get results query = query.distinct().order_by(Sample.created_on.desc()) rows = [ sample for sample in query if check_access(enforcer, user=current_user.username, path=f"/run/{str(sample.run_id)}", method="GET") ] return paginatify( items_label='samples', items=rows, item_to_dict=lambda sample: SampleResult.parse_obj( add_sample_id(sample._asdict())), page=page, per_page=per_page, )