From 12943a4f3143cc0064dc90d780298b9deac85309 Mon Sep 17 00:00:00 2001 From: Sujan Adhikari Date: Tue, 12 Nov 2024 15:14:49 +0545 Subject: [PATCH 1/3] fix: update project form --- src/backend/app/projects/project_routes.py | 31 +++++++++++++++----- src/frontend/src/api/CreateProjectService.ts | 5 ++-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index eb2dd40f88..aa9ddcbf63 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -524,7 +524,7 @@ async def generate_files( xlsform_upload: Annotated[ Optional[BytesIO], Depends(central_deps.read_optional_xlsform) ], - additional_entities: list[str] = None, + additional_entities: Optional[list[str]] = None, ): """Generate additional content to initialise the project. @@ -849,12 +849,13 @@ async def update_project_form( project_user_dict: Annotated[ProjectUserDict, Depends(project_manager)], xform_id: str = Form(...), category: XLSFormType = Form(...), -) -> DbProject: +): """Update the XForm data in ODK Central. Also updates the category and custom XLSForm data in the database. """ project = project_user_dict["project"] + user = project_user_dict["user"] # TODO we currently do nothing with the provided category # TODO allowing for category updates is disabled due to complexity @@ -876,16 +877,30 @@ async def update_project_form( ) sql = """ - INSERT INTO projects - (xlsform_content) - VALUES - (%(xls_data)s) + UPDATE projects + SET + xlsform_content = %(xls_data)s, + author_id = %(author_id)s, + short_description = %(text)s + WHERE + id = %(project_id)s RETURNING id, hashtags; """ async with db.cursor() as cur: - await cur.execute(sql, {"xls_data": xlsform.getvalue()}) + await cur.execute( + sql, + { + "xls_data": xlsform.getvalue(), + "author_id": user.id, + "project_id": project.id, + }, + ) + db.commit() - return project + return JSONResponse( + status_code=HTTPStatus.OK, + content={"message": f"Successfully updated the form for project {project.id}"}, + ) @router.get("/{project_id}/download") diff --git a/src/frontend/src/api/CreateProjectService.ts b/src/frontend/src/api/CreateProjectService.ts index 0ad75228c8..4babab7170 100755 --- a/src/frontend/src/api/CreateProjectService.ts +++ b/src/frontend/src/api/CreateProjectService.ts @@ -90,6 +90,7 @@ const CreateProjectService = ( } // Generate project files + console.log('additional entity', [additionalFeature?.name?.split('.')?.[0]]); const generateProjectFile = await dispatch( GenerateProjectFilesService( `${import.meta.env.VITE_API_URL}/projects/${projectId}/generate-project-data`, @@ -448,14 +449,14 @@ const PostFormUpdate = (url: string, projectData: Record) => { formFormData.append('xlsform', projectData.upload); const postFormUpdateResponse = await axios.post(url, formFormData); - const resp: ProjectDetailsModel = postFormUpdateResponse.data; + const resp: { message: string } = postFormUpdateResponse.data; // dispatch(CreateProjectActions.SetIndividualProjectDetails(modifiedResponse)); // dispatch(CreateProjectActions.SetPostFormUpdate(resp)); dispatch(CreateProjectActions.SetPostFormUpdateLoading(false)); dispatch( CommonActions.SetSnackBar({ open: true, - message: 'Form Successfully Updated', + message: resp.message, variant: 'success', duration: 2000, }), From 83060d512a22a091903ef2170f830d6b47883990 Mon Sep 17 00:00:00 2001 From: Sujan Adhikari Date: Wed, 13 Nov 2024 09:59:54 +0545 Subject: [PATCH 2/3] remove console log --- src/frontend/src/api/CreateProjectService.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/frontend/src/api/CreateProjectService.ts b/src/frontend/src/api/CreateProjectService.ts index 4babab7170..bf26e0ee65 100755 --- a/src/frontend/src/api/CreateProjectService.ts +++ b/src/frontend/src/api/CreateProjectService.ts @@ -90,7 +90,6 @@ const CreateProjectService = ( } // Generate project files - console.log('additional entity', [additionalFeature?.name?.split('.')?.[0]]); const generateProjectFile = await dispatch( GenerateProjectFilesService( `${import.meta.env.VITE_API_URL}/projects/${projectId}/generate-project-data`, From 5cc44ae1eb21253adcf689d4e872b4e1a61974ec Mon Sep 17 00:00:00 2001 From: Sujan Adhikari Date: Wed, 13 Nov 2024 10:26:56 +0545 Subject: [PATCH 3/3] fix: refactored sql to update project form --- src/backend/app/projects/project_routes.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index aa9ddcbf63..72b6912058 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -855,7 +855,6 @@ async def update_project_form( Also updates the category and custom XLSForm data in the database. """ project = project_user_dict["project"] - user = project_user_dict["user"] # TODO we currently do nothing with the provided category # TODO allowing for category updates is disabled due to complexity @@ -879,9 +878,7 @@ async def update_project_form( sql = """ UPDATE projects SET - xlsform_content = %(xls_data)s, - author_id = %(author_id)s, - short_description = %(text)s + xlsform_content = %(xls_data)s WHERE id = %(project_id)s RETURNING id, hashtags; @@ -891,7 +888,6 @@ async def update_project_form( sql, { "xls_data": xlsform.getvalue(), - "author_id": user.id, "project_id": project.id, }, )