From 9a9d72810bde203c0ffe630394c780260dbc1674 Mon Sep 17 00:00:00 2001 From: Sujan Adhikari <109404840+Sujanadh@users.noreply.github.com> Date: Tue, 23 Jan 2024 22:19:58 +0545 Subject: [PATCH] feat: paginated submissions per task (#1128) * merge development * feat: paginated submissions by task --------- Co-authored-by: sujanadh --- .../app/submissions/submission_crud.py | 29 ++++++++++ .../app/submissions/submission_routes.py | 55 ++++++++++++++----- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/src/backend/app/submissions/submission_crud.py b/src/backend/app/submissions/submission_crud.py index 9007f4cee4..327ca7729d 100644 --- a/src/backend/app/submissions/submission_crud.py +++ b/src/backend/app/submissions/submission_crud.py @@ -38,6 +38,7 @@ from app.central.central_crud import get_odk_form, get_odk_project, list_odk_xforms from app.config import settings +from app.db import db_models from app.projects import project_crud, project_schemas from app.s3 import add_obj_to_bucket, get_obj_from_bucket from app.tasks import tasks_crud @@ -801,3 +802,31 @@ async def get_submission_by_project(project_id: int, skip: 0, limit: 100, db: Se end_index = skip + limit paginated_content = content[start_index:end_index] return len(content), paginated_content + + +async def get_submission_by_task( + project: db_models.DbProject, task_id: int, filters: dict, db: Session +): + """Get submissions and count by task. + + Args: + project: The project instance. + task_id: The ID of the task. + filters: A dictionary of filters. + db: The database session. + + Returns: + Tuple: A tuple containing the list of submissions and the count. + """ + odk_credentials = project_schemas.ODKCentral( + odk_central_url=project.odk_central_url, + odk_central_user=project.odk_central_user, + odk_central_password=project.odk_central_password, + ) + + xform = get_odk_form(odk_credentials) + data = xform.listSubmissions(project.odkid, task_id, filters) + submissions = data.get("value", []) + count = data.get("@odata.count", 0) + + return submissions, count diff --git a/src/backend/app/submissions/submission_routes.py b/src/backend/app/submissions/submission_routes.py index 08c04536ec..31f1225e1e 100644 --- a/src/backend/app/submissions/submission_routes.py +++ b/src/backend/app/submissions/submission_routes.py @@ -31,7 +31,7 @@ from app.central import central_crud from app.config import settings from app.db import database -from app.projects import project_crud, project_schemas +from app.projects import project_crud, project_deps, project_schemas from app.submissions import submission_crud, submission_schemas from app.tasks import tasks_crud @@ -366,23 +366,13 @@ async def submission_table( results_per_page: int = Query(13, le=100), db: Session = Depends(database.get_db), ): - """This API returns the submission table of a project. + """This api returns the submission table of a project. - Args: - background_tasks (BackgroundTasks): The background tasks manager. - - project_id (int): The ID of the project. + It takes two parameter: project_id and task_id. - page (int, optional): The page number for pagination. Defaults to 1. - - results_per_page (int, optional): The number of results per page for pagination. - Defaults to 13. - - db (Session, optional): The database session. - - Returns: - PaginatedSubmissions: The paginated submission table of the project. + project_id: The ID of the project. + task_id: The ID of the task. """ skip = (page - 1) * results_per_page limit = results_per_page @@ -402,3 +392,38 @@ async def submission_table( pagination=submission_schemas.PaginationInfo(**pagination.dict()), ) return response + + +@router.get("/task_submissions/{project_id}") +async def task_submissions( + task_id: int, + project: project_deps.get_project_by_id = Depends(), + page: int = Query(1, ge=1), + limit: int = Query(13, le=100), + db: Session = Depends(database.get_db), +): + """This api returns the submission table of a project. + + It takes two parameter: project_id and task_id. + + project_id: The ID of the project. + + task_id: The ID of the task. + """ + skip = (page - 1) * limit + filters = { + "$top": limit, + "$skip": skip, + "$count": True, + "$wkt": True, + } + + data, count = await submission_crud.get_submission_by_task( + project, task_id, filters, db + ) + pagination = await project_crud.get_pagination(page, count, limit, count) + response = submission_schemas.PaginatedSubmissions( + results=data, + pagination=submission_schemas.PaginationInfo(**pagination.dict()), + ) + return response