Add workflow to study

This commit is contained in:
Dan Funk 2019-12-18 15:22:46 -05:00
parent fd60dc99c3
commit 53a8811769
4 changed files with 23 additions and 11 deletions

View File

@ -52,8 +52,13 @@ def add_workflow_to_study(study_id, body):
if workflow_spec_model is None:
error = ApiError('unknown_spec', 'The specification "' + body['id'] + '" is not recognized.')
return ApiErrorSchema.dump(error), 404
processor = WorkflowProcessor.create(workflow_spec_model.id)
processor = WorkflowProcessor.create(workflow_spec_model.id)
workflow = WorkflowModel(bpmn_workflow_json=processor.serialize(),
status=processor.get_status(),
study_id=study_id,
workflow_spec_id=workflow_spec_model.id)
db.session.add(workflow)
def get_workflow(workflow_id):
return db.session.query(WorkflowModel).filter_by(id=workflow_id).first()

View File

@ -55,7 +55,6 @@ class WorkflowModel(db.Model):
status = db.Column(db.Enum(WorkflowStatus))
study_id = db.Column(db.Integer, db.ForeignKey('study.id'))
workflow_spec_id = db.Column(db.Integer, db.ForeignKey('workflow_spec.id'))
messages: db.Column
class WorkflowSchema(ModelSchema):
class Meta:

View File

@ -33,12 +33,12 @@ class CustomBpmnScriptEngine(BpmnScriptEngine):
class WorkflowProcessor:
script_engine = CustomBpmnScriptEngine()
serializer = CompactWorkflowSerializer()
_script_engine = CustomBpmnScriptEngine()
_serializer = CompactWorkflowSerializer()
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.script_engine = self.script_engine
self.bpmn_workflow = self._serializer.deserialize_workflow(bpmn_json, self.get_spec(workflow_spec_id))
self.bpmn_workflow.script_engine = self._script_engine
@staticmethod
def get_spec(workflow_spec_id):
@ -48,9 +48,9 @@ class WorkflowProcessor:
@classmethod
def create(cls, workflow_spec_id):
spec = cls.get_spec(workflow_spec_id)
bpmn_workflow = BpmnWorkflow(spec, script_engine=cls.script_engine)
bpmn_workflow = BpmnWorkflow(spec, script_engine=cls._script_engine)
bpmn_workflow.do_engine_steps()
json = cls.serializer.serialize_workflow(bpmn_workflow)
json = cls._serializer.serialize_workflow(bpmn_workflow)
processor = cls(workflow_spec_id, json)
return processor

View File

@ -1,7 +1,10 @@
import json
import unittest
from sqlalchemy import func
from crc import db
from crc.models import StudyModel, StudySchema, WorkflowSpecModel, WorkflowSpecSchema
from crc.models import StudyModel, StudySchema, WorkflowSpecModel, WorkflowSpecSchema, WorkflowModel, WorkflowStatus
from tests.base_test import BaseTest
@ -47,9 +50,14 @@ class TestStudy(BaseTest, unittest.TestCase):
def test_add_workflow_to_study(self):
self.load_example_data()
study = db.session.query(StudyModel).first()
self.assertEqual(0, db.session.query(WorkflowModel).count())
spec = db.session.query(WorkflowSpecModel).first()
rv = self.app.post('/v1.0/study/%i/workflows' % study.id,content_type="application/json",
data=json.dumps(WorkflowSpecSchema().dump(spec)))
self.assert_success(rv)
self.assertEqual(1, db.session.query(WorkflowModel).count())
workflow = db.session.query(WorkflowModel).first()
self.assertEqual(study.id, workflow.study_id)
self.assertEqual(WorkflowStatus.user_input_required, workflow.status)
self.assertIsNotNone(workflow.bpmn_workflow_json)
self.assertEqual(spec.id, workflow.workflow_spec_id)