From 2374400542d6bab72d4cbcf69a3aa6a2852a13ef Mon Sep 17 00:00:00 2001 From: mike cullerton Date: Thu, 15 Apr 2021 12:39:12 -0400 Subject: [PATCH 1/3] Automatically set the first workflow for a workflow_spec to primary --- crc/api/file.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crc/api/file.py b/crc/api/file.py index 861f9f04..50a06124 100644 --- a/crc/api/file.py +++ b/crc/api/file.py @@ -6,7 +6,7 @@ from flask import send_file from crc import session from crc.api.common import ApiError -from crc.models.file import FileSchema, FileModel, File, FileModelSchema, FileDataModel +from crc.models.file import FileSchema, FileModel, File, FileModelSchema, FileDataModel, FileType from crc.models.workflow import WorkflowSpecModel from crc.services.file_service import FileService @@ -47,9 +47,16 @@ def add_file(workflow_spec_id=None, workflow_id=None, form_field_key=None): name=file.filename, content_type=file.content_type, binary_data=file.stream.read()) elif workflow_spec_id: + # check if we have a primary already + have_primary = FileModel.query.filter(FileModel.workflow_spec_id==workflow_spec_id, FileModel.type==FileType.bpmn, FileModel.primary==True).all() + # set this to primary if we don't already have one + if not have_primary: + primary = True + else: + primary = False workflow_spec = session.query(WorkflowSpecModel).filter_by(id=workflow_spec_id).first() file_model = FileService.add_workflow_spec_file(workflow_spec, file.filename, file.content_type, - file.stream.read()) + file.stream.read(), primary=primary) else: raise ApiError("invalid_file", "You must supply either a workflow spec id or a workflow_id and form_field_key.") From 8953637466ff2b5858314f40689dc4db875f5bb1 Mon Sep 17 00:00:00 2001 From: mike cullerton Date: Thu, 15 Apr 2021 13:28:03 -0400 Subject: [PATCH 2/3] moved the minimal_bpmn method to base_test --- tests/base_test.py | 6 ++++++ tests/files/test_files_api.py | 5 ----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/base_test.py b/tests/base_test.py index a9fcfce1..aa608add 100644 --- a/tests/base_test.py +++ b/tests/base_test.py @@ -456,3 +456,9 @@ class BaseTest(unittest.TestCase): if 'impersonate_user' in g: del g.impersonate_user + + def minimal_bpmn(self, content): + """Returns a bytesIO object of a well formed BPMN xml file with some string content of your choosing.""" + minimal_dbpm = "%s" + return (minimal_dbpm % content).encode() + diff --git a/tests/files/test_files_api.py b/tests/files/test_files_api.py index 6029050a..554a5204 100644 --- a/tests/files/test_files_api.py +++ b/tests/files/test_files_api.py @@ -13,11 +13,6 @@ from example_data import ExampleDataLoader class TestFilesApi(BaseTest): - def minimal_bpmn(self, content): - """Returns a bytesIO object of a well formed BPMN xml file with some string content of your choosing.""" - minimal_dbpm = "%s" - return (minimal_dbpm % content).encode() - def test_list_files_for_workflow_spec(self): self.load_example_data(use_crc_data=True) spec_id = 'core_info' From fdaa06988697aa068da78d8576a253fc4a0340a2 Mon Sep 17 00:00:00 2001 From: mike cullerton Date: Thu, 15 Apr 2021 13:29:42 -0400 Subject: [PATCH 3/3] Added test for automatically setting the primary bpmn for a workflow spec --- tests/test_auto_set_primary_bpmn.py | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/test_auto_set_primary_bpmn.py diff --git a/tests/test_auto_set_primary_bpmn.py b/tests/test_auto_set_primary_bpmn.py new file mode 100644 index 00000000..d6280b69 --- /dev/null +++ b/tests/test_auto_set_primary_bpmn.py @@ -0,0 +1,39 @@ +from tests.base_test import BaseTest +from crc import session +from crc.models.file import FileModel, FileType +from crc.models.workflow import WorkflowSpecModel, WorkflowSpecModelSchema, WorkflowSpecCategoryModel +import io +import json + + +class TestAutoSetPrimaryBPMN(BaseTest): + + def test_auto_set_primary_bpmn(self): + self.load_example_data() + category_id = session.query(WorkflowSpecCategoryModel).first().id + # Add a workflow spec + spec = WorkflowSpecModel(id='make_cookies', name='make_cookies', display_name='Cooooookies', + description='Om nom nom delicious cookies', category_id=category_id) + rv = self.app.post('/v1.0/workflow-specification', + headers=self.logged_in_headers(), + content_type="application/json", + data=json.dumps(WorkflowSpecModelSchema().dump(spec))) + self.assert_success(rv) + # grab the spec from the db + db_spec = session.query(WorkflowSpecModel).filter_by(id='make_cookies').first() + self.assertEqual(spec.display_name, db_spec.display_name) + # Make sure we don't already have a primary bpmn file + have_primary = FileModel.query.filter(FileModel.workflow_spec_id==db_spec.id, FileModel.type==FileType.bpmn, FileModel.primary==True).all() + self.assertEqual(have_primary, []) + 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' % db_spec.id, data=data, follow_redirects=True, + content_type='multipart/form-data', headers=self.logged_in_headers()) + self.assert_success(rv) + file_id = rv.json['id'] + # Make sure we now have a primary bpmn + have_primary = FileModel.query.filter(FileModel.workflow_spec_id==db_spec.id, FileModel.type==FileType.bpmn, FileModel.primary==True).all() + self.assertEqual(len(have_primary), 1) + self.assertEqual(file_id, have_primary[0].id)