2020-01-14 16:02:44 +00:00
|
|
|
import uuid
|
|
|
|
|
2020-03-11 19:16:42 +00:00
|
|
|
from crc.api.stats import update_workflow_stats, log_task_complete
|
2020-03-11 19:27:22 +00:00
|
|
|
from crc import session, auth
|
2020-01-21 20:24:17 +00:00
|
|
|
from crc.api.common import ApiError, ApiErrorSchema
|
2020-03-11 18:28:53 +00:00
|
|
|
from crc.api.file import delete_file
|
2020-03-05 16:18:20 +00:00
|
|
|
from crc.models.api_models import Task, WorkflowApi, WorkflowApiSchema
|
2020-03-11 18:28:53 +00:00
|
|
|
from crc.models.file import FileModel
|
2020-03-05 16:18:20 +00:00
|
|
|
from crc.models.workflow import WorkflowModel, WorkflowSpecModelSchema, WorkflowSpecModel
|
2020-02-10 21:19:23 +00:00
|
|
|
from crc.services.workflow_processor import WorkflowProcessor
|
2019-12-27 18:50:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def all_specifications():
|
2020-01-03 16:44:24 +00:00
|
|
|
schema = WorkflowSpecModelSchema(many=True)
|
2020-01-14 16:45:12 +00:00
|
|
|
return schema.dump(session.query(WorkflowSpecModel).all())
|
2019-12-27 18:50:03 +00:00
|
|
|
|
|
|
|
|
2020-03-11 19:27:22 +00:00
|
|
|
@auth.login_required
|
2020-01-14 02:43:09 +00:00
|
|
|
def add_workflow_specification(body):
|
2020-01-14 19:10:53 +00:00
|
|
|
new_spec = WorkflowSpecModelSchema().load(body, session=session)
|
|
|
|
session.add(new_spec)
|
|
|
|
session.commit()
|
|
|
|
return WorkflowSpecModelSchema().dump(new_spec)
|
|
|
|
|
|
|
|
|
2020-03-11 19:27:22 +00:00
|
|
|
@auth.login_required
|
2020-01-28 15:14:06 +00:00
|
|
|
def get_workflow_specification(spec_id):
|
2020-01-14 19:10:53 +00:00
|
|
|
if spec_id is None:
|
2020-01-23 21:05:09 +00:00
|
|
|
error = ApiError('unknown_spec', 'Please provide a valid Workflow Specification ID.')
|
2020-01-14 19:10:53 +00:00
|
|
|
return ApiErrorSchema.dump(error), 404
|
|
|
|
|
2020-01-21 20:24:17 +00:00
|
|
|
spec: WorkflowSpecModel = session.query(WorkflowSpecModel).filter_by(id=spec_id).first()
|
2020-01-14 19:10:53 +00:00
|
|
|
|
2020-01-21 20:24:17 +00:00
|
|
|
if spec is None:
|
2020-01-23 21:05:09 +00:00
|
|
|
error = ApiError('unknown_spec', 'The Workflow Specification "' + spec_id + '" is not recognized.')
|
2020-01-14 19:10:53 +00:00
|
|
|
return ApiErrorSchema.dump(error), 404
|
|
|
|
|
2020-01-28 15:14:06 +00:00
|
|
|
return WorkflowSpecModelSchema().dump(spec)
|
|
|
|
|
|
|
|
|
2020-03-11 19:27:22 +00:00
|
|
|
@auth.login_required
|
2020-01-28 15:14:06 +00:00
|
|
|
def update_workflow_specification(spec_id, body):
|
2020-01-21 20:24:17 +00:00
|
|
|
spec = WorkflowSpecModelSchema().load(body, session=session)
|
2020-01-28 15:14:06 +00:00
|
|
|
spec.id = spec_id
|
2020-01-21 20:24:17 +00:00
|
|
|
session.add(spec)
|
2020-01-14 16:45:12 +00:00
|
|
|
session.commit()
|
2020-01-21 20:24:17 +00:00
|
|
|
return WorkflowSpecModelSchema().dump(spec)
|
2020-01-14 02:43:09 +00:00
|
|
|
|
|
|
|
|
2020-03-11 19:27:22 +00:00
|
|
|
@auth.login_required
|
2020-01-23 21:05:09 +00:00
|
|
|
def delete_workflow_specification(spec_id):
|
|
|
|
if spec_id is None:
|
|
|
|
error = ApiError('unknown_spec', 'Please provide a valid Workflow Specification ID.')
|
|
|
|
return ApiErrorSchema.dump(error), 404
|
|
|
|
|
|
|
|
spec: WorkflowSpecModel = session.query(WorkflowSpecModel).filter_by(id=spec_id).first()
|
|
|
|
|
|
|
|
if spec is None:
|
|
|
|
error = ApiError('unknown_spec', 'The Workflow Specification "' + spec_id + '" is not recognized.')
|
|
|
|
return ApiErrorSchema.dump(error), 404
|
|
|
|
|
2020-01-24 14:26:17 +00:00
|
|
|
# Delete all items in the database related to the deleted workflow spec.
|
|
|
|
files = session.query(FileModel).filter_by(workflow_spec_id=spec_id).all()
|
|
|
|
for file in files:
|
|
|
|
delete_file(file.id)
|
|
|
|
|
|
|
|
session.query(WorkflowModel).filter_by(workflow_spec_id=spec_id).delete()
|
2020-02-04 20:44:06 +00:00
|
|
|
session.query(WorkflowSpecModel).filter_by(id=spec_id).delete()
|
2020-01-23 21:05:09 +00:00
|
|
|
session.commit()
|
|
|
|
|
|
|
|
|
2020-02-10 21:19:23 +00:00
|
|
|
def __get_workflow_api_model(processor: WorkflowProcessor):
|
2020-02-07 16:34:44 +00:00
|
|
|
spiff_tasks = processor.get_all_user_tasks()
|
2020-03-11 19:16:42 +00:00
|
|
|
user_tasks = list(map(Task.from_spiff, spiff_tasks))
|
2020-02-10 21:19:23 +00:00
|
|
|
workflow_api = WorkflowApi(
|
|
|
|
id=processor.get_workflow_id(),
|
|
|
|
status=processor.get_status(),
|
2020-02-07 17:36:08 +00:00
|
|
|
last_task=Task.from_spiff(processor.bpmn_workflow.last_task),
|
2020-02-10 21:19:23 +00:00
|
|
|
next_task=None,
|
2020-02-07 17:36:08 +00:00
|
|
|
user_tasks=user_tasks,
|
2020-03-05 16:18:20 +00:00
|
|
|
workflow_spec_id=processor.workflow_spec_id,
|
2020-03-05 21:45:44 +00:00
|
|
|
spec_version=processor.get_spec_version(),
|
|
|
|
is_latest_spec=processor.get_spec_version() == processor.get_latest_version_string(processor.workflow_spec_id)
|
2020-02-07 17:36:08 +00:00
|
|
|
)
|
2020-03-04 18:40:25 +00:00
|
|
|
if processor.next_task():
|
2020-02-11 16:11:21 +00:00
|
|
|
workflow_api.next_task = Task.from_spiff(processor.next_task())
|
2020-02-10 21:19:23 +00:00
|
|
|
return workflow_api
|
2020-02-07 16:34:44 +00:00
|
|
|
|
2020-03-05 16:18:20 +00:00
|
|
|
|
2020-03-11 19:27:22 +00:00
|
|
|
@auth.login_required
|
2020-03-05 21:45:44 +00:00
|
|
|
def get_workflow(workflow_id, soft_reset=False, hard_reset=False):
|
2020-02-07 16:34:44 +00:00
|
|
|
workflow_model = session.query(WorkflowModel).filter_by(id=workflow_id).first()
|
2020-03-05 21:45:44 +00:00
|
|
|
processor = WorkflowProcessor(workflow_model, soft_reset=soft_reset, hard_reset=hard_reset)
|
2020-03-11 19:16:42 +00:00
|
|
|
|
|
|
|
workflow_api_model = __get_workflow_api_model(processor)
|
|
|
|
update_workflow_stats(workflow_model, workflow_api_model)
|
|
|
|
return WorkflowApiSchema().dump(workflow_api_model)
|
2019-12-27 18:50:03 +00:00
|
|
|
|
2019-12-31 16:31:30 +00:00
|
|
|
|
2020-03-11 19:27:22 +00:00
|
|
|
@auth.login_required
|
2019-12-30 18:15:39 +00:00
|
|
|
def delete(workflow_id):
|
2020-01-14 16:45:12 +00:00
|
|
|
session.query(WorkflowModel).filter_by(id=workflow_id).delete()
|
|
|
|
session.commit()
|
2019-12-27 18:50:03 +00:00
|
|
|
|
2020-03-10 19:46:14 +00:00
|
|
|
|
2020-03-11 19:27:22 +00:00
|
|
|
@auth.login_required
|
2019-12-27 18:50:03 +00:00
|
|
|
def get_task(workflow_id, task_id):
|
2020-01-14 16:45:12 +00:00
|
|
|
workflow = session.query(WorkflowModel).filter_by(id=workflow_id).first()
|
2019-12-27 18:50:03 +00:00
|
|
|
return workflow.bpmn_workflow().get_task(task_id)
|
|
|
|
|
|
|
|
|
2020-03-11 19:27:22 +00:00
|
|
|
@auth.login_required
|
2020-01-14 16:02:44 +00:00
|
|
|
def update_task(workflow_id, task_id, body):
|
2020-02-07 16:34:44 +00:00
|
|
|
workflow_model = session.query(WorkflowModel).filter_by(id=workflow_id).first()
|
2020-03-05 18:25:28 +00:00
|
|
|
processor = WorkflowProcessor(workflow_model)
|
2020-01-14 16:02:44 +00:00
|
|
|
task_id = uuid.UUID(task_id)
|
|
|
|
task = processor.bpmn_workflow.get_task(task_id)
|
|
|
|
task.data = body
|
|
|
|
processor.complete_task(task)
|
2020-02-03 20:15:36 +00:00
|
|
|
processor.do_engine_steps()
|
2020-02-07 16:34:44 +00:00
|
|
|
workflow_model.last_completed_task_id = task.id
|
|
|
|
workflow_model.bpmn_workflow_json = processor.serialize()
|
|
|
|
session.add(workflow_model)
|
2020-01-14 16:45:12 +00:00
|
|
|
session.commit()
|
2020-03-11 01:29:57 +00:00
|
|
|
|
2020-03-11 18:28:53 +00:00
|
|
|
workflow_api_model = __get_workflow_api_model(processor)
|
2020-03-11 01:29:57 +00:00
|
|
|
update_workflow_stats(workflow_model, workflow_api_model)
|
|
|
|
log_task_complete(workflow_model, task_id)
|
|
|
|
return WorkflowApiSchema().dump(workflow_api_model)
|