2019-12-18 19:02:17 +00:00
|
|
|
import enum
|
|
|
|
|
2020-03-04 18:40:25 +00:00
|
|
|
import jinja2
|
2019-12-31 21:32:47 +00:00
|
|
|
import marshmallow
|
2020-03-04 18:40:25 +00:00
|
|
|
from jinja2 import Template
|
2020-02-18 15:14:03 +00:00
|
|
|
from marshmallow import INCLUDE
|
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
|
2020-03-04 18:40:25 +00:00
|
|
|
from crc.api.common import ApiError
|
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
|
|
|
|
2020-02-18 15:14: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
|
|
|
|
2020-03-04 18:40:25 +00:00
|
|
|
|
2020-02-07 16:34:44 +00:00
|
|
|
class Task(object):
|
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):
|
2020-03-02 18:14:03 +00:00
|
|
|
documentation = spiff_task.task_spec.documentation if hasattr(spiff_task.task_spec, "documentation") else ""
|
2019-12-19 16:58:51 +00:00
|
|
|
instance = cls(spiff_task.id,
|
|
|
|
spiff_task.task_spec.name,
|
2019-12-20 20:39:02 +00:00
|
|
|
spiff_task.task_spec.description,
|
2020-02-07 16:34:44 +00:00
|
|
|
spiff_task.task_spec.__class__.__name__,
|
2020-01-31 17:40:08 +00:00
|
|
|
spiff_task.get_state_name(),
|
2020-02-07 16:34:44 +00:00
|
|
|
None,
|
2020-02-10 21:19:23 +00:00
|
|
|
documentation,
|
2020-02-03 15:49:48 +00:00
|
|
|
spiff_task.data)
|
2019-12-19 16:58:51 +00:00
|
|
|
if hasattr(spiff_task.task_spec, "form"):
|
|
|
|
instance.form = spiff_task.task_spec.form
|
2020-02-25 17:01:25 +00:00
|
|
|
if documentation != "" and documentation is not None:
|
2020-02-29 22:22:38 +00:00
|
|
|
|
2020-02-25 17:01:25 +00:00
|
|
|
instance.process_documentation(documentation)
|
2019-12-19 16:58:51 +00:00
|
|
|
return instance
|
|
|
|
|
2020-02-25 17:01:25 +00:00
|
|
|
def process_documentation(self, documentation):
|
|
|
|
'''Runs markdown documentation through the Jinja2 processor to inject data
|
|
|
|
create loops, etc...'''
|
2020-02-29 22:22:38 +00:00
|
|
|
|
|
|
|
template = Template(documentation)
|
2020-03-04 18:40:25 +00:00
|
|
|
try:
|
|
|
|
self.documentation = template.render(**self.data)
|
|
|
|
except jinja2.exceptions.UndefinedError as ue:
|
|
|
|
raise ApiError(code="template_error", message="Error processing template for task %s: %s" %
|
|
|
|
(self.name, str(ue)), status_code=500)
|
2019-12-19 16:58:51 +00:00
|
|
|
|
|
|
|
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 = [
|
2020-02-18 15:14:03 +00:00
|
|
|
"id", "type", "label", "default_value", "options", "validation", "properties", "value"
|
2019-12-20 19:32:04 +00:00
|
|
|
]
|
|
|
|
|
2020-02-18 15:14:03 +00:00
|
|
|
default_value = marshmallow.fields.String(required=False, allow_none=True)
|
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)
|
2020-02-07 16:34:44 +00:00
|
|
|
form = marshmallow.fields.Nested(FormSchema, required=False, allow_none=True)
|
|
|
|
title = marshmallow.fields.String(required=False, allow_none=True)
|
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)
|
2020-02-07 16:34:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WorkflowApi(object):
|
2020-02-07 17:36:08 +00:00
|
|
|
def __init__(self, id, status, user_tasks, last_task, next_task, workflow_spec_id):
|
2020-02-07 16:34:44 +00:00
|
|
|
self.id = id
|
|
|
|
self.status = status
|
|
|
|
self.user_tasks = user_tasks
|
|
|
|
self.last_task = last_task
|
|
|
|
self.next_task = next_task
|
2020-02-07 17:36:08 +00:00
|
|
|
self.workflow_spec_id = workflow_spec_id
|
2020-02-07 16:34:44 +00:00
|
|
|
|
2020-02-18 15:14:03 +00:00
|
|
|
|
2020-02-07 16:34:44 +00:00
|
|
|
class WorkflowApiSchema(ma.Schema):
|
|
|
|
class Meta:
|
|
|
|
model = WorkflowApi
|
2020-02-07 17:36:08 +00:00
|
|
|
fields = ["id", "status", "user_tasks", "last_task", "next_task", "workflow_spec_id"]
|
2020-02-07 16:34:44 +00:00
|
|
|
unknown = INCLUDE
|
2020-02-18 15:14:03 +00:00
|
|
|
|
2020-02-07 16:34:44 +00:00
|
|
|
status = EnumField(WorkflowStatus)
|
|
|
|
user_tasks = marshmallow.fields.List(marshmallow.fields.Nested(TaskSchema, dump_only=True))
|
|
|
|
last_task = marshmallow.fields.Nested(TaskSchema, dump_only=True)
|
2020-02-10 21:19:23 +00:00
|
|
|
next_task = marshmallow.fields.Nested(TaskSchema, dump_only=True, required=False)
|
2020-02-07 16:34:44 +00:00
|
|
|
|
|
|
|
@marshmallow.post_load
|
|
|
|
def make_workflow(self, data, **kwargs):
|
2020-02-07 17:36:08 +00:00
|
|
|
return WorkflowApi(**data)
|