2019-12-18 19:02:17 +00:00
|
|
|
import enum
|
|
|
|
|
2019-12-31 21:32:47 +00:00
|
|
|
import marshmallow
|
2019-12-18 19:02:17 +00:00
|
|
|
from marshmallow_enum import EnumField
|
2019-12-31 21:32:47 +00:00
|
|
|
from marshmallow_sqlalchemy import ModelSchema
|
2019-12-18 19:02:17 +00:00
|
|
|
|
2019-12-19 16:58:51 +00:00
|
|
|
from crc import db, ma
|
2019-12-18 19:02:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WorkflowSpecModel(db.Model):
|
|
|
|
__tablename__ = 'workflow_spec'
|
|
|
|
id = db.Column(db.String, primary_key=True)
|
2020-01-21 20:22:44 +00:00
|
|
|
name = db.Column(db.String)
|
2019-12-18 19:02:17 +00:00
|
|
|
display_name = db.Column(db.String)
|
|
|
|
description = db.Column(db.Text)
|
2020-01-23 15:54:41 +00:00
|
|
|
primary_process_id = db.Column(db.String)
|
2019-12-18 19:02:17 +00:00
|
|
|
|
2019-12-27 18:50:03 +00:00
|
|
|
|
2020-01-03 16:44:24 +00:00
|
|
|
class WorkflowSpecModelSchema(ModelSchema):
|
2019-12-18 19:02:17 +00:00
|
|
|
class Meta:
|
|
|
|
model = WorkflowSpecModel
|
|
|
|
|
2020-01-03 16:44:24 +00:00
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
class WorkflowStatus(enum.Enum):
|
|
|
|
new = "new"
|
|
|
|
user_input_required = "user_input_required"
|
|
|
|
waiting = "waiting"
|
|
|
|
complete = "complete"
|
|
|
|
|
|
|
|
|
|
|
|
class WorkflowModel(db.Model):
|
|
|
|
__tablename__ = 'workflow'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-01-23 15:53:59 +00:00
|
|
|
bpmn_workflow_json = db.Column(db.JSON)
|
2019-12-18 19:02:17 +00:00
|
|
|
status = db.Column(db.Enum(WorkflowStatus))
|
|
|
|
study_id = db.Column(db.Integer, db.ForeignKey('study.id'))
|
2020-01-06 14:18:13 +00:00
|
|
|
workflow_spec_id = db.Column(db.String, db.ForeignKey('workflow_spec.id'))
|
2019-12-18 19:02:17 +00:00
|
|
|
|
2019-12-19 16:58:51 +00:00
|
|
|
|
2020-01-03 16:44:24 +00:00
|
|
|
class WorkflowModelSchema(ModelSchema):
|
2019-12-18 19:02:17 +00:00
|
|
|
class Meta:
|
|
|
|
model = WorkflowModel
|
2020-01-06 14:18:13 +00:00
|
|
|
include_fk = True # Includes foreign keys
|
2019-12-20 19:32:04 +00:00
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
status = EnumField(WorkflowStatus)
|
|
|
|
|
|
|
|
|
2019-12-19 16:58:51 +00:00
|
|
|
class Task:
|
2020-02-03 15:49:48 +00:00
|
|
|
def __init__(self, id, name, title, type, state, form, documentation, data):
|
2019-12-19 16:58:51 +00:00
|
|
|
self.id = id
|
|
|
|
self.name = name
|
2019-12-20 20:39:02 +00:00
|
|
|
self.title = title
|
2019-12-19 16:58:51 +00:00
|
|
|
self.type = type
|
|
|
|
self.state = state
|
|
|
|
self.form = form
|
2019-12-20 20:39:02 +00:00
|
|
|
self.documentation = documentation
|
2020-02-03 15:49:48 +00:00
|
|
|
self.data = data
|
2019-12-19 16:58:51 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_spiff(cls, spiff_task):
|
|
|
|
instance = cls(spiff_task.id,
|
|
|
|
spiff_task.task_spec.name,
|
2019-12-20 20:39:02 +00:00
|
|
|
spiff_task.task_spec.description,
|
2019-12-19 16:58:51 +00:00
|
|
|
"task",
|
2020-01-31 17:40:08 +00:00
|
|
|
spiff_task.get_state_name(),
|
2019-12-20 20:39:02 +00:00
|
|
|
{},
|
2020-02-03 15:49:48 +00:00
|
|
|
spiff_task.task_spec.documentation,
|
|
|
|
spiff_task.data)
|
2019-12-19 16:58:51 +00:00
|
|
|
if hasattr(spiff_task.task_spec, "form"):
|
|
|
|
instance.type = "form"
|
|
|
|
instance.form = spiff_task.task_spec.form
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
class OptionSchema(ma.Schema):
|
|
|
|
class Meta:
|
|
|
|
fields = ["id", "name"]
|
|
|
|
|
|
|
|
|
2019-12-20 19:32:04 +00:00
|
|
|
class ValidationSchema(ma.Schema):
|
|
|
|
class Meta:
|
|
|
|
fields = ["name", "config"]
|
|
|
|
|
|
|
|
|
|
|
|
class PropertiesSchema(ma.Schema):
|
|
|
|
class Meta:
|
|
|
|
fields = ["id", "value"]
|
|
|
|
|
|
|
|
|
2019-12-31 21:32:47 +00:00
|
|
|
class FormFieldSchema(ma.Schema):
|
2019-12-19 16:58:51 +00:00
|
|
|
class Meta:
|
2019-12-20 19:32:04 +00:00
|
|
|
fields = [
|
2019-12-31 21:32:47 +00:00
|
|
|
"id", "type", "label", "defaultValue", "options", "validation", "properties", "value"
|
2019-12-20 19:32:04 +00:00
|
|
|
]
|
|
|
|
|
2019-12-31 21:32:47 +00:00
|
|
|
options = marshmallow.fields.List(marshmallow.fields.Nested(OptionSchema))
|
|
|
|
validation = marshmallow.fields.List(marshmallow.fields.Nested(ValidationSchema))
|
|
|
|
properties = marshmallow.fields.List(marshmallow.fields.Nested(PropertiesSchema))
|
2019-12-19 16:58:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FormSchema(ma.Schema):
|
2019-12-31 21:32:47 +00:00
|
|
|
key = marshmallow.fields.String(required=True, allow_none=False)
|
|
|
|
fields = marshmallow.fields.List(marshmallow.fields.Nested(FormFieldSchema))
|
2019-12-19 16:58:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TaskSchema(ma.Schema):
|
|
|
|
class Meta:
|
2020-02-03 15:49:48 +00:00
|
|
|
fields = ["id", "name", "title", "type", "state", "form", "documentation", "data"]
|
2020-01-03 16:44:24 +00:00
|
|
|
|
2019-12-31 21:32:47 +00:00
|
|
|
documentation = marshmallow.fields.String(required=False, allow_none=True)
|
|
|
|
form = marshmallow.fields.Nested(FormSchema)
|
2019-12-19 16:58:51 +00:00
|
|
|
|
2019-12-31 21:32:47 +00:00
|
|
|
@marshmallow.post_load
|
2019-12-19 16:58:51 +00:00
|
|
|
def make_task(self, data, **kwargs):
|
|
|
|
return Task(**data)
|