Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default odk creds when organisation do not have their own during project creation #2070

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/backend/app/organisations/organisation_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)],
Expand Down
5 changes: 5 additions & 0 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down Expand Up @@ -166,7 +169,7 @@ const ProjectDetailsForm = ({ flag }) => {
}}
onMouseLeave={() => dispatch(CreateProjectActions.SetDescriptionToFocus(null))}
>
{hasODKCredentials && (
{
<CustomCheckbox
key="defaultODKCredentials"
label="Use default ODK credentials"
Expand All @@ -175,10 +178,10 @@ const ProjectDetailsForm = ({ flag }) => {
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 && (
<>
<InputTextField
id="odk_central_url"
Expand Down
Loading