diff --git a/src/backend/app/organisations/organisation_deps.py b/src/backend/app/organisations/organisation_deps.py index 6db119e02..6688c7e44 100644 --- a/src/backend/app/organisations/organisation_deps.py +++ b/src/backend/app/organisations/organisation_deps.py @@ -18,10 +18,12 @@ """Organisation dependencies for use in Depends.""" +import os from typing import Annotated from fastapi import Depends from fastapi.exceptions import HTTPException +from loguru import logger as log from psycopg import Connection from app.central import central_schemas @@ -79,10 +81,12 @@ async def get_org_odk_creds( 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", + log.info( + """Organisation does not have ODK Central credentials configured + using default hotosm credentials""", ) + default_creds = await get_default_odk_creds() + return central_schemas.ODKCentralDecrypted(**default_creds.dict()) return central_schemas.ODKCentralDecrypted( odk_central_url=url, @@ -91,6 +95,15 @@ async def get_org_odk_creds( ) +async def get_default_odk_creds(): + """Get default odk credentials.""" + return central_schemas.ODKCentralIn( + odk_central_url=os.getenv("ODK_CENTRAL_URL"), + odk_central_user=os.getenv("ODK_CENTRAL_USER"), + odk_central_password=os.getenv("ODK_CENTRAL_PASSWD"), + ) + + async def org_exists( org_id: int | str, db: Annotated[Connection, Depends(db_conn)], diff --git a/src/backend/app/projects/project_crud.py b/src/backend/app/projects/project_crud.py index 71d5e95db..f00d90c01 100644 --- a/src/backend/app/projects/project_crud.py +++ b/src/backend/app/projects/project_crud.py @@ -51,6 +51,7 @@ parse_geojson_file_to_featcol, split_geojson_by_task_areas, ) +from app.organisations.organisation_deps import get_default_odk_creds from app.projects import project_deps, project_schemas from app.s3 import add_file_to_bucket, add_obj_to_bucket @@ -557,6 +558,10 @@ async def generate_project_files( project_odk_form_id = project.odk_form_id project_odk_creds = project.odk_credentials + if not project_odk_creds: + # get default credentials + project_odk_creds = await get_default_odk_creds() + odk_token = await generate_odk_central_project_content( project_odk_id, project_odk_form_id, diff --git a/src/frontend/src/components/createnewproject/ProjectDetailsForm.tsx b/src/frontend/src/components/createnewproject/ProjectDetailsForm.tsx index 0fe74239f..7a255f774 100644 --- a/src/frontend/src/components/createnewproject/ProjectDetailsForm.tsx +++ b/src/frontend/src/components/createnewproject/ProjectDetailsForm.tsx @@ -75,6 +75,9 @@ const ProjectDetailsForm = ({ flag }) => { if (selectedOrg && selectedOrg.hasODKCredentials) { handleCustomChange('defaultODKCredentials', selectedOrg.hasODKCredentials); + } else { + // Allow the user to choose default credentials for orgs without ODK credentials + handleCustomChange('defaultODKCredentials', false); } }; @@ -166,7 +169,7 @@ const ProjectDetailsForm = ({ flag }) => { }} onMouseLeave={() => dispatch(CreateProjectActions.SetDescriptionToFocus(null))} > - {hasODKCredentials && ( + { { handleCustomChange('defaultODKCredentials', !values.defaultODKCredentials); }} className="fmtm-text-black" - labelClickable={true} + labelClickable={hasODKCredentials} // Dynamically set labelClickable based on hasODKCredentials /> - )} - {((!values.defaultODKCredentials && hasODKCredentials) || !hasODKCredentials) && ( + } + {!values.defaultODKCredentials && !hasODKCredentials && ( <>