diff --git a/crc/api/file.py b/crc/api/file.py index 3b7d2b3d..3de74d09 100644 --- a/crc/api/file.py +++ b/crc/api/file.py @@ -12,13 +12,14 @@ from crc.services.file_service import FileService def get_files(workflow_spec_id=None, study_id=None, workflow_id=None, task_id=None, form_field_key=None): if all(v is None for v in [workflow_spec_id, study_id, workflow_id, task_id, form_field_key]): - return ApiErrorSchema().dump(ApiError('missing_parameter', - 'Please specify at least one of workflow_spec_id, study_id, ' - 'workflow_id, and task_id for this file in the HTTP parameters')), 400 + raise ApiError('missing_parameter', + 'Please specify at least one of workflow_spec_id, study_id, ' + 'workflow_id, and task_id for this file in the HTTP parameters') results = FileService.get_files(workflow_spec_id, study_id, workflow_id, task_id, form_field_key) return FileModelSchema(many=True).dump(results) + def get_reference_files(): results = FileService.get_files(is_reference=True) return FileModelSchema(many=True).dump(results) @@ -28,19 +29,21 @@ def add_file(workflow_spec_id=None, study_id=None, workflow_id=None, task_id=Non all_none = all(v is None for v in [workflow_spec_id, study_id, workflow_id, task_id, form_field_key]) missing_some = (workflow_spec_id is None) and (None in [study_id, workflow_id, task_id, form_field_key]) if all_none or missing_some: - return ApiErrorSchema().dump(ApiError('missing_parameter', - 'Please specify either a workflow_spec_id or all 3 of study_id, ' - 'workflow_id, task_id and field_id for this file in the HTTP parameters')), 404 + raise ApiError('missing_parameter', + 'Please specify either a workflow_spec_id or all 3 of study_id, ' + 'workflow_id, task_id and field_id for this file in the HTTP parameters') if 'file' not in connexion.request.files: - return ApiErrorSchema().dump(ApiError('invalid_file', - 'Expected a file named "file" in the multipart form request')), 404 + raise ApiError('invalid_file', + 'Expected a file named "file" in the multipart form request') file = connexion.request.files['file'] if workflow_spec_id: workflow_spec = session.query(WorkflowSpecModel).filter_by(id=workflow_spec_id).first() - file_model = FileService.add_workflow_spec_file(workflow_spec, file.filename, file.content_type, file.stream.read()) + file_model = FileService.add_workflow_spec_file(workflow_spec, file.filename, file.content_type, + file.stream.read()) else: - file_model = FileService.add_form_field_file(study_id, workflow_id, task_id, form_field_key, file.filename, file.content_type, file.stream.read()) + file_model = FileService.add_form_field_file(study_id, workflow_id, task_id, form_field_key, file.filename, + file.content_type, file.stream.read()) return FileModelSchema().dump(file_model) @@ -70,7 +73,6 @@ def set_reference_file(name): "The file you uploaded has an extension '%s', but it should have an extension of '%s' " % (file_extension, name_extension)) - file_models = FileService.get_files(name=name, is_reference=True) if len(file_models) == 0: file_model = FileService.add_reference_file(name, file.content_type, file.stream.read()) @@ -85,7 +87,7 @@ def update_file_data(file_id): file_model = session.query(FileModel).filter_by(id=file_id).with_for_update().first() file = connexion.request.files['file'] if file_model is None: - return ApiErrorSchema().dump(ApiError('no_such_file', 'The file id you provided does not exist')), 404 + raise ApiError('no_such_file', 'The file id you provided does not exist') file_model = FileService.update_file(file_model, file.stream.read(), file.content_type) return FileModelSchema().dump(file_model) @@ -93,7 +95,7 @@ def update_file_data(file_id): def get_file_data(file_id): file_data = FileService.get_file_data(file_id) if file_data is None: - return ApiErrorSchema().dump(ApiError('no_such_file', 'The file id you provided does not exist')), 404 + raise ApiError('no_such_file', 'The file id you provided does not exist') return send_file( io.BytesIO(file_data.data), attachment_filename=file_data.file_model.name, @@ -105,20 +107,18 @@ def get_file_data(file_id): def get_file_info(file_id): file_model = session.query(FileModel).filter_by(id=file_id).with_for_update().first() if file_model is None: - return ApiErrorSchema().dump(ApiError('no_such_file', 'The file id you provided does not exist')), 404 + raise ApiError('no_such_file', 'The file id you provided does not exist', status_code=404) return FileModelSchema().dump(file_model) def update_file_info(file_id, body): if file_id is None: - error = ApiError('unknown_file', 'Please provide a valid File ID.') - return ApiErrorSchema.dump(error), 404 + raise ApiError('unknown_file', 'Please provide a valid File ID.') file_model = session.query(FileModel).filter_by(id=file_id).first() if file_model is None: - error = ApiError('unknown_file_model', 'The file_model "' + file_id + '" is not recognized.') - return ApiErrorSchema.dump(error), 404 + raise ApiError('unknown_file_model', 'The file_model "' + file_id + '" is not recognized.') file_model = FileModelSchema().load(body, session=session) session.add(file_model) diff --git a/crc/api/study.py b/crc/api/study.py index f66c4fd0..47cd5ca1 100644 --- a/crc/api/study.py +++ b/crc/api/study.py @@ -66,14 +66,12 @@ def __add_study_workflows_from_status(study_id, status_spec): @auth.login_required 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 + raise ApiError('unknown_study', 'Please provide a valid Study ID.') study = session.query(StudyModel).filter_by(id=study_id).first() if study is None: - error = ApiError('unknown_study', 'The study "' + study_id + '" is not recognized.') - return ApiErrorSchema.dump(error), 404 + raise ApiError('unknown_study', 'The study "' + study_id + '" is not recognized.') schema = StudyModelSchema() study = schema.load(body, session=session, instance=study, partial=True) @@ -207,8 +205,7 @@ def get_study_workflows(study_id): def add_workflow_to_study(study_id, body): workflow_spec_model: WorkflowSpecModel = session.query(WorkflowSpecModel).filter_by(id=body["id"]).first() if workflow_spec_model is None: - error = ApiError('unknown_spec', 'The specification "' + body['id'] + '" is not recognized.') - return ApiErrorSchema.dump(error), 404 + raise ApiError('unknown_spec', 'The specification "' + body['id'] + '" is not recognized.') processor = WorkflowProcessor.create(study_id, workflow_spec_model.id) # If workflow spec is a status spec, update the study status spec diff --git a/crc/api/workflow.py b/crc/api/workflow.py index 87535367..e78a1ecf 100644 --- a/crc/api/workflow.py +++ b/crc/api/workflow.py @@ -27,14 +27,12 @@ def add_workflow_specification(body): @auth.login_required def get_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 + raise ApiError('unknown_spec', 'Please provide a valid Workflow Specification ID.') 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 + raise ApiError('unknown_spec', 'The Workflow Specification "' + spec_id + '" is not recognized.') return WorkflowSpecModelSchema().dump(spec) @@ -58,14 +56,12 @@ def update_workflow_specification(spec_id, body): @auth.login_required 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 + raise ApiError('unknown_spec', 'Please provide a valid Workflow Specification ID.') 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 + raise ApiError('unknown_spec', 'The Workflow Specification "' + spec_id + '" is not recognized.') # Delete all items in the database related to the deleted workflow spec. files = session.query(FileModel).filter_by(workflow_spec_id=spec_id).all()