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

114 lines
4.1 KiB
Python
Raw Normal View History

import uuid
from flask import json
2020-01-24 16:52:52 +00:00
from crc.api.file import delete_file
from crc import session
2020-01-21 20:24:17 +00:00
from crc.api.common import ApiError, ApiErrorSchema
from crc.models.workflow import WorkflowModel, WorkflowSpecModelSchema, WorkflowSpecModel, \
Task, TaskSchema, WorkflowApiSchema, WorkflowApi
2020-01-24 16:52:52 +00:00
from crc.workflow_processor import WorkflowProcessor
from crc.models.file import FileModel
def all_specifications():
schema = WorkflowSpecModelSchema(many=True)
return schema.dump(session.query(WorkflowSpecModel).all())
def add_workflow_specification(body):
new_spec = WorkflowSpecModelSchema().load(body, session=session)
session.add(new_spec)
session.commit()
return WorkflowSpecModelSchema().dump(new_spec)
def get_workflow_specification(spec_id):
if spec_id is None:
2020-01-23 21:05:09 +00:00
error = ApiError('unknown_spec', 'Please provide a valid Workflow Specification ID.')
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-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.')
return ApiErrorSchema.dump(error), 404
return WorkflowSpecModelSchema().dump(spec)
def update_workflow_specification(spec_id, body):
2020-01-21 20:24:17 +00:00
spec = WorkflowSpecModelSchema().load(body, session=session)
spec.id = spec_id
2020-01-21 20:24:17 +00:00
session.add(spec)
session.commit()
2020-01-21 20:24:17 +00:00
return WorkflowSpecModelSchema().dump(spec)
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
# 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()
session.query(WorkflowSpecModel).filter_by(id=spec_id).delete()
2020-01-23 21:05:09 +00:00
session.commit()
def __get_workflow_api_model(workflow_model: WorkflowModel, processor: WorkflowProcessor):
spiff_tasks = processor.get_all_user_tasks()
user_tasks = map(Task.from_spiff, spiff_tasks)
return WorkflowApi(
id=workflow_model.id,
status=workflow_model.status,
last_task=Task.from_spiff(processor.bpmn_workflow.last_task),
next_task=Task.from_spiff(processor.next_task()),
user_tasks=user_tasks,
workflow_spec_id=workflow_model.workflow_spec_id
)
def get_workflow(workflow_id):
schema = WorkflowApiSchema()
workflow_model = session.query(WorkflowModel).filter_by(id=workflow_id).first()
processor = WorkflowProcessor(workflow_model.workflow_spec_id,
workflow_model.bpmn_workflow_json)
return schema.dump(__get_workflow_api_model(workflow_model, processor))
2019-12-30 18:15:39 +00:00
def delete(workflow_id):
session.query(WorkflowModel).filter_by(id=workflow_id).delete()
session.commit()
def get_task(workflow_id, task_id):
workflow = session.query(WorkflowModel).filter_by(id=workflow_id).first()
return workflow.bpmn_workflow().get_task(task_id)
def update_task(workflow_id, task_id, body):
workflow_model = session.query(WorkflowModel).filter_by(id=workflow_id).first()
processor = WorkflowProcessor(workflow_model.workflow_spec_id, workflow_model.bpmn_workflow_json)
task_id = uuid.UUID(task_id)
task = processor.bpmn_workflow.get_task(task_id)
task.data = body
processor.complete_task(task)
processor.do_engine_steps()
workflow_model.last_completed_task_id = task.id
workflow_model.bpmn_workflow_json = processor.serialize()
session.add(workflow_model)
session.commit()
return WorkflowApiSchema().dump(__get_workflow_api_model(workflow_model, processor)
)