def test_change_primary_bpmn(self): self.load_example_data() spec = session.query(WorkflowSpecModel).first() data = {} data['file'] = io.BytesIO( self.minimal_bpmn("abcdef")), 'my_new_file.bpmn' # Add a new BPMN file to the specification rv = self.app.post('/v1.0/file?workflow_spec_id=%s' % spec.id, data=data, follow_redirects=True, content_type='multipart/form-data', headers=self.logged_in_headers()) self.assert_success(rv) self.assertIsNotNone(rv.get_data()) json_data = json.loads(rv.get_data(as_text=True)) file = FileModelSchema().load(json_data, session=session) # Delete the primary BPMN file for the workflow. orig_model = session.query(FileModel).\ filter(FileModel.primary == True).\ filter(FileModel.workflow_spec_id == spec.id).first() rv = self.app.delete('/v1.0/file?file_id=%s' % orig_model.id, headers=self.logged_in_headers()) # Set that new file to be the primary BPMN, assure it has a primary_process_id file.primary = True rv = self.app.put('/v1.0/file/%i' % file.id, content_type="application/json", data=json.dumps(FileModelSchema().dump(file)), headers=self.logged_in_headers()) self.assert_success(rv) json_data = json.loads(rv.get_data(as_text=True)) self.assertTrue(json_data['primary']) self.assertIsNotNone(json_data['primary_process_id'])
def test_create_file(self): self.load_example_data() spec = session.query(WorkflowSpecModel).first() data = {'file': (io.BytesIO(b"abcdef"), 'random_fact.svg')} rv = self.app.post('/v1.0/file?workflow_spec_id=%s' % spec.id, data=data, follow_redirects=True, content_type='multipart/form-data', headers=self.logged_in_headers()) self.assert_success(rv) self.assertIsNotNone(rv.get_data()) json_data = json.loads(rv.get_data(as_text=True)) file = FileModelSchema().load(json_data, session=session) self.assertEqual(FileType.svg, file.type) self.assertFalse(file.primary) self.assertEqual("image/svg+xml", file.content_type) self.assertEqual(spec.id, file.workflow_spec_id) rv = self.app.get('/v1.0/file/%i' % file.id, headers=self.logged_in_headers()) self.assert_success(rv) json_data = json.loads(rv.get_data(as_text=True)) file2 = FileModelSchema().load(json_data, session=session) self.assertEqual(file, file2)
def test_list_reference_files(self): ExampleDataLoader.clean_db() file_name = DocumentService.DOCUMENT_LIST filepath = os.path.join(app.root_path, 'static', 'reference', 'irb_documents.xlsx') with open(filepath, 'rb') as myfile: file_data = myfile.read() data = {'file': (io.BytesIO(file_data), file_name)} rv = self.app.post('/v1.0/reference_file', data=data, follow_redirects=True, content_type='multipart/form-data', headers=self.logged_in_headers()) self.assert_success(rv) rv = self.app.get('/v1.0/reference_file', follow_redirects=True, content_type="application/json", headers=self.logged_in_headers()) self.assert_success(rv) json_data = json.loads(rv.get_data(as_text=True)) self.assertEqual(1, len(json_data)) file = FileModelSchema(many=True).load(json_data, session=session) self.assertEqual(file_name, file[0].name) self.assertTrue(file[0].is_reference)
def write_file_info_to_system(file_path, file_model): json_file_path = f'{file_path}.json' latest_file_model = session.query(FileModel).filter( FileModel.id == file_model.id).first() file_schema = FileModelSchema().dumps(latest_file_model) with open(json_file_path, 'w') as j_handle: j_handle.write(file_schema)
def test_document_added_to_workflow_shows_up_in_file_list(self): self.load_example_data() self.create_reference_document() workflow = self.create_workflow('docx') # get the first form in the two form workflow. task = self.get_workflow_api(workflow).next_task data = { "full_name": "Buck of the Wild", "date": "5/1/2020", "title": "Leader of the Pack", "company": "In the company of wolves", "last_name": "Mr. Wolf" } workflow_api = self.complete_form(workflow, task, data) self.assertIsNotNone(workflow_api.next_task) self.assertEqual("EndEvent_0evb22x", workflow_api.next_task.name) self.assertTrue(workflow_api.status == WorkflowStatus.complete) rv = self.app.get('/v1.0/file?workflow_id=%i' % workflow.id, headers=self.logged_in_headers()) self.assert_success(rv) json_data = json.loads(rv.get_data(as_text=True)) files = FileModelSchema(many=True).load(json_data, session=session) self.assertTrue(len(files) == 1) # Assure we can still delete the study even when there is a file attached to a workflow. rv = self.app.delete('/v1.0/study/%i' % workflow.study_id, headers=self.logged_in_headers()) self.assert_success(rv)
def get_protocol(study_id): """Returns the study protocol, if it has been uploaded.""" file = db.session.query(FileModel)\ .filter_by(study_id=study_id)\ .filter_by(form_field_key='Study_Protocol_Document')\ .first() return FileModelSchema().dump(file)
def update_reference_file_info(self, old_file_model, body): file_data = self.get_reference_file_data(old_file_model.name) old_file_path = self.get_reference_file_path(old_file_model.name) self.delete_reference_file_data(old_file_path) self.delete_reference_file_info(old_file_path) new_file_model = FileModelSchema().load(body, session=session) new_file_path = self.get_reference_file_path(new_file_model.name) self.write_reference_file_data_to_system(new_file_path, file_data.data) self.write_reference_file_info_to_system(new_file_path, new_file_model) return new_file_model
def test_update_file_info(self): self.load_example_data() file: FileModel = session.query(FileModel).first() file.name = "silly_new_name.bpmn" rv = self.app.put('/v1.0/file/%i' % file.id, content_type="application/json", data=json.dumps(FileModelSchema().dump(file)), headers=self.logged_in_headers()) self.assert_success(rv) db_file = session.query(FileModel).filter_by(id=file.id).first() self.assertIsNotNone(db_file) self.assertEqual(file.name, db_file.name)
def update_file_info(file_id, body): if file_id is None: raise ApiError('no_such_file', 'Please provide a valid File ID.') file_model = session.query(FileModel).filter_by(id=file_id).first() if file_model is None: raise ApiError('unknown_file_model', 'The file_model "' + file_id + '" is not recognized.') file_model = FileModelSchema().load(body, session=session) session.add(file_model) session.commit() return FileSchema().dump(to_file_api(file_model))
def test_list_files_for_workflow_spec(self): self.load_example_data(use_crc_data=True) spec_id = 'core_info' spec = session.query(WorkflowSpecModel).filter_by(id=spec_id).first() rv = self.app.get('/v1.0/file?workflow_spec_id=%s' % spec_id, follow_redirects=True, content_type="application/json", headers=self.logged_in_headers()) self.assert_success(rv) json_data = json.loads(rv.get_data(as_text=True)) self.assertEqual(5, len(json_data)) files = FileModelSchema(many=True).load(json_data, session=session) file_names = [f.name for f in files] self.assertTrue("%s.bpmn" % spec.name in file_names)
def update_spec_file_info(self, old_file_model, body): file_data = self.get_spec_file_data(old_file_model.id) old_file_path = self.get_path_from_spec_file_model(old_file_model) self.delete_spec_file_data(old_file_path) self.delete_spec_file_info(old_file_path) new_file_model = FileModelSchema().load(body, session=session) new_file_path = self.get_path_from_spec_file_model(new_file_model) self.write_file_data_to_system(new_file_path, file_data.data) self.write_file_info_to_system(new_file_path, new_file_model) print('update_spec_file_info') return new_file_model
def test_set_reference_file(self): file_name = "irb_document_types.xls" data = {'file': (io.BytesIO(b"abcdef"), "does_not_matter.xls")} rv = self.app.put('/v1.0/reference_file/%s' % file_name, data=data, follow_redirects=True, content_type='multipart/form-data', headers=self.logged_in_headers()) self.assert_success(rv) self.assertIsNotNone(rv.get_data()) json_data = json.loads(rv.get_data(as_text=True)) file = FileModelSchema().load(json_data, session=session) self.assertEqual(FileType.xls, file.type) self.assertTrue(file.is_reference) self.assertEqual("application/vnd.ms-excel", file.content_type)
def test_add_reference_file(self): ExampleDataLoader().load_reference_documents() file_name = 'new.xlsx' data = {'file': (io.BytesIO(b"abcdef"), file_name)} rv = self.app.post('/v1.0/reference_file', data=data, follow_redirects=True, content_type='multipart/form-data', headers=self.logged_in_headers()) self.assertIsNotNone(rv.get_data()) json_data = json.loads(rv.get_data(as_text=True)) file = FileModelSchema().load(json_data, session=session) self.assertEqual(FileType.xlsx, file.type) self.assertFalse(file.primary) self.assertEqual(True, file.is_reference)
def process_workflow_spec_file(session, workflow_spec_file, workflow_spec_file_path): # workflow_spec_file_path = os.path.join os.makedirs(os.path.dirname(workflow_spec_file_path), exist_ok=True) file_data_model = session.query(FileDataModel). \ filter(FileDataModel.file_model_id == workflow_spec_file.id). \ order_by(sa.desc(FileDataModel.version)). \ first() with open(workflow_spec_file_path, 'wb') as f_handle: f_handle.write(file_data_model.data) json_file_path = f'{workflow_spec_file_path}.json' workflow_spec_file_model = session.query(FileModel).filter( FileModel.id == file_data_model.file_model_id).first() workflow_spec_file_schema = FileModelSchema().dumps( workflow_spec_file_model) with open(json_file_path, 'w') as j_handle: j_handle.write(workflow_spec_file_schema)
def test_update_file_data(self): self.load_example_data() spec = session.query(WorkflowSpecModel).first() data = {} data['file'] = io.BytesIO( self.minimal_bpmn("abcdef")), 'my_new_file.bpmn' rv = self.app.post('/v1.0/file?workflow_spec_id=%s' % spec.id, data=data, follow_redirects=True, content_type='multipart/form-data', headers=self.logged_in_headers()) json_data = json.loads(rv.get_data(as_text=True)) file = FileModelSchema().load(json_data, session=session) data['file'] = io.BytesIO( self.minimal_bpmn("efghijk")), 'my_new_file.bpmn' rv = self.app.put('/v1.0/file/%i/data' % file.id, data=data, follow_redirects=True, content_type='multipart/form-data', headers=self.logged_in_headers()) self.assert_success(rv) self.assertIsNotNone(rv.get_data()) file_json = json.loads(rv.get_data(as_text=True)) self.assertEqual(2, file_json['latest_version']) self.assertEqual(FileType.bpmn.value, file_json['type']) self.assertEqual("application/octet-stream", file_json['content_type']) self.assertEqual(spec.id, file.workflow_spec_id) # Assure it is updated in the database and properly persisted. file_model = session.query(FileModel).filter( FileModel.id == file.id).first() file_data = FileService.get_file_data(file_model.id) self.assertEqual(2, file_data.version) rv = self.app.get('/v1.0/file/%i/data' % file.id, headers=self.logged_in_headers()) self.assert_success(rv) data = rv.get_data() self.assertIsNotNone(data) self.assertEqual(self.minimal_bpmn("efghijk"), data)
def test_list_reference_files(self): ExampleDataLoader.clean_db() file_name = FileService.DOCUMENT_LIST data = {'file': (io.BytesIO(b"abcdef"), file_name)} rv = self.app.put('/v1.0/reference_file/%s' % file_name, data=data, follow_redirects=True, content_type='multipart/form-data', headers=self.logged_in_headers()) rv = self.app.get('/v1.0/reference_file', follow_redirects=True, content_type="application/json", headers=self.logged_in_headers()) self.assert_success(rv) json_data = json.loads(rv.get_data(as_text=True)) self.assertEqual(1, len(json_data)) file = FileModelSchema(many=True).load(json_data, session=session) self.assertEqual(file_name, file[0].name) self.assertTrue(file[0].is_reference)
def test_update_reference_file_data(self): self.load_example_data() file_name = "documents.xlsx" filepath = os.path.join(app.root_path, 'static', 'reference', 'irb_documents.xlsx') with open(filepath, 'rb') as myfile: file_data = myfile.read() data = {'file': (io.BytesIO(file_data), file_name)} rv = self.app.put('/v1.0/reference_file/%s/data' % file_name, data=data, follow_redirects=True, content_type='multipart/form-data', headers=self.logged_in_headers()) self.assert_success(rv) self.assertIsNotNone(rv.get_data()) json_data = json.loads(rv.get_data(as_text=True)) file = FileModelSchema().load(json_data, session=session) self.assertEqual(FileType.xlsx, file.type) self.assertTrue(file.is_reference) self.assertEqual( "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.content_type)
def test_update_file_info(self): self.load_example_data() file: FileModel = session.query(FileModel).filter( column('workflow_spec_id').isnot(None)).first() file_model = FileModel(id=file.id, name="silly_new_name.bpmn", type=file.type, content_type=file.content_type, is_reference=file.is_reference, primary=file.primary, primary_process_id=file.primary_process_id, workflow_id=file.workflow_id, workflow_spec_id=file.workflow_spec_id, archived=file.archived) # file.name = "silly_new_name.bpmn" rv = self.app.put('/v1.0/spec_file/%i' % file.id, content_type="application/json", data=json.dumps(FileModelSchema().dump(file_model)), headers=self.logged_in_headers()) self.assert_success(rv) db_file = session.query(FileModel).filter_by(id=file.id).first() self.assertIsNotNone(db_file) self.assertEqual("silly_new_name.bpmn", db_file.name)