Skip to content

Commit

Permalink
fix: update logic getting default org odk creds
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Feb 26, 2024
1 parent 2f0291d commit ac72182
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ def generate_project_files(
)

odk_sync = async_to_sync(project_deps.get_odk_credentials)
odk_credentials = odk_sync(db, project)
odk_credentials = odk_sync(db, project_id)

if custom_form:
log.debug("User provided custom XLSForm")
Expand Down Expand Up @@ -1672,7 +1672,7 @@ async def update_project_form(
odk_id = project.odkid

# ODK Credentials
odk_credentials = await project_deps.get_odk_credentials(db, project)
odk_credentials = await project_deps.get_odk_credentials(db, project_id)

if form:
xlsform = f"/tmp/custom_form.{form_type}"
Expand Down
49 changes: 35 additions & 14 deletions src/backend/app/projects/project_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

"""Project dependencies for use in Depends."""

from typing import Any, Optional, Union
from typing import Optional

from fastapi import Depends
from fastapi.exceptions import HTTPException
Expand All @@ -27,7 +27,7 @@
from app.db.database import get_db
from app.db.db_models import DbProject
from app.models.enums import HTTPStatus
from app.projects import project_crud, project_schemas
from app.projects import project_schemas


async def get_project_by_id(
Expand All @@ -48,17 +48,38 @@ async def get_project_by_id(
return db_project


async def get_odk_credentials(db: Session, project: Union[int, Any]):
"""Get odk credentials of project."""
if isinstance(project, int):
db_project = await project_crud.get_project(db, project)
else:
db_project = project
async def get_odk_credentials(db: Session, project_id: int):
"""Get ODK credentials of a project, or default organization credentials."""
sql = """
SELECT
COALESCE(
NULLIF(projects.odk_central_url, ''),
organisations.odk_central_url)
AS odk_central_url,
COALESCE(
NULLIF(projects.odk_central_user, ''),
organisations.odk_central_user)
AS odk_central_user,
COALESCE(
NULLIF(projects.odk_central_password, ''),
organisations.odk_central_password
) AS odk_central_password
FROM
projects
LEFT JOIN
organisations ON projects.organisation_id = organisations.id
WHERE
projects.id = :project_id
"""
result = db.execute(sql, {"project_id": project_id})
creds = result.first()

odk_credentials = {
"odk_central_url": db_project.odk_central_url,
"odk_central_user": db_project.odk_central_user,
"odk_central_password": db_project.odk_central_password,
}
url = creds.odk_central_url
user = creds.odk_central_user
password = creds.odk_central_password

return project_schemas.ODKCentralDecrypted(**odk_credentials)
return project_schemas.ODKCentralDecrypted(
odk_central_url=url,
odk_central_user=user,
odk_central_password=password,
)
5 changes: 3 additions & 2 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ async def create_project(
)

# Must decrypt ODK password & connect to ODK Central before proj created
# cannot use get_odk_credentials helper as no project id yet
if project_info.odk_central_url:
odk_creds_decrypted = project_schemas.ODKCentralDecrypted(
odk_central_url=project_info.odk_central_url,
Expand Down Expand Up @@ -822,7 +823,7 @@ async def get_or_set_data_extract(
url: Optional[str] = None,
project_id: int = Query(..., description="Project ID"),
db: Session = Depends(database.get_db),
project_user_dict: db_models.DbUser = Depends(project_admin),
org_user_dict: db_models.DbUser = Depends(project_admin),
):
"""Get or set the data extract URL for a project."""
fgb_url = await project_crud.get_or_set_data_extract_url(
Expand All @@ -839,7 +840,7 @@ async def upload_custom_extract(
custom_extract_file: UploadFile = File(...),
project_id: int = Query(..., description="Project ID"),
db: Session = Depends(database.get_db),
project_user_dict: db_models.DbUser = Depends(project_admin),
org_user_dict: db_models.DbUser = Depends(project_admin),
):
"""Upload a custom data extract geojson for a project.
Expand Down
18 changes: 9 additions & 9 deletions src/backend/app/submissions/submission_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_submission_of_project(db: Session, project_id: int, task_id: int = None)

# ODK Credentials
odk_sync = async_to_sync(project_deps.get_odk_credentials)
odk_credentials = odk_sync(db, project_info)
odk_credentials = odk_sync(db, project_id)
xform = get_odk_form(odk_credentials)

# If task id is not provided, submission for all the task are listed
Expand Down Expand Up @@ -146,7 +146,7 @@ def convert_to_osm(db: Session, project_id: int, task_id: int):

# ODK Credentials
odk_sync = async_to_sync(project_deps.get_odk_credentials)
odk_credentials = odk_sync(db, project_info)
odk_credentials = odk_sync(db, project_id)
# Get ODK Form with odk credentials from the project.
xform = get_odk_form(odk_credentials)

Expand Down Expand Up @@ -221,7 +221,7 @@ async def gather_all_submission_csvs(db, project_id):

# ODK Credentials
odk_sync = async_to_sync(project_deps.get_odk_credentials)
odk_credentials = odk_sync(db, project_info)
odk_credentials = odk_sync(db, project_id)
# Get ODK Form with odk credentials from the project.
xform = get_odk_form(odk_credentials)

Expand Down Expand Up @@ -312,7 +312,7 @@ def update_submission_in_s3(

# Gather metadata
odk_sync = async_to_sync(project_deps.get_odk_credentials)
odk_credentials = odk_sync(db, project)
odk_credentials = odk_sync(db, project_id)
odk_forms = list_odk_xforms(project.odkid, odk_credentials, True)

# Get latest submission date
Expand Down Expand Up @@ -406,7 +406,7 @@ def get_all_submissions_json(db: Session, project_id):

# ODK Credentials
odk_sync = async_to_sync(project_deps.get_odk_credentials)
odk_credentials = odk_sync(db, project_info)
odk_credentials = odk_sync(db, project_id)
project = get_odk_project(odk_credentials)

get_task_id_list_sync = async_to_sync(tasks_crud.get_task_id_list)
Expand Down Expand Up @@ -469,7 +469,7 @@ async def download_submission(
project_tasks = project_info.tasks

# ODK Credentials
odk_credentials = await project_deps.get_odk_credentials(db, project_info)
odk_credentials = await project_deps.get_odk_credentials(db, project_id)
# Get ODK Form with odk credentials from the project.
xform = get_odk_form(odk_credentials)
if not export_json:
Expand Down Expand Up @@ -574,7 +574,7 @@ async def get_submission_points(db: Session, project_id: int, task_id: int = Non
form_category = project_info.xform_title

# ODK Credentials
odk_credentials = await project_deps.get_odk_credentials(db, project_info)
odk_credentials = await project_deps.get_odk_credentials(db, project_id)
xform = get_odk_form(odk_credentials)

if task_id:
Expand Down Expand Up @@ -630,7 +630,7 @@ async def get_submission_count_of_a_project(db: Session, project_id: int):
project_tasks = project_info.tasks

# ODK Credentials
odk_credentials = await project_deps.get_odk_credentials(db, project_info)
odk_credentials = await project_deps.get_odk_credentials(db, project_id)
# Get ODK Form with odk credentials from the project.
xform = get_odk_form(odk_credentials)

Expand Down Expand Up @@ -799,7 +799,7 @@ async def get_submission_by_task(
Returns:
Tuple: A tuple containing the list of submissions and the count.
"""
odk_credentials = await project_deps.get_odk_credentials(db, project)
odk_credentials = await project_deps.get_odk_credentials(db, project.id)

xform = get_odk_form(odk_credentials)
data = xform.listSubmissions(project.odkid, str(task_id), filters)
Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/submissions/submission_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ async def get_submission_form_fields(
"""
project = await project_crud.get_project(db, project_id)
task_list = await tasks_crud.get_task_id_list(db, project_id)
odk_credentials = await project_deps.get_odk_credentials(db, project)
odk_credentials = await project_deps.get_odk_credentials(db, project_id)
odk_form = central_crud.get_odk_form(odk_credentials)
response = odk_form.form_fields(project.odkid, str(task_list[0]))
return response
Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/tasks/tasks_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ async def task_features_count(
project = await project_crud.get_project(db, project_id)

# ODK Credentials
odk_credentials = await project_deps.get_odk_credentials(db, project)
odk_credentials = await project_deps.get_odk_credentials(db, project_id)

odk_details = central_crud.list_odk_xforms(project.odkid, odk_credentials, True)

Expand Down

0 comments on commit ac72182

Please sign in to comment.