Skip to content

Commit

Permalink
fix: update organisation using same parser function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sujanadh committed Nov 14, 2024
1 parent 5876566 commit 75e3e25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
9 changes: 7 additions & 2 deletions src/backend/app/organisations/organisation_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,24 @@ 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)


@router.patch("/{org_id}/", response_model=OrganisationOut)
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}")
Expand Down
32 changes: 17 additions & 15 deletions src/backend/app/organisations/organisation_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
Expand All @@ -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."""
Expand Down

0 comments on commit 75e3e25

Please sign in to comment.