diff --git a/src/backend/app/organisations/organisation_crud.py b/src/backend/app/organisations/organisation_crud.py index 36e6ca649d..de3371780a 100644 --- a/src/backend/app/organisations/organisation_crud.py +++ b/src/backend/app/organisations/organisation_crud.py @@ -98,7 +98,7 @@ async def create_organisation( if await get_organisation_by_name(db, org_name=org_model.name): raise HTTPException( status_code=HTTPStatus.CONFLICT, - detail=f"Organisation already exists with the name {org_model.name}", + detail=f"Organisation already exists with the name ({org_model.name})", ) # Required to check if exists on error diff --git a/src/backend/app/organisations/organisation_deps.py b/src/backend/app/organisations/organisation_deps.py index 36c147302d..f2d18a1fde 100644 --- a/src/backend/app/organisations/organisation_deps.py +++ b/src/backend/app/organisations/organisation_deps.py @@ -23,13 +23,12 @@ from fastapi import Depends from fastapi.exceptions import HTTPException from loguru import logger as log -from sqlalchemy import func from sqlalchemy.orm import Session from app.db.database import get_db from app.db.db_models import DbOrganisation, DbProject from app.models.enums import HTTPStatus -from app.projects import project_deps +from app.projects import project_deps, project_schemas async def get_organisation_by_name( @@ -45,16 +44,20 @@ async def get_organisation_by_name( Returns: DbOrganisation: organisation with the given id """ - org_obj = ( - db.query(DbOrganisation) - .filter(func.lower(DbOrganisation.name).like(func.lower(f"%{org_name}%"))) - .first() - ) + # # For getting org with LIKE match + # org_obj = ( + # db.query(DbOrganisation) + # .filter(func.lower(DbOrganisation.name).like(func.lower(f"%{org_name}%"))) + # .first() + # ) + org_obj = db.query(DbOrganisation).filter_by(name=org_name).first() + if org_obj and check_approved and org_obj.approved is False: raise HTTPException( - status_code=HTTPStatus.NOT_FOUND, + status_code=HTTPStatus.FORBIDDEN, detail=f"Organisation ({org_obj.id}) is not approved yet", ) + return org_obj @@ -72,23 +75,51 @@ async def get_organisation_by_id( DbOrganisation: organisation with the given id """ org_obj = db.query(DbOrganisation).filter_by(id=org_id).first() + if org_obj and check_approved and org_obj.approved is False: raise HTTPException( - status_code=HTTPStatus.NOT_FOUND, - detail=f"Organisation {org_id} is not approved yet", + status_code=HTTPStatus.FORBIDDEN, + detail=f"Organisation ({org_id}) is not approved yet", ) return org_obj +async def get_org_odk_creds( + org: DbOrganisation, +) -> project_schemas.ODKCentralDecrypted: + """Get odk credentials for an organisation, else error.""" + url = org.odk_central_url + user = org.odk_central_user + password = org.odk_central_password + + if not all([url, user, password]): + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, + detail="Organisation does not have ODK Central credentials configured", + ) + + return project_schemas.ODKCentralDecrypted( + odk_central_url=org.odk_central_url, + odk_central_user=org.odk_central_user, + odk_central_password=org.odk_central_password, + ) + + async def check_org_exists( db: Session, - org_id: Union[str, int], + org_id: Union[str, int, None], check_approved: bool = True, ) -> DbOrganisation: """Check if organisation name exists, else error. The org_id can also be an org name. """ + if not org_id: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, + detail="Organisation id not provided", + ) + try: org_id = int(org_id) except ValueError: @@ -98,14 +129,14 @@ async def check_org_exists( log.debug(f"Getting organisation by id: {org_id}") db_organisation = await get_organisation_by_id(db, org_id, check_approved) - if isinstance(org_id, str): + else: # is string log.debug(f"Getting organisation by name: {org_id}") db_organisation = await get_organisation_by_name(db, org_id, check_approved) if not db_organisation: raise HTTPException( status_code=HTTPStatus.NOT_FOUND, - detail=f"Organisation {org_id} does not exist", + detail=f"Organisation ({org_id}) does not exist", ) log.debug(f"Organisation match: {db_organisation}") diff --git a/src/backend/app/tasks/tasks_schemas.py b/src/backend/app/tasks/tasks_schemas.py index 3389c01bb0..ace91f1857 100644 --- a/src/backend/app/tasks/tasks_schemas.py +++ b/src/backend/app/tasks/tasks_schemas.py @@ -20,7 +20,7 @@ from datetime import datetime from typing import Any, List, Optional -from geojson_pydantic import Feature +from geojson_pydantic import Feature as GeojsonFeature from loguru import logger as log from pydantic import BaseModel, ConfigDict, Field, ValidationInfo from pydantic.functional_serializers import field_serializer @@ -71,8 +71,8 @@ class Task(BaseModel): project_id: int project_task_index: int project_task_name: str - outline_geojson: Optional[Feature] = None - outline_centroid: Optional[Feature] = None + outline_geojson: Optional[GeojsonFeature] = None + outline_centroid: Optional[GeojsonFeature] = None initial_feature_count: Optional[int] = None task_status: TaskStatus locked_by_uid: Optional[int] = None