diff --git a/src/nsls2api/api/v1/facility_api.py b/src/nsls2api/api/v1/facility_api.py index b6bed2c..0246d28 100644 --- a/src/nsls2api/api/v1/facility_api.py +++ b/src/nsls2api/api/v1/facility_api.py @@ -65,7 +65,9 @@ async def get_proposals_for_cycle(facility: FacilityName, cycle: str): proposal_list = await proposal_service.fetch_proposals_for_cycle(cycle, facility) if proposal_list is None: return fastapi.responses.JSONResponse( - {"error": f"No proposals were found for cycle {cycle} for facility {facility.name}"}, + { + "error": f"No proposals were found for cycle {cycle} for facility {facility.name}" + }, status_code=404, ) model = CycleProposalList( diff --git a/src/nsls2api/api/v1/jobs_api.py b/src/nsls2api/api/v1/jobs_api.py index 48e68c9..0a6961c 100644 --- a/src/nsls2api/api/v1/jobs_api.py +++ b/src/nsls2api/api/v1/jobs_api.py @@ -82,10 +82,12 @@ async def sync_proposal_types(facility: FacilityName = FacilityName.nsls2): "/sync/proposals/cycle/{cycle}", dependencies=[Depends(get_current_user)], include_in_schema=SYNC_ROUTES_IN_SCHEMA, - tags=["sync"], deprecated=True, + tags=["sync"], + deprecated=True, ) -async def sync_proposals_for_cycle(request: Request, cycle: str, - facility: FacilityName = FacilityName.nsls2) -> BackgroundJob: +async def sync_proposals_for_cycle( + request: Request, cycle: str, facility: FacilityName = FacilityName.nsls2 +) -> BackgroundJob: sync_params = JobSyncParameters(cycle=cycle, facility=facility) job = await background_service.create_background_job( JobActions.synchronize_proposals_for_cycle, @@ -100,8 +102,9 @@ async def sync_proposals_for_cycle(request: Request, cycle: str, include_in_schema=SYNC_ROUTES_IN_SCHEMA, tags=["sync"], ) -async def sync_proposals_for_facility_cycle(request: Request, - facility: FacilityName, cycle: str) -> BackgroundJob: +async def sync_proposals_for_facility_cycle( + request: Request, facility: FacilityName, cycle: str +) -> BackgroundJob: sync_params = JobSyncParameters(cycle=cycle, facility=facility) job = await background_service.create_background_job( JobActions.synchronize_proposals_for_cycle, @@ -125,12 +128,13 @@ async def sync_cycles(facility: FacilityName = FacilityName.nsls2): @router.get( "/sync/update-cycles/{facility}", include_in_schema=SYNC_ROUTES_IN_SCHEMA, - tags=["sync"], summary="Updates the local (nsls2core DB) cycle <-> proposal mapping" + tags=["sync"], + summary="Updates the local (nsls2core DB) cycle <-> proposal mapping", ) async def sync_update_cycles( - request: fastapi.Request, - facility: FacilityName = FacilityName.nsls2, - cycle: Optional[str] = None, + request: fastapi.Request, + facility: FacilityName = FacilityName.nsls2, + cycle: Optional[str] = None, ): sync_params = JobSyncParameters(facility=facility, sync_source=JobSyncSource.PASS) diff --git a/src/nsls2api/api/v1/proposal_api.py b/src/nsls2api/api/v1/proposal_api.py index 8ffbb0a..b53f2e7 100644 --- a/src/nsls2api/api/v1/proposal_api.py +++ b/src/nsls2api/api/v1/proposal_api.py @@ -1,4 +1,3 @@ -from http.client import HTTPException from typing import Annotated import fastapi diff --git a/src/nsls2api/services/pass_service.py b/src/nsls2api/services/pass_service.py index 9ceac02..4bc55a9 100644 --- a/src/nsls2api/services/pass_service.py +++ b/src/nsls2api/services/pass_service.py @@ -121,8 +121,9 @@ async def get_saf_from_proposal( return saf_list -async def get_commissioning_proposals_by_year(year: str, facility_name: FacilityName = FacilityName.nsls2) -> Optional[list[PassProposal]]: - +async def get_commissioning_proposals_by_year( + year: str, facility_name: FacilityName = FacilityName.nsls2 +) -> Optional[list[PassProposal]]: pass_facility = await facility_service.pass_id_for_facility(facility_name) if not pass_facility: error_message: str = f"Facility {facility_name} does not have a PASS ID." diff --git a/src/nsls2api/services/proposal_service.py b/src/nsls2api/services/proposal_service.py index b919eba..88e7f77 100644 --- a/src/nsls2api/services/proposal_service.py +++ b/src/nsls2api/services/proposal_service.py @@ -74,8 +74,12 @@ async def recently_updated(count=5, beamline: str | None = None): # return result -async def fetch_proposals_for_cycle(cycle_name: str, facility_name: FacilityName = FacilityName.nsls2) -> list[str]: - cycle = await Cycle.find_one(Cycle.name == cycle_name, Cycle.facility == facility_name) +async def fetch_proposals_for_cycle( + cycle_name: str, facility_name: FacilityName = FacilityName.nsls2 +) -> list[str]: + cycle = await Cycle.find_one( + Cycle.name == cycle_name, Cycle.facility == facility_name + ) if cycle is None: raise LookupError(f"Cycle {cycle} not found in local database.") return cycle.proposals @@ -151,13 +155,13 @@ async def search_proposals(search_text: str) -> Optional[list[Proposal]]: # Get a list of proposals that match the given criteria async def fetch_proposals( - proposal_id: list[str] | None = None, - beamline: list[str] | None = None, - cycle: list[str] | None = None, - facility: list[str] | None = None, - page_size: int = 10, - page: int = 1, - include_directories: bool = False, + proposal_id: list[str] | None = None, + beamline: list[str] | None = None, + cycle: list[str] | None = None, + facility: list[str] | None = None, + page_size: int = 10, + page: int = 1, + include_directories: bool = False, ) -> Optional[list[ProposalFullDetails]]: query = [] @@ -202,7 +206,7 @@ async def fetch_proposals( async def proposal_type_description_from_pass_type_id( - pass_type_id: int, + pass_type_id: int, ) -> Optional[str]: proposal_type = await ProposalType.find_one( ProposalType.pass_id == str(pass_type_id) @@ -245,7 +249,7 @@ async def fetch_users_on_proposal(proposal_id: str) -> Optional[list[User]]: async def fetch_usernames_from_proposal( - proposal_id: str, + proposal_id: str, ) -> Optional[list[str]]: proposal = await proposal_by_id(proposal_id) @@ -304,18 +308,18 @@ async def has_valid_cycle(proposal: Proposal): # If we don't have any cycles listed and this is not a commissioning # proposal then the cycle information is invalid return not ( - (len(proposal.cycles) == 0) - and ( - proposal.pass_type_id != 300005 - or proposal.type == "Beamline Commissioning (beamline staff only)" - ) + (len(proposal.cycles) == 0) + and ( + proposal.pass_type_id != 300005 + or proposal.type == "Beamline Commissioning (beamline staff only)" + ) ) async def is_commissioning(proposal: Proposal): return ( - proposal.pass_type_id == "300005" - or proposal.type == "Beamline Commissioning (beamline staff only)" + proposal.pass_type_id == "300005" + or proposal.type == "Beamline Commissioning (beamline staff only)" ) @@ -439,7 +443,7 @@ async def generate_fake_proposal_id() -> int: async def generate_fake_test_proposal( - facility_name: FacilityName = FacilityName.nsls2, add_specific_user=None + facility_name: FacilityName = FacilityName.nsls2, add_specific_user=None ) -> Optional[Proposal]: """ Generates a fake test proposal. diff --git a/src/nsls2api/services/sync_service.py b/src/nsls2api/services/sync_service.py index fb8fc0a..0818012 100644 --- a/src/nsls2api/services/sync_service.py +++ b/src/nsls2api/services/sync_service.py @@ -317,7 +317,9 @@ async def synchronize_proposal_from_pass(proposal_id: str) -> None: logger.debug(f"Response: {response}") -async def update_proposals_with_cycle(cycle_name: str, facility_name: FacilityName = FacilityName.nsls2) -> None: +async def update_proposals_with_cycle( + cycle_name: str, facility_name: FacilityName = FacilityName.nsls2 +) -> None: """ Update the cycle <-> proposals mapping for the given cycle. @@ -325,9 +327,13 @@ async def update_proposals_with_cycle(cycle_name: str, facility_name: FacilityNa :type cycle_name: str """ - proposal_list = await proposal_service.fetch_proposals_for_cycle(cycle_name, facility_name=facility_name) + proposal_list = await proposal_service.fetch_proposals_for_cycle( + cycle_name, facility_name=facility_name + ) - logger.info(f"Found {len(proposal_list)} proposals for {facility_name} cycle {cycle_name}.") + logger.info( + f"Found {len(proposal_list)} proposals for {facility_name} cycle {cycle_name}." + ) for proposal_id in proposal_list: # Add the cycle to the Proposal object @@ -352,14 +358,19 @@ async def worker_synchronize_proposal_from_pass(proposal_id: str) -> None: ) -async def worker_synchronize_proposals_for_cycle_from_pass(cycle: str, - facility_name: FacilityName = FacilityName.nsls2) -> None: +async def worker_synchronize_proposals_for_cycle_from_pass( + cycle: str, facility_name: FacilityName = FacilityName.nsls2 +) -> None: start_time = datetime.datetime.now() cycle_year = await facility_service.cycle_year(cycle, facility_name=facility_name) - proposals = await proposal_service.fetch_proposals_for_cycle(cycle, facility_name=facility_name) - logger.info(f"Synchronizing {len(proposals)} proposals for facility {facility_name} in {cycle} cycle.") + proposals = await proposal_service.fetch_proposals_for_cycle( + cycle, facility_name=facility_name + ) + logger.info( + f"Synchronizing {len(proposals)} proposals for facility {facility_name} in {cycle} cycle." + ) for proposal_id in proposals: logger.info(f"Synchronizing proposal {proposal_id}.") @@ -367,7 +378,9 @@ async def worker_synchronize_proposals_for_cycle_from_pass(cycle: str, commissioning_proposals: list[ PassProposal - ] = await pass_service.get_commissioning_proposals_by_year(cycle_year, facility_name=facility_name) + ] = await pass_service.get_commissioning_proposals_by_year( + cycle_year, facility_name=facility_name + ) logger.info( f"Synchronizing {len(proposals)} commissioning proposals for the year {cycle_year}." )