From 75e3e2588ed30cd0206e9544a4992d980234e68a Mon Sep 17 00:00:00 2001 From: Sujan Adhikari Date: Thu, 14 Nov 2024 12:26:03 +0545 Subject: [PATCH] fix: update organisation using same parser function --- .../app/organisations/organisation_routes.py | 9 ++++-- .../app/organisations/organisation_schemas.py | 32 ++++++++++--------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/backend/app/organisations/organisation_routes.py b/src/backend/app/organisations/organisation_routes.py index f7b8560d5a..748cdbd5dc 100644 --- a/src/backend/app/organisations/organisation_routes.py +++ b/src/backend/app/organisations/organisation_routes.py @@ -101,6 +101,11 @@ async def create_organisation( Either a logo can be uploaded, or a link to the logo provided in the Organisation JSON ('logo': 'https://your.link.to.logo.png'). """ + if org_in.name is None: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, + detail="The `name` is required to create an organisation.", + ) return await DbOrganisation.create(db, org_in, current_user.id, logo) @@ -108,12 +113,12 @@ async def create_organisation( async def update_organisation( db: Annotated[Connection, Depends(db_conn)], org_user_dict: Annotated[AuthUser, Depends(org_admin)], - new_values: OrganisationUpdate = Depends(), + new_values: OrganisationUpdate = Depends(parse_organisation_input), logo: UploadFile = File(None), ): """Partial update for an existing organisation.""" org_id = org_user_dict.get("org").id - return DbOrganisation.update(db, org_id, new_values, logo) + return await DbOrganisation.update(db, org_id, new_values, logo) @router.delete("/{org_id}") diff --git a/src/backend/app/organisations/organisation_schemas.py b/src/backend/app/organisations/organisation_schemas.py index 0f9d2f980e..cc8f86581d 100644 --- a/src/backend/app/organisations/organisation_schemas.py +++ b/src/backend/app/organisations/organisation_schemas.py @@ -23,10 +23,11 @@ from pydantic import BaseModel, Field from pydantic.functional_validators import model_validator -from app.central.central_schemas import ODKCentralIn -from app.db.enums import OrganisationType, CommunityType +from app.central.central_schemas import ODKCentralIn +from app.db.enums import CommunityType, OrganisationType from app.db.models import DbOrganisation, slugify + class OrganisationInBase(ODKCentralIn, DbOrganisation): """Base model for project insert / update (validators). @@ -51,18 +52,25 @@ class OrganisationIn(OrganisationInBase): # Name is mandatory name: str + +class OrganisationUpdate(OrganisationInBase): + """Edit an org from user input.""" + + # Allow the name field to be omitted / not updated + name: Optional[str] = None + + def parse_organisation_input( - name: str = Form(...), + name: Optional[str] = Form(None), slug: Optional[str] = Form(None), created_by: Optional[int] = Form(None), community_type: CommunityType = Form(None), type: OrganisationType = Form(None, alias="type"), odk_central_url: Optional[str] = Form(None), odk_central_user: Optional[str] = Form(None), - odk_central_password: Optional[str] = Form(None) -) -> OrganisationIn: - """ - Parse organisation input data from a FastAPI Form. + odk_central_password: Optional[str] = Form(None), +) -> OrganisationUpdate: + """Parse organisation input data from a FastAPI Form. The organisation fields are passed as keyword arguments. The ODKCentralIn model is used to parse the ODK credential fields, and @@ -76,22 +84,16 @@ def parse_organisation_input( odk_central_user=odk_central_user, odk_central_password=odk_central_password, ) - org_data = OrganisationIn( + org_data = OrganisationUpdate( name=name, slug=slug, created_by=created_by, community_type=community_type, type=type, - **odk_central_data.dict(exclude_unset=True) + **odk_central_data.dict(exclude_unset=True), ) return org_data -class OrganisationUpdate(OrganisationInBase): - """Edit an org from user input.""" - - # Allow the name field to be omitted / not updated - name: Optional[str] = None - class OrganisationOut(BaseModel): """Organisation to display to user."""