2019-12-18 19:02:17 +00:00
|
|
|
from connexion import NoContent
|
|
|
|
|
2020-01-14 16:45:12 +00:00
|
|
|
from crc import session
|
2019-12-27 18:50:03 +00:00
|
|
|
from crc.api.common import ApiError, ApiErrorSchema
|
2020-01-24 16:52:52 +00:00
|
|
|
from crc.models.study import StudyModelSchema, StudyModel
|
|
|
|
from crc.models.workflow import WorkflowModel, WorkflowModelSchema, WorkflowSpecModel
|
|
|
|
from crc.workflow_processor import WorkflowProcessor
|
2019-12-18 19:02:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def all_studies():
|
2019-12-27 18:50:03 +00:00
|
|
|
# todo: Limit returned studies to a user
|
2020-01-03 16:44:24 +00:00
|
|
|
schema = StudyModelSchema(many=True)
|
2020-01-14 16:45:12 +00:00
|
|
|
return schema.dump(session.query(StudyModel).all())
|
2019-12-18 19:02:17 +00:00
|
|
|
|
|
|
|
|
2020-01-03 16:44:24 +00:00
|
|
|
def add_study(body):
|
2020-01-14 16:45:12 +00:00
|
|
|
study = StudyModelSchema().load(body, session=session)
|
|
|
|
session.add(study)
|
|
|
|
session.commit()
|
2020-01-30 14:11:17 +00:00
|
|
|
# FIXME: We need to ask the protocol builder what workflows to add to the study, not just add them all.
|
|
|
|
for spec in session.query(WorkflowSpecModel).all():
|
|
|
|
workflow = __get_workflow_instance(study.id, spec)
|
|
|
|
session.add(workflow)
|
|
|
|
session.commit()
|
2020-01-03 16:44:24 +00:00
|
|
|
return StudyModelSchema().dump(study)
|
|
|
|
|
|
|
|
|
|
|
|
def update_study(study_id, body):
|
|
|
|
if study_id is None:
|
|
|
|
error = ApiError('unknown_study', 'Please provide a valid Study ID.')
|
|
|
|
return ApiErrorSchema.dump(error), 404
|
|
|
|
|
2020-01-14 16:45:12 +00:00
|
|
|
study = session.query(StudyModel).filter_by(id=study_id).first()
|
2020-01-03 16:44:24 +00:00
|
|
|
|
|
|
|
if study is None:
|
|
|
|
error = ApiError('unknown_study', 'The study "' + study_id + '" is not recognized.')
|
|
|
|
return ApiErrorSchema.dump(error), 404
|
|
|
|
|
2020-01-14 16:45:12 +00:00
|
|
|
study = StudyModelSchema().load(body, session=session)
|
|
|
|
session.add(study)
|
|
|
|
session.commit()
|
2020-01-03 16:44:24 +00:00
|
|
|
return StudyModelSchema().dump(study)
|
|
|
|
|
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
def get_study(study_id):
|
2020-01-14 16:45:12 +00:00
|
|
|
study = session.query(StudyModel).filter_by(id=study_id).first()
|
2020-01-03 16:44:24 +00:00
|
|
|
schema = StudyModelSchema()
|
2019-12-18 19:02:17 +00:00
|
|
|
if study is None:
|
|
|
|
return NoContent, 404
|
|
|
|
return schema.dump(study)
|
|
|
|
|
|
|
|
|
|
|
|
def post_update_study_from_protocol_builder(study_id):
|
2019-12-27 18:50:03 +00:00
|
|
|
# todo: Actually get data from an external service here
|
2019-12-18 19:02:17 +00:00
|
|
|
return NoContent, 304
|
|
|
|
|
|
|
|
|
|
|
|
def get_study_workflows(study_id):
|
2020-01-14 16:45:12 +00:00
|
|
|
workflows = session.query(WorkflowModel).filter_by(study_id=study_id).all()
|
2020-01-03 16:44:24 +00:00
|
|
|
schema = WorkflowModelSchema(many=True)
|
2019-12-18 19:02:17 +00:00
|
|
|
return schema.dump(workflows)
|
|
|
|
|
|
|
|
|
|
|
|
def add_workflow_to_study(study_id, body):
|
2020-01-14 16:45:12 +00:00
|
|
|
workflow_spec_model = session.query(WorkflowSpecModel).filter_by(id=body["id"]).first()
|
2019-12-18 19:02:17 +00:00
|
|
|
if workflow_spec_model is None:
|
|
|
|
error = ApiError('unknown_spec', 'The specification "' + body['id'] + '" is not recognized.')
|
|
|
|
return ApiErrorSchema.dump(error), 404
|
2020-01-30 14:11:17 +00:00
|
|
|
workflow = __get_workflow_instance(study_id, workflow_spec_model)
|
|
|
|
session.add(workflow)
|
|
|
|
session.commit()
|
|
|
|
return WorkflowModelSchema().dump(workflow)
|
2019-12-18 19:02:17 +00:00
|
|
|
|
2020-01-30 14:11:17 +00:00
|
|
|
def __get_workflow_instance(study_id, workflow_spec_model):
|
2019-12-18 20:22:46 +00:00
|
|
|
processor = WorkflowProcessor.create(workflow_spec_model.id)
|
|
|
|
workflow = WorkflowModel(bpmn_workflow_json=processor.serialize(),
|
|
|
|
status=processor.get_status(),
|
|
|
|
study_id=study_id,
|
|
|
|
workflow_spec_id=workflow_spec_model.id)
|
2020-01-30 14:11:17 +00:00
|
|
|
return workflow
|