Skip to content

Commit

Permalink
fix: add org_deps get_org_odk_creds method
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jan 31, 2024
1 parent a3e21c1 commit dd96fe3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/backend/app/organisations/organisation_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 44 additions & 13 deletions src/backend/app/organisations/organisation_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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


Expand All @@ -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:
Expand All @@ -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}")
Expand Down
6 changes: 3 additions & 3 deletions src/backend/app/tasks/tasks_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dd96fe3

Please sign in to comment.