From 4105bc46b182f903bc2bb7cf23b6037d5ee974ea Mon Sep 17 00:00:00 2001 From: Angelina Uno-Antonison Date: Thu, 9 Nov 2023 09:39:06 -0600 Subject: [PATCH] wip --- backend/src/routers/analysis_router.py | 71 +++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/backend/src/routers/analysis_router.py b/backend/src/routers/analysis_router.py index 76646a36..6d18ab4e 100644 --- a/backend/src/routers/analysis_router.py +++ b/backend/src/routers/analysis_router.py @@ -107,25 +107,72 @@ def update_event( raise HTTPException(status_code=409, detail=str(exception)) from exception @router.post("/{analysis_name}/sections", response_model=Analysis) -def update_analysis_sections( +def add_analysis_section_content( analysis_name: str, type: SectionRowType, updated_sections: List[Section], + upload_file: UploadFile = File(...), repositories=Depends(database), authorized=Security(get_authorization, scopes=["write"]) #pylint: disable=unused-argument ): """Updates the sections that have changes""" print(type) - print("The section type that is being saved in the update sections endpoint") - for section in updated_sections: - for field in section.content: - field_name, field_value= field["fieldName"], field["value"] - if "Nominator" == field_name: - repositories["analysis"].update_analysis_nominator(analysis_name, '; '.join(field_value)) - repositories["analysis"].update_analysis_section(analysis_name, section.header, field_name, {"value": field_value}) + if(type == SectionRowType.TEXT): + update_analysis_sections_text_fields(analysis_name, updated_sections, repositories["analysis"]) + elif( type == SectionRowType.IMAGE or type == SectionRowType.DOCUMENT): + try: + new_object_id = add_file_to_bucket_repository(upload_file, repositories["bucket"]) + except Exception as exception: + raise HTTPException(status_code=500, detail=str(exception)) from exception + + # adding supportin evidence file + # field_value_file = { + # "name": upload_file.filename, "attachment_id": str(new_file_object_id), "type": "file", "comments": comments + # } + # + # return repositories['analysis'].attach_section_supporting_evidence_file( + # analysis_name, section_name, field_name, field_value_file + # ) + + else: + print(type) + print("ADDING TYPE NOT SUPPORTED YET, IN PROGRESS") return repositories["analysis"].find_by_name(analysis_name) +@router.put("/{analysis_name}/sections", response_model=Analysis) +def update_analysis_section_content( + analysis_name: str, + type: SectionRowType, + updated_sections: List[Section], + repositories=Depends(database), + authorized=Security(get_authorization, scopes=["write"]) #pylint: disable=unused-argument +): + """Updates the sections that have changes""" + print(type) + if(type == SectionRowType.TEXT): + update_analysis_sections_text_fields(analysis_name, updated_sections, repositories["analysis"]) + else: + print(type) + print("ADDING TYPE NOT SUPPORTED YET, IN PROGRESS") + + return repositories["analysis"].find_by_name(analysis_name) + +def update_analysis_sections_text_fields(analysis_name, updated_sections: List[Section], analysis_repository): + for section in updated_sections: + for field in section.content: + field_name, field_value= field["fieldName"], field["value"] + if "Nominator" == field_name: + analysis_repository.update_analysis_nominator(analysis_name, '; '.join(field_value)) + analysis_repository.update_analysis_section(analysis_name, section.header, field_name, {"value": field_value}) + +def add_file_to_bucket_repository(file_to_save, bucket_repository): + return bucket_repository.save_file( + file_to_save.file, file_to_save.filename, file_to_save.content_type + ) + +def add_analysis_section_document_field(analysis_name, section_name, field_name, document_file, analysis_repository): + @router.put("/{analysis_name}/section/attach/file") def attach_animal_model_system_report( analysis_name: str, @@ -234,6 +281,14 @@ def attach_section_image( authorized=Security(get_authorization, scopes=["write"]) #pylint: disable=unused-argument ): """ Saves the uploaded image it to the specified field_name in the analysis's section.""" + try: + new_file_object_id = repositories["bucket"].save_file( + upload_file.file, upload_file.filename, upload_file.content_type + ) + except Exception as exception: + raise HTTPException(status_code=500, detail=str(exception)) from exception + + try: new_file_object_id = repositories["bucket"].save_file( upload_file.file, upload_file.filename, upload_file.content_type