diff --git a/xas-standards-api/src/xas_standards_api/app.py b/xas-standards-api/src/xas_standards_api/app.py index 25a4f88..41fdc0e 100644 --- a/xas-standards-api/src/xas_standards_api/app.py +++ b/xas-standards-api/src/xas_standards_api/app.py @@ -1,6 +1,6 @@ import datetime import os -from typing import Annotated, List, Optional, Union +from typing import Annotated, List, Optional import requests from fastapi import ( @@ -123,7 +123,9 @@ async def get_current_user( @app.get("/api/user") -async def check( session: Session = Depends(get_session), user_id: str = Depends(get_current_user)): +async def check( + session: Session = Depends(get_session), user_id: str = Depends(get_current_user) +): statement = select(Person).where(Person.identifier == user_id) person = session.exec(statement).first() @@ -168,38 +170,42 @@ def read_standards( ) -> CursorPage[XASStandardResponse]: statement = select(XASStandard).where( - XASStandard.review_status == ReviewStatus.approved) + XASStandard.review_status == ReviewStatus.approved + ) if element: statement = statement.join(Element, XASStandard.element_z == Element.z).where( Element.symbol == element ) - return paginate( - session, statement.order_by(XASStandard.id), + session, + statement.order_by(XASStandard.id), ) + @app.get("/api/admin/standards") def read_standards_admin( session: Session = Depends(get_session), user_id: str = Depends(get_current_user), ) -> CursorPage[AdminXASStandardResponse]: - + statement = select(Person).where(Person.identifier == user_id) person = session.exec(statement).first() if person is None or not person.admin: - raise HTTPException(status_code=401, detail=f"No standard with id={user_id}") - + raise HTTPException(status_code=401, detail=f"No standard with id={user_id}") + if not person.admin: raise HTTPException(status_code=401, detail=f"User {user_id} not admin") - - statement = select(XASStandard).where(XASStandard.review_status == ReviewStatus.pending) + statement = select(XASStandard).where( + XASStandard.review_status == ReviewStatus.pending + ) return paginate(session, statement.order_by(XASStandard.id)) + @app.get("/api/standards/{id}") async def read_standard( id: int, session: Session = Depends(get_session) @@ -251,16 +257,18 @@ def add_standard_file( @app.patch("/api/standards") -def submit_review(review: XASStandardAdminReviewInput, - session: Session = Depends(get_session), - user_id: str = Depends(get_current_user)): - +def submit_review( + review: XASStandardAdminReviewInput, + session: Session = Depends(get_session), + user_id: str = Depends(get_current_user), +): + statement = select(Person).where(Person.identifier == user_id) person = session.exec(statement).first() if person is None or not person.admin: - raise HTTPException(status_code=401, detail=f"No standard with id={user_id}") - + raise HTTPException(status_code=401, detail=f"No standard with id={user_id}") + if not person.admin: raise HTTPException(status_code=401, detail=f"User {user_id} not admin") return update_review(session, review, person.id) diff --git a/xas-standards-api/src/xas_standards_api/crud.py b/xas-standards-api/src/xas_standards_api/crud.py index b82596e..233ab77 100644 --- a/xas-standards-api/src/xas_standards_api/crud.py +++ b/xas-standards-api/src/xas_standards_api/crud.py @@ -49,7 +49,6 @@ def get_standard(session, id) -> XASStandard: return standard else: raise HTTPException(status_code=404, detail=f"No standard with id={id}") - def update_review(session, review, reviewer_id): diff --git a/xas-standards-api/src/xas_standards_api/schemas.py b/xas-standards-api/src/xas_standards_api/schemas.py index 3fd7397..d86d625 100644 --- a/xas-standards-api/src/xas_standards_api/schemas.py +++ b/xas-standards-api/src/xas_standards_api/schemas.py @@ -158,7 +158,12 @@ class XASStandard(XASStandardInput, table=True): element: Element = Relationship(sa_relationship_kwargs={"lazy": "joined"}) edge: Edge = Relationship(sa_relationship_kwargs={"lazy": "joined"}) beamline: Beamline = Relationship(sa_relationship_kwargs={"lazy": "selectin"}) - submitter: Person = Relationship(sa_relationship_kwargs={"lazy": "selectin", "foreign_keys": "[XASStandard.submitter_id]"}) + submitter: Person = Relationship( + sa_relationship_kwargs={ + "lazy": "selectin", + "foreign_keys": "[XASStandard.submitter_id]", + } + ) class XASStandardResponse(XASStandardInput): @@ -168,6 +173,7 @@ class XASStandardResponse(XASStandardInput): beamline: BeamlineResponse submitter_id: int + class AdminXASStandardResponse(XASStandardResponse): submitter: Person @@ -176,4 +182,4 @@ class XASStandardAdminReviewInput(SQLModel): reviewer_comments: Optional[str] = None review_status: ReviewStatus standard_id: int - #get fedid from person table + # get fedid from person table