From e4feac25c952b803ac0626d1be64095dc460def6 Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Thu, 14 Sep 2023 14:58:00 +0545 Subject: [PATCH] user wise submission count api --- src/backend/app/submission/submission_crud.py | 30 ++++++++++++++++++- .../app/submission/submission_routes.py | 9 +++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/backend/app/submission/submission_crud.py b/src/backend/app/submission/submission_crud.py index baebf17927..5b7fadca46 100644 --- a/src/backend/app/submission/submission_crud.py +++ b/src/backend/app/submission/submission_crud.py @@ -37,6 +37,8 @@ from ..projects import project_crud, project_schemas from osm_fieldwork.json2osm import JsonDump from pathlib import Path +from ..central import central_crud +from collections import defaultdict def get_submission_of_project(db: Session, project_id: int, task_id: int = None): @@ -707,4 +709,30 @@ async def get_submission_count_of_a_project(db:Session, if json_data_value: files.extend(json_data_value) - return len(files) \ No newline at end of file + return len(files) + + +async def get_odk_user_info(db: Session, project_id): + project = project_crud.get_project(db, project_id) + odkid = project.odkid + + # Get odk credentials from project. + odk_credentials = { + "odk_central_url": project.odk_central_url, + "odk_central_user": project.odk_central_user, + "odk_central_password": project.odk_central_password, + } + + odk_credentials = project_schemas.ODKCentral(**odk_credentials) + + odk_project = central_crud.get_odk_project(odk_credentials) + filters = { + "$select":"username" + } + result = odk_project.getAllSubmissions(odkid, None, filters) + + username_counts = defaultdict(int) + for item in result: + username = item["username"] + username_counts[username] += 1 + return username_counts diff --git a/src/backend/app/submission/submission_routes.py b/src/backend/app/submission/submission_routes.py index c6ba45b164..36d5d8fb24 100644 --- a/src/backend/app/submission/submission_routes.py +++ b/src/backend/app/submission/submission_routes.py @@ -241,4 +241,11 @@ async def get_osm_xml( # Create a plain XML response response = Response(content=processed_xml_string, media_type="application/xml") - return response \ No newline at end of file + return response + + +@router.get("/user_info/{project_id}") +async def get_user_info(project_id: int, + db: Session = Depends(database.get_db) + ): + return await submission_crud.get_odk_user_info(db, project_id) \ No newline at end of file