cr-connect-workflow/crc/models/workflow.py

112 lines
3.1 KiB
Python
Raw Normal View History

import enum
import marshmallow
from marshmallow_enum import EnumField
from marshmallow_sqlalchemy import ModelSchema
from crc import db, ma
class WorkflowSpecModel(db.Model):
__tablename__ = 'workflow_spec'
id = db.Column(db.String, primary_key=True)
display_name = db.Column(db.String)
description = db.Column(db.Text)
class WorkflowSpecModelSchema(ModelSchema):
class Meta:
model = WorkflowSpecModel
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)
bpmn_workflow_json = db.Column(db.TEXT)
status = db.Column(db.Enum(WorkflowStatus))
study_id = db.Column(db.Integer, db.ForeignKey('study.id'))
workflow_spec_id = db.Column(db.String, db.ForeignKey('workflow_spec.id'))
class WorkflowModelSchema(ModelSchema):
class Meta:
model = WorkflowModel
include_fk = True # Includes foreign keys
2019-12-20 19:32:04 +00:00
status = EnumField(WorkflowStatus)
class Task:
2019-12-20 20:39:02 +00:00
def __init__(self, id, name, title, type, state, form, documentation):
self.id = id
self.name = name
2019-12-20 20:39:02 +00:00
self.title = title
self.type = type
self.state = state
self.form = form
2019-12-20 20:39:02 +00:00
self.documentation = documentation
@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,
spiff_task.get_state_name(),
"task",
2019-12-20 20:39:02 +00:00
{},
spiff_task.task_spec.documentation)
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"]
class FormFieldSchema(ma.Schema):
class Meta:
2019-12-20 19:32:04 +00:00
fields = [
"id", "type", "label", "defaultValue", "options", "validation", "properties", "value"
2019-12-20 19:32:04 +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))
class FormSchema(ma.Schema):
key = marshmallow.fields.String(required=True, allow_none=False)
fields = marshmallow.fields.List(marshmallow.fields.Nested(FormFieldSchema))
class TaskSchema(ma.Schema):
class Meta:
2019-12-20 20:39:02 +00:00
fields = ["id", "name", "title", "type", "state", "form", "documentation"]
documentation = marshmallow.fields.String(required=False, allow_none=True)
form = marshmallow.fields.Nested(FormSchema)
@marshmallow.post_load
def make_task(self, data, **kwargs):
return Task(**data)