Skip to content

Commit

Permalink
refactor: remove broken submission code no longer used
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed May 21, 2024
1 parent 340d04e commit 68c50d1
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 213 deletions.
15 changes: 0 additions & 15 deletions src/backend/app/central/central_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,21 +358,6 @@ async def update_project_xform(
xform_obj.publishForm(odk_id, form_name)


def download_submissions(
project_id: int,
xform_id: str,
submission_id: Optional[str] = None,
get_json: bool = True,
odk_central: Optional[project_schemas.ODKCentralDecrypted] = None,
):
"""Download all submissions for an XForm."""
xform = get_odk_form(odk_central)
# FIXME: should probably filter by timestamps or status value
data = xform.getSubmissions(project_id, xform_id, submission_id, True, get_json)
fixed = str(data, "utf-8")
return fixed.splitlines()


async def read_and_test_xform(
input_data: BytesIO,
form_file_ext: str,
Expand Down
157 changes: 1 addition & 156 deletions src/backend/app/central/central_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,14 @@
#
"""Routes to relay requests to ODK Central server."""

import json

from fastapi import APIRouter, Depends, HTTPException
from fastapi import APIRouter, Depends
from fastapi.concurrency import run_in_threadpool
from fastapi.responses import JSONResponse
from loguru import logger as log
from sqlalchemy import (
column,
select,
table,
)
from sqlalchemy.orm import Session
from sqlalchemy.sql import text

from app.central import central_crud
from app.db import database
from app.projects import project_schemas

router = APIRouter(
prefix="/central",
Expand Down Expand Up @@ -64,149 +55,3 @@ async def get_form_lists(
"""
forms = await central_crud.get_form_list(db)
return forms


@router.get("/list-submissions")
async def list_submissions(
project_id: int,
xml_form_id: str = None,
db: Session = Depends(database.get_db),
) -> list[dict]:
"""Get all submissions JSONs for a project."""
try:
project = table(
"projects",
column("project_name_prefix"),
column("xform_title"),
column("id"),
column("odkid"),
)
where = f"id={project_id}"
sql = select(project).where(text(where))
result = db.execute(sql)
first = result.first()
if not first:
return {"error": "No such project!"}

submissions = list()

if not xml_form_id:
xforms = central_crud.list_odk_xforms(first.odkid)

for xform in xforms:
try:
data = central_crud.download_submissions(
first.odkid, xform["xml_form_id"], None, False
)
except Exception:
continue
if len(submissions) == 0:
submissions.append(json.loads(data[0]))
if len(data) >= 2:
for entry in range(1, len(data)):
submissions.append(json.loads(data[entry]))
else:
data = central_crud.download_submissions(first.odkid, xml_form_id)
if len(submissions) == 0:
submissions.append(json.loads(data[0]))
if len(data) >= 2:
for entry in range(1, len(data)):
submissions.append(json.loads(data[entry]))

return submissions
except Exception as e:
log.error(e)
raise HTTPException(status_code=500, detail=str(e)) from e


@router.get("/submission")
async def get_submission(
project_id: int,
xml_form_id: str = None,
submission_id: str = None,
db: Session = Depends(database.get_db),
) -> dict:
"""Return the submission JSON for a single XForm.
Parameters:
project_id (int): the id of the project in the database.
xml_form_id (str): the xml_form_id of the form in Central.
submission_id (str): the submission id of the submission in Central.
If the submission_id is provided, an individual submission is returned.
Returns:
dict: Submission JSON.
"""
try:
"""Download the submissions data from Central."""
project = table(
"projects",
column("project_name_prefix"),
column("xform_title"),
column("id"),
column("odkid"),
column("odk_central_url"),
column("odk_central_user"),
column("odk_central_password"),
)
where = f"id={project_id}"
sql = select(project).where(text(where))
result = db.execute(sql)
first = result.first()
if not first:
return {"error": "No such project!"}

# ODK Credentials
odk_credentials = project_schemas.ODKCentralDecrypted(
odk_central_url=first.odk_central_url,
odk_central_user=first.odk_central_user,
odk_central_password=first.odk_central_password,
)

submissions = []

if xml_form_id and submission_id:
data = central_crud.download_submissions(
first.odkid, xml_form_id, submission_id, True, odk_credentials
)
if submissions != 0:
submissions.append(json.loads(data[0]))
if len(data) >= 2:
for entry in range(1, len(data)):
submissions.append(json.loads(data[entry]))

else:
if not xml_form_id:
xforms = central_crud.list_odk_xforms(first.odkid, odk_credentials)
for xform in xforms:
try:
data = central_crud.download_submissions(
first.odkid,
xform["xmlFormId"],
None,
True,
odk_credentials,
)
except Exception:
continue
# if len(submissions) == 0:
submissions.append(json.loads(data[0]))
if len(data) >= 2:
for entry in range(1, len(data)):
submissions.append(json.loads(data[entry]))
else:
data = central_crud.download_submissions(
first.odkid, xml_form_id, None, True, odk_credentials
)
submissions.append(json.loads(data[0]))
if len(data) >= 2:
for entry in range(1, len(data)):
submissions.append(json.loads(data[entry]))
if len(submissions) == 1:
return submissions[0]

return submissions
except Exception as e:
log.error(e)
raise HTTPException(status_code=500, detail=str(e)) from e
42 changes: 0 additions & 42 deletions src/backend/app/submissions/submission_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,48 +314,6 @@ def get_all_submissions_json(db: Session, project_id):
return submissions


# TODO delete me
# def get_project_submission(db: Session, project_id: int):
# """Get."""
# get_project_sync = async_to_sync(project_crud.get_project)
# project_info = get_project_sync(db, project_id)

# # Return empty list if project is not found
# if not project_info:
# raise HTTPException(status_code=404, detail="Project not found")

# odkid = project_info.odkid
# project_name = project_info.project_name_prefix
# form_category = project_info.xform_title
# project_tasks = project_info.tasks

# # ODK Credentials
# odk_credentials = project_schemas.ODKCentralDecrypted(
# odk_central_url=project_info.odk_central_url,
# odk_central_user=project_info.odk_central_user,
# odk_central_password=project_info.odk_central_password,
# )

# # Get ODK Form with odk credentials from the project.
# xform = get_odk_form(odk_credentials)

# submissions = []

# task_list = [x.id for x in project_tasks]
# for id in task_list:
# xml_form_id = f"{project_name}_{form_category}_{id}"
# file = xform.getSubmissions(odkid, xml_form_id, None, False, True)
# if not file:
# json_data = None
# else:
# json_data = json.loads(file)
# json_data_value = json_data.get("value")
# if json_data_value:
# submissions.extend(json_data_value)

# return submissions


async def download_submission_in_json(db: Session, project_id: int):
"""Download submission data from ODK Central."""
project = await project_crud.get_project(db, project_id)
Expand Down

0 comments on commit 68c50d1

Please sign in to comment.