Skip to content

Commit

Permalink
fix: org creation using Form params
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Feb 13, 2024
1 parent 941fdde commit c88355e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/backend/app/organisations/organisation_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
"""Logic for organisation management."""

from io import BytesIO
from typing import Optional

from fastapi import HTTPException, Response, UploadFile
from fastapi import File, HTTPException, Response, UploadFile
from loguru import logger as log
from sqlalchemy import update
from sqlalchemy.orm import Session
Expand Down Expand Up @@ -59,7 +60,7 @@ async def get_unapproved_organisations(


async def upload_logo_to_s3(
db_org: db_models.DbOrganisation, logo_file: UploadFile(None)
db_org: db_models.DbOrganisation, logo_file: UploadFile
) -> str:
"""Upload logo using standardised /{org_id}/logo.png format.
Expand Down Expand Up @@ -91,7 +92,7 @@ async def upload_logo_to_s3(


async def create_organisation(
db: Session, org_model: OrganisationIn, logo: UploadFile(None)
db: Session, org_model: OrganisationIn, logo: Optional[UploadFile] = File(None)
) -> db_models.DbOrganisation:
"""Creates a new organisation with the given name, description, url, type, and logo.
Expand Down
10 changes: 8 additions & 2 deletions src/backend/app/organisations/organisation_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#
"""Routes for organisation management."""

from typing import Optional

from fastapi import (
APIRouter,
Depends,
Expand Down Expand Up @@ -71,11 +73,15 @@ async def get_organisation_detail(
async def create_organisation(
# Depends required below to allow logo upload
org: organisation_schemas.OrganisationIn = Depends(),
logo: UploadFile = File(None),
logo: Optional[UploadFile] = File(None),
db: Session = Depends(database.get_db),
current_user: DbUser = Depends(login_required),
) -> organisation_schemas.OrganisationOut:
"""Create an organisation with the given details."""
"""Create an organisation with the given details.
TODO refactor to use base64 encoded logo / no upload file.
TODO then we can use the pydantic model as intended.
"""
return await organisation_crud.create_organisation(db, org, logo)


Expand Down
39 changes: 34 additions & 5 deletions src/backend/app/organisations/organisation_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from re import sub
from typing import Optional

from pydantic import BaseModel, computed_field
from fastapi import Form
from pydantic import BaseModel, Field, computed_field

from app.config import HttpUrlStr
from app.models.enums import CommunityType, OrganisationType
Expand All @@ -33,10 +34,16 @@
class OrganisationIn(ODKCentralIn):
"""Organisation to create from user input."""

name: str
description: Optional[str] = None
url: Optional[HttpUrlStr] = None
community_type: Optional[CommunityType] = None
name: str = Field(Form(..., description="Organisation name"))
description: Optional[str] = Field(
Form(None, description="Organisation description")
)
url: Optional[HttpUrlStr] = Field(
Form(None, description=("Organisation website URL"))
)
community_type: Optional[CommunityType] = Field(
Form(None, description=("Organisation community type"))
)

@computed_field
@property
Expand All @@ -50,6 +57,28 @@ def slug(self) -> str:
slug = sub(r"[-\s]+", "-", slug)
return slug

# TODO replace once computed logo complete below
odk_central_url: Optional[HttpUrlStr] = Field(
Form(None, description=("ODK Central URL"))
)
odk_central_user: Optional[str] = Field(
Form(None, description=("ODK Central User"))
)
odk_central_password: Optional[str] = Field(
Form(None, description=("ODK Central Password"))
)

# TODO decode base64 logo and upload in computed property
# @computed_field
# @property
# def logo(self) -> Optional[str]:
# """Decode and upload logo to S3, return URL."""
# if not self.logo_base64:
# return None
# logo_decoded = base64.b64decode(self.logo_base64)
# # upload to S3
# return url


class OrganisationEdit(OrganisationIn):
"""Organisation to edit via user input."""
Expand Down

0 comments on commit c88355e

Please sign in to comment.