From 6361e94af5a52cd262175c392902e4303a3d005f Mon Sep 17 00:00:00 2001 From: Dan Funk Date: Tue, 14 Jan 2020 11:02:44 -0500 Subject: [PATCH] Allow submitting data and progressing to the next task in a workflow. --- Pipfile.lock | 6 +++--- crc/api.yml | 39 ++++++++++++++++++++++++++++++--------- crc/api/workflow.py | 19 ++++++++++--------- crc/workflow_processor.py | 5 +++-- tests/test_api.py | 9 ++++----- 5 files changed, 50 insertions(+), 28 deletions(-) diff --git a/Pipfile.lock b/Pipfile.lock index c757c61d..3514ccd9 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -508,9 +508,9 @@ }, "pyrsistent": { "hashes": [ - "sha256:f3b280d030afb652f79d67c5586157c5c1355c9a58dfc7940566e28d28f3df1b" + "sha256:cdc7b5e3ed77bed61270a47d35434a30617b9becdf2478af76ad2c6ade307280" ], - "version": "==0.15.6" + "version": "==0.15.7" }, "python-dateutil": { "hashes": [ @@ -640,7 +640,7 @@ "spiffworkflow": { "editable": true, "git": "https://github.com/sartography/SpiffWorkflow.git", - "ref": "d9f406c57ab05ce6e978318a22b6d327b635e1ae" + "ref": "970371c1a0aaacb8ed9d25321ee534bf34fa5fa8" }, "sqlalchemy": { "hashes": [ diff --git a/crc/api.yml b/crc/api.yml index 0c8240e8..766ff0a0 100644 --- a/crc/api.yml +++ b/crc/api.yml @@ -431,7 +431,7 @@ paths: schema: $ref: "#/components/schemas/Error" # /v1.0/workflow/0/task/0 - /workflow/{workflow_id}/task/{task_name}: + /workflow/{workflow_id}/task/{task_id}: parameters: - name: workflow_id in: path @@ -440,12 +440,13 @@ paths: schema: type: integer format: int32 - - name: task_name + - name: task_id in: path required: true description: The id of the task schema: type: string + format: uuid get: operationId: crc.api.workflow.get_task summary: Get details of specific task in specific workflow instance @@ -464,17 +465,37 @@ paths: application/json: schema: $ref: "#/components/schemas/Error" - + /workflow/{workflow_id}/task/{task_id}/data: + parameters: + - name: workflow_id + in: path + required: true + description: The id of the workflow + schema: + type: integer + format: int32 + - name: task_id + in: path + required: true + description: The id of the task + schema: + type: string + format: uuid put: operationId: crc.api.workflow.update_task - summary: Update, attempt to complete a workflow task + summary: Exclusively for User Tasks, submits form data as a flat set of key/values. tags: - Workflows and Tasks - parameters: - - in: body - name: task - schema: - $ref: '#/components/schemas/Task' + requestBody: + description: Key / Value pairs in JSON format. + required: true + content: + application/json: + schema: + type: object + example: + favorite_color: blue + capital_assyria: Assur responses: '201': description: Null response diff --git a/crc/api/workflow.py b/crc/api/workflow.py index 4edd2356..104af692 100644 --- a/crc/api/workflow.py +++ b/crc/api/workflow.py @@ -1,3 +1,5 @@ +import uuid + from crc import db from crc.models.workflow import WorkflowModel, WorkflowModelSchema, WorkflowSpecModelSchema, WorkflowSpecModel, \ Task, TaskSchema @@ -42,14 +44,13 @@ def get_task(workflow_id, task_id): return workflow.bpmn_workflow().get_task(task_id) -def update_task(workflow_id, task_name, body): +def update_task(workflow_id, task_id, body): workflow = db.session.query(WorkflowModel).filter_by(id=workflow_id).first() processor = WorkflowProcessor(workflow.workflow_spec_id, workflow.bpmn_workflow_json) - task = processor.bpmn_workflow.get_tasks_from_spec_name(task_name)[0] - if workflow and task and body: - print('workflow', workflow.id) - print('task', task.id) - print('body', body) - processor = WorkflowProcessor(workflow.workflow_spec_id, workflow.bpmn_workflow_json) - processor.complete_task(task) - return body + task_id = uuid.UUID(task_id) + task = processor.bpmn_workflow.get_task(task_id) + task.data = body + processor.complete_task(task) + workflow.bpmn_workflow_json = processor.serialize() + db.session.add(workflow) + db.session.commit() \ No newline at end of file diff --git a/crc/workflow_processor.py b/crc/workflow_processor.py index f2299dd5..f84d8d8b 100644 --- a/crc/workflow_processor.py +++ b/crc/workflow_processor.py @@ -4,6 +4,7 @@ from SpiffWorkflow.bpmn.BpmnScriptEngine import BpmnScriptEngine from SpiffWorkflow.bpmn.serializer.CompactWorkflowSerializer import CompactWorkflowSerializer from SpiffWorkflow.bpmn.workflow import BpmnWorkflow from SpiffWorkflow.camunda.parser.CamundaParser import CamundaParser +from SpiffWorkflow.camunda.serializer.CamundaSerializer import CamundaSerializer from crc import db from crc.models.file import FileDataModel, FileModel @@ -33,10 +34,10 @@ class CustomBpmnScriptEngine(BpmnScriptEngine): class WorkflowProcessor: _script_engine = CustomBpmnScriptEngine() - _serializer = CompactWorkflowSerializer() + _serializer = CamundaSerializer() def __init__(self, workflow_spec_id, bpmn_json): - self.bpmn_workflow = self._serializer.deserialize_workflow(bpmn_json, self.get_spec(workflow_spec_id)) + self.bpmn_workflow = self._serializer.deserialize_workflow(bpmn_json, wf_spec=self.get_spec(workflow_spec_id)) self.bpmn_workflow.script_engine = self._script_engine @staticmethod diff --git a/tests/test_api.py b/tests/test_api.py index ced01654..0fa1b1e1 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -145,7 +145,7 @@ class TestStudy(BaseTest, unittest.TestCase): self.assertEqual(3, len(tasks[0].form["fields"][0]["options"])) def test_two_forms_task(self): - # Set up a new workfllow + # Set up a new workflow self.load_example_data() study = db.session.query(StudyModel).first() spec = db.session.query(WorkflowSpecModel).filter_by(id='two_forms').first() @@ -164,9 +164,9 @@ class TestStudy(BaseTest, unittest.TestCase): self.assertEqual(1, len(tasks[0].form['fields'])) # Complete the form for Step one and post it. - tasks[0].form['fields'][0]['value']="Blue" - rv = self.app.put('/v1.0/workflow/%i/task/%s' % (workflow.id, tasks[0].name), content_type="application/json", - data=json.dumps(TaskSchema().dump(tasks[0]))) + tasks[0].form['fields'][0]['value'] = "Blue" + rv = self.app.put('/v1.0/workflow/%i/task/%s/data' % (workflow.id, tasks[0].id), content_type="application/json", + data=json.dumps({"color":"blue"})) self.assert_success(rv) # Get the next Task @@ -175,4 +175,3 @@ class TestStudy(BaseTest, unittest.TestCase): json_data = json.loads(rv.get_data(as_text=True)) tasks = TaskSchema(many=True).load(json_data) self.assertEqual("StepTwo", tasks[0].name) - self.assertEqual(3, len(tasks[0].form["fields"][0]["options"]))