Just raise the ApiError, no need to marshal it.

This commit is contained in:
Dan Funk 2020-03-23 12:22:26 -04:00
parent e2331330e5
commit 967ac65d0e
3 changed files with 25 additions and 32 deletions

View File

@ -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): 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]): 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', raise ApiError('missing_parameter',
'Please specify at least one of workflow_spec_id, study_id, ' 'Please specify at least one of workflow_spec_id, study_id, '
'workflow_id, and task_id for this file in the HTTP parameters')), 400 '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) results = FileService.get_files(workflow_spec_id, study_id, workflow_id, task_id, form_field_key)
return FileModelSchema(many=True).dump(results) return FileModelSchema(many=True).dump(results)
def get_reference_files(): def get_reference_files():
results = FileService.get_files(is_reference=True) results = FileService.get_files(is_reference=True)
return FileModelSchema(many=True).dump(results) 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]) 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]) 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: if all_none or missing_some:
return ApiErrorSchema().dump(ApiError('missing_parameter', raise ApiError('missing_parameter',
'Please specify either a workflow_spec_id or all 3 of study_id, ' '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 'workflow_id, task_id and field_id for this file in the HTTP parameters')
if 'file' not in connexion.request.files: if 'file' not in connexion.request.files:
return ApiErrorSchema().dump(ApiError('invalid_file', raise ApiError('invalid_file',
'Expected a file named "file" in the multipart form request')), 404 'Expected a file named "file" in the multipart form request')
file = connexion.request.files['file'] file = connexion.request.files['file']
if workflow_spec_id: if workflow_spec_id:
workflow_spec = session.query(WorkflowSpecModel).filter_by(id=workflow_spec_id).first() 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: 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) 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' " % "The file you uploaded has an extension '%s', but it should have an extension of '%s' " %
(file_extension, name_extension)) (file_extension, name_extension))
file_models = FileService.get_files(name=name, is_reference=True) file_models = FileService.get_files(name=name, is_reference=True)
if len(file_models) == 0: if len(file_models) == 0:
file_model = FileService.add_reference_file(name, file.content_type, file.stream.read()) 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_model = session.query(FileModel).filter_by(id=file_id).with_for_update().first()
file = connexion.request.files['file'] file = connexion.request.files['file']
if file_model is None: 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) file_model = FileService.update_file(file_model, file.stream.read(), file.content_type)
return FileModelSchema().dump(file_model) return FileModelSchema().dump(file_model)
@ -93,7 +95,7 @@ def update_file_data(file_id):
def get_file_data(file_id): def get_file_data(file_id):
file_data = FileService.get_file_data(file_id) file_data = FileService.get_file_data(file_id)
if file_data is None: 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( return send_file(
io.BytesIO(file_data.data), io.BytesIO(file_data.data),
attachment_filename=file_data.file_model.name, attachment_filename=file_data.file_model.name,
@ -105,20 +107,18 @@ def get_file_data(file_id):
def get_file_info(file_id): def get_file_info(file_id):
file_model = session.query(FileModel).filter_by(id=file_id).with_for_update().first() file_model = session.query(FileModel).filter_by(id=file_id).with_for_update().first()
if file_model is None: 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) return FileModelSchema().dump(file_model)
def update_file_info(file_id, body): def update_file_info(file_id, body):
if file_id is None: if file_id is None:
error = ApiError('unknown_file', 'Please provide a valid File ID.') raise ApiError('unknown_file', 'Please provide a valid File ID.')
return ApiErrorSchema.dump(error), 404
file_model = session.query(FileModel).filter_by(id=file_id).first() file_model = session.query(FileModel).filter_by(id=file_id).first()
if file_model is None: if file_model is None:
error = ApiError('unknown_file_model', 'The file_model "' + file_id + '" is not recognized.') raise ApiError('unknown_file_model', 'The file_model "' + file_id + '" is not recognized.')
return ApiErrorSchema.dump(error), 404
file_model = FileModelSchema().load(body, session=session) file_model = FileModelSchema().load(body, session=session)
session.add(file_model) session.add(file_model)

View File

@ -66,14 +66,12 @@ def __add_study_workflows_from_status(study_id, status_spec):
@auth.login_required @auth.login_required
def update_study(study_id, body): def update_study(study_id, body):
if study_id is None: if study_id is None:
error = ApiError('unknown_study', 'Please provide a valid Study ID.') raise ApiError('unknown_study', 'Please provide a valid Study ID.')
return ApiErrorSchema.dump(error), 404
study = session.query(StudyModel).filter_by(id=study_id).first() study = session.query(StudyModel).filter_by(id=study_id).first()
if study is None: if study is None:
error = ApiError('unknown_study', 'The study "' + study_id + '" is not recognized.') raise ApiError('unknown_study', 'The study "' + study_id + '" is not recognized.')
return ApiErrorSchema.dump(error), 404
schema = StudyModelSchema() schema = StudyModelSchema()
study = schema.load(body, session=session, instance=study, partial=True) 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): def add_workflow_to_study(study_id, body):
workflow_spec_model: WorkflowSpecModel = session.query(WorkflowSpecModel).filter_by(id=body["id"]).first() workflow_spec_model: WorkflowSpecModel = session.query(WorkflowSpecModel).filter_by(id=body["id"]).first()
if workflow_spec_model is None: if workflow_spec_model is None:
error = ApiError('unknown_spec', 'The specification "' + body['id'] + '" is not recognized.') raise ApiError('unknown_spec', 'The specification "' + body['id'] + '" is not recognized.')
return ApiErrorSchema.dump(error), 404
processor = WorkflowProcessor.create(study_id, workflow_spec_model.id) processor = WorkflowProcessor.create(study_id, workflow_spec_model.id)
# If workflow spec is a status spec, update the study status spec # If workflow spec is a status spec, update the study status spec

View File

@ -27,14 +27,12 @@ def add_workflow_specification(body):
@auth.login_required @auth.login_required
def get_workflow_specification(spec_id): def get_workflow_specification(spec_id):
if spec_id is None: if spec_id is None:
error = ApiError('unknown_spec', 'Please provide a valid Workflow Specification ID.') raise 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() spec: WorkflowSpecModel = session.query(WorkflowSpecModel).filter_by(id=spec_id).first()
if spec is None: if spec is None:
error = ApiError('unknown_spec', 'The Workflow Specification "' + spec_id + '" is not recognized.') raise ApiError('unknown_spec', 'The Workflow Specification "' + spec_id + '" is not recognized.')
return ApiErrorSchema.dump(error), 404
return WorkflowSpecModelSchema().dump(spec) return WorkflowSpecModelSchema().dump(spec)
@ -58,14 +56,12 @@ def update_workflow_specification(spec_id, body):
@auth.login_required @auth.login_required
def delete_workflow_specification(spec_id): def delete_workflow_specification(spec_id):
if spec_id is None: if spec_id is None:
error = ApiError('unknown_spec', 'Please provide a valid Workflow Specification ID.') raise 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() spec: WorkflowSpecModel = session.query(WorkflowSpecModel).filter_by(id=spec_id).first()
if spec is None: if spec is None:
error = ApiError('unknown_spec', 'The Workflow Specification "' + spec_id + '" is not recognized.') raise 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. # Delete all items in the database related to the deleted workflow spec.
files = session.query(FileModel).filter_by(workflow_spec_id=spec_id).all() files = session.query(FileModel).filter_by(workflow_spec_id=spec_id).all()