Skip to content

Commit

Permalink
feat: paginated submissions per task (#1128)
Browse files Browse the repository at this point in the history
* merge development

* feat: paginated submissions by task

---------

Co-authored-by: sujanadh <[email protected]>
  • Loading branch information
Sujanadh and sujanadh authored Jan 23, 2024
1 parent 3060f19 commit 9a9d728
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
29 changes: 29 additions & 0 deletions src/backend/app/submissions/submission_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
55 changes: 40 additions & 15 deletions src/backend/app/submissions/submission_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit 9a9d728

Please sign in to comment.