From 8e37f27399ac8e9ebe9e5f8912714adda3bf376c Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 14 May 2021 15:52:25 -0400 Subject: [PATCH] Assure that any data store values associated with a file come back as a part of the get_study_data[documents ] endooint. --- crc/services/study_service.py | 26 ++++++++++++++------- tests/study/test_study_details_documents.py | 21 +++++++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/crc/services/study_service.py b/crc/services/study_service.py index b01c42b8..8d117cac 100644 --- a/crc/services/study_service.py +++ b/crc/services/study_service.py @@ -6,11 +6,13 @@ from typing import List import flask import requests from SpiffWorkflow import WorkflowException +from SpiffWorkflow.bpmn.PythonScriptEngine import Box from SpiffWorkflow.exceptions import WorkflowTaskExecException from ldap3.core.exceptions import LDAPSocketOpenError from crc import db, session, app from crc.api.common import ApiError +from crc.models.data_store import DataStoreModel from crc.models.email import EmailModel from crc.models.file import FileDataModel, FileModel, FileModelSchema, File, LookupFileModel, LookupDataModel from crc.models.ldap import LdapSchema @@ -297,21 +299,27 @@ class StudyService(object): if hasattr(flask.g,'user'): token = flask.g.user.encode_auth_token() for file in doc_files: - doc['files'].append({'file_id': file.id, - 'name': file.name, - 'url': app.config['APPLICATION_ROOT']+ - 'file/' + str(file.id) + - '/download?auth_token='+ - urllib.parse.quote_plus(token), - 'workflow_id': file.workflow_id}) - + file_data = {'file_id': file.id, + 'name': file.name, + 'url': app.config['APPLICATION_ROOT']+ + 'file/' + str(file.id) + + '/download?auth_token='+ + urllib.parse.quote_plus(token), + 'workflow_id': file.workflow_id + } + data = db.session.query(DataStoreModel).filter(DataStoreModel.file_id==file.id).all() + data_store_data = {} + for d in data: + data_store_data[d.key] = d.value + file_data["data_store"] = data_store_data + doc['files'].append(Box(file_data)) # update the document status to match the status of the workflow it is in. if 'status' not in doc or doc['status'] is None: workflow: WorkflowModel = session.query(WorkflowModel).filter_by(id=file.workflow_id).first() doc['status'] = workflow.status.value documents[code] = doc - return documents + return Box(documents) @staticmethod def get_investigators(study_id, all=False): diff --git a/tests/study/test_study_details_documents.py b/tests/study/test_study_details_documents.py index ef1180fc..757ff938 100644 --- a/tests/study/test_study_details_documents.py +++ b/tests/study/test_study_details_documents.py @@ -14,6 +14,7 @@ from crc.scripts.study_info import StudyInfo from crc.services.file_service import FileService from crc.services.study_service import StudyService from crc.services.workflow_processor import WorkflowProcessor +from crc.scripts.file_data_set import FileDataSet class TestStudyDetailsDocumentsScript(BaseTest): @@ -93,3 +94,23 @@ class TestStudyDetailsDocumentsScript(BaseTest): docs = StudyInfo().do_task_validate_only(task, study.id, workflow_model.id, "documents") self.assertTrue(isinstance(docs, Box)) + @patch('crc.services.protocol_builder.requests.get') + def test_study_info_returns_document_data_store_values_with_documents(self, mock_get): + mock_get.return_value.ok = True + mock_get.return_value.text = self.protocol_builder_response('required_docs.json') + self.load_example_data() + self.create_reference_document() + study = session.query(StudyModel).first() + workflow_spec_model = self.load_test_spec("two_forms") + workflow_model = StudyService._create_workflow_model(study, workflow_spec_model) + irb_code = "UVACompl_PRCAppr" # The first file referenced in pb required docs. + file = FileService.add_workflow_file(workflow_id=workflow_model.id, + name="anything.png", content_type="text", + binary_data=b'1234', irb_doc_code=irb_code) + processor = WorkflowProcessor(workflow_model) + task = processor.next_task() + FileDataSet().do_task(task, study.id, workflow_model.id, key="ginger", value="doodle", file_id=file.id) + docs = StudyInfo().do_task(task, study.id, workflow_model.id, "documents") + self.assertTrue(isinstance(docs, Box)) + self.assertEquals(1, len(docs.UVACompl_PRCAppr.files)) + self.assertEquals("doodle", docs.UVACompl_PRCAppr.files[0].data_store.ginger) \ No newline at end of file