2019-12-27 13:50:03 -05:00
|
|
|
import io
|
2021-09-07 12:09:30 -04:00
|
|
|
from datetime import datetime
|
2019-12-27 13:50:03 -05:00
|
|
|
|
|
|
|
import connexion
|
|
|
|
from flask import send_file
|
|
|
|
|
2020-01-14 11:45:12 -05:00
|
|
|
from crc import session
|
2020-04-15 11:13:32 -04:00
|
|
|
from crc.api.common import ApiError
|
2021-05-05 11:30:08 -04:00
|
|
|
from crc.api.user import verify_token
|
2022-01-20 15:22:47 -05:00
|
|
|
from crc.models.file import FileSchema, FileModel, File, FileModelSchema, FileDataModel
|
2021-07-06 13:10:20 -04:00
|
|
|
from crc.services.document_service import DocumentService
|
2022-02-02 12:59:56 -05:00
|
|
|
from crc.services.user_file_service import UserFileService
|
2019-12-27 13:50:03 -05:00
|
|
|
|
2022-01-11 15:30:22 -05:00
|
|
|
|
2020-05-28 20:03:50 -04:00
|
|
|
def to_file_api(file_model):
|
2022-02-02 12:59:56 -05:00
|
|
|
file_data_model = UserFileService.get_file_data(file_model.id)
|
2022-01-07 15:34:51 -05:00
|
|
|
return File.from_models(file_model, file_data_model,
|
2021-07-06 13:10:20 -04:00
|
|
|
DocumentService.get_dictionary())
|
2020-05-28 20:03:50 -04:00
|
|
|
|
|
|
|
|
2022-01-24 14:43:36 -05:00
|
|
|
def get_files(workflow_id=None, form_field_key=None, study_id=None):
|
2022-01-20 14:57:32 -05:00
|
|
|
if workflow_id is None:
|
2020-03-23 12:22:26 -04:00
|
|
|
raise ApiError('missing_parameter',
|
2022-01-20 14:57:32 -05:00
|
|
|
'Please specify a workflow_id with an optional form_field_key')
|
2019-12-27 13:50:03 -05:00
|
|
|
|
2021-04-12 12:23:33 -04:00
|
|
|
if study_id is not None:
|
2022-02-02 12:59:56 -05:00
|
|
|
file_models = UserFileService.get_files_for_study(study_id=study_id, irb_doc_code=form_field_key)
|
2021-04-12 12:23:33 -04:00
|
|
|
else:
|
2022-02-02 12:59:56 -05:00
|
|
|
file_models = UserFileService.get_files(workflow_id=workflow_id,
|
2022-01-20 14:57:32 -05:00
|
|
|
irb_doc_code=form_field_key)
|
2020-05-28 20:03:50 -04:00
|
|
|
|
|
|
|
files = (to_file_api(model) for model in file_models)
|
|
|
|
return FileSchema(many=True).dump(files)
|
2020-02-03 21:56:18 -05:00
|
|
|
|
2022-01-20 14:57:32 -05:00
|
|
|
|
2022-01-11 15:30:22 -05:00
|
|
|
def add_file(workflow_id=None, task_spec_name=None, form_field_key=None):
|
2020-02-10 16:19:23 -05:00
|
|
|
file = connexion.request.files['file']
|
A major refactor of how we search and store files, as there was a lot of confusing bits in here.
From an API point of view you can do the following (and only the following)
/files?workflow_spec_id=x
* You can find all files associated with a workflow_spec_id, and add a file with a workflow_spec_id
/files?workflow_id=x
* You can find all files associated with a workflow_id, and add a file that is directly associated with the workflow
/files?workflow_id=x&form_field_key=y
* You can find all files associated with a form element on a running workflow, and add a new file.
Note: you can add multiple files to the same form_field_key, IF they have different file names. If the same name, the original file is archived,
and the new file takes its place.
The study endpoints always return a list of the file metadata associated with the study. Removed /studies-files, but there is an
endpoint called
/studies/all - that returns all the studies in the system, and does include their files.
On a deeper level:
The File model no longer contains:
- study_id,
- task_id,
- form_field_key
Instead, if the file is associated with workflow - then that is the one way it is connected to the study, and we use this relationship to find files for a study.
A file is never associated with a task_id, as these change when the workflow is reloaded.
The form_field_key must match the irb_doc_code, so when requesting files for a form field, we just look up the irb_doc_code.
2020-05-28 08:27:26 -04:00
|
|
|
if workflow_id:
|
|
|
|
if form_field_key is None:
|
|
|
|
raise ApiError('invalid_workflow_file',
|
|
|
|
'When adding a workflow related file, you must specify a form_field_key')
|
2021-08-26 10:37:05 -04:00
|
|
|
if task_spec_name is None:
|
|
|
|
raise ApiError('invalid_workflow_file',
|
|
|
|
'When adding a workflow related file, you must specify a task_spec_name')
|
2022-02-02 12:59:56 -05:00
|
|
|
file_model = UserFileService.add_workflow_file(workflow_id=workflow_id, irb_doc_code=form_field_key,
|
2021-08-26 10:37:05 -04:00
|
|
|
task_spec_name=task_spec_name,
|
A major refactor of how we search and store files, as there was a lot of confusing bits in here.
From an API point of view you can do the following (and only the following)
/files?workflow_spec_id=x
* You can find all files associated with a workflow_spec_id, and add a file with a workflow_spec_id
/files?workflow_id=x
* You can find all files associated with a workflow_id, and add a file that is directly associated with the workflow
/files?workflow_id=x&form_field_key=y
* You can find all files associated with a form element on a running workflow, and add a new file.
Note: you can add multiple files to the same form_field_key, IF they have different file names. If the same name, the original file is archived,
and the new file takes its place.
The study endpoints always return a list of the file metadata associated with the study. Removed /studies-files, but there is an
endpoint called
/studies/all - that returns all the studies in the system, and does include their files.
On a deeper level:
The File model no longer contains:
- study_id,
- task_id,
- form_field_key
Instead, if the file is associated with workflow - then that is the one way it is connected to the study, and we use this relationship to find files for a study.
A file is never associated with a task_id, as these change when the workflow is reloaded.
The form_field_key must match the irb_doc_code, so when requesting files for a form field, we just look up the irb_doc_code.
2020-05-28 08:27:26 -04:00
|
|
|
name=file.filename, content_type=file.content_type,
|
|
|
|
binary_data=file.stream.read())
|
2022-01-11 15:30:22 -05:00
|
|
|
else:
|
|
|
|
raise ApiError("invalid_file", "You must supply either a workflow spec id or a workflow_id and form_field_key.")
|
|
|
|
|
|
|
|
return FileSchema().dump(to_file_api(file_model))
|
|
|
|
|
|
|
|
|
2020-01-31 10:39:19 -05:00
|
|
|
def update_file_data(file_id):
|
2020-01-14 11:45:12 -05:00
|
|
|
file_model = session.query(FileModel).filter_by(id=file_id).with_for_update().first()
|
2020-02-10 16:19:23 -05:00
|
|
|
file = connexion.request.files['file']
|
2019-12-27 13:50:03 -05:00
|
|
|
if file_model is None:
|
2021-08-31 15:28:21 -04:00
|
|
|
raise ApiError('no_such_file', f'The file id you provided ({file_id}) does not exist')
|
2022-02-02 12:59:56 -05:00
|
|
|
file_model = UserFileService.update_file(file_model, file.stream.read(), file.content_type)
|
2020-05-28 20:03:50 -04:00
|
|
|
return FileSchema().dump(to_file_api(file_model))
|
2019-12-27 13:50:03 -05:00
|
|
|
|
2022-01-11 15:30:22 -05:00
|
|
|
|
2020-12-07 08:49:38 -05:00
|
|
|
def get_file_data_by_hash(md5_hash):
|
|
|
|
filedatamodel = session.query(FileDataModel).filter(FileDataModel.md5_hash == md5_hash).first()
|
2022-01-11 15:30:22 -05:00
|
|
|
return get_file_data(filedatamodel.file_model_id, version=filedatamodel.version)
|
2019-12-27 13:50:03 -05:00
|
|
|
|
2022-01-07 15:34:51 -05:00
|
|
|
|
2020-05-23 15:21:30 -04:00
|
|
|
def get_file_data(file_id, version=None):
|
2022-01-07 15:34:51 -05:00
|
|
|
file_model = session.query(FileModel).filter(FileModel.id==file_id).first()
|
2022-01-11 15:30:22 -05:00
|
|
|
if file_model is not None:
|
2022-02-02 12:59:56 -05:00
|
|
|
file_data_model = UserFileService.get_file_data(file_id, version)
|
2022-01-11 15:30:22 -05:00
|
|
|
if file_data_model is not None:
|
|
|
|
return send_file(
|
|
|
|
io.BytesIO(file_data_model.data),
|
|
|
|
attachment_filename=file_model.name,
|
|
|
|
mimetype=file_model.content_type,
|
|
|
|
cache_timeout=-1 # Don't cache these files on the browser.
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise ApiError('missing_data_model', f'The data model for file ({file_id}) does not exist')
|
|
|
|
else:
|
|
|
|
raise ApiError('missing_file_model', f'The file id you provided ({file_id}) does not exist')
|
|
|
|
|
|
|
|
|
2021-05-05 11:30:08 -04:00
|
|
|
def get_file_data_link(file_id, auth_token, version=None):
|
|
|
|
if not verify_token(auth_token):
|
|
|
|
raise ApiError('not_authenticated', 'You need to include an authorization token in the URL with this')
|
2022-01-11 15:30:22 -05:00
|
|
|
file_model = session.query(FileModel).filter(FileModel.id==file_id).first()
|
2022-02-02 12:59:56 -05:00
|
|
|
file_data = UserFileService.get_file_data(file_id, version)
|
2021-05-05 11:30:08 -04:00
|
|
|
if file_data is None:
|
2021-08-31 15:28:21 -04:00
|
|
|
raise ApiError('no_such_file', f'The file id you provided ({file_id}) does not exist')
|
2021-05-05 11:30:08 -04:00
|
|
|
return send_file(
|
|
|
|
io.BytesIO(file_data.data),
|
2022-01-11 15:30:22 -05:00
|
|
|
attachment_filename=file_model.name,
|
|
|
|
mimetype=file_model.content_type,
|
2021-05-05 11:30:08 -04:00
|
|
|
cache_timeout=-1, # Don't cache these files on the browser.
|
|
|
|
last_modified=file_data.date_created,
|
2022-01-11 15:30:22 -05:00
|
|
|
as_attachment=True
|
2021-05-05 11:30:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-12-27 13:50:03 -05:00
|
|
|
def get_file_info(file_id):
|
2020-01-14 11:45:12 -05:00
|
|
|
file_model = session.query(FileModel).filter_by(id=file_id).with_for_update().first()
|
2019-12-27 13:50:03 -05:00
|
|
|
if file_model is None:
|
2021-08-31 15:28:21 -04:00
|
|
|
raise ApiError('no_such_file', f'The file id you provided ({file_id}) does not exist', status_code=404)
|
2020-05-28 20:03:50 -04:00
|
|
|
return FileSchema().dump(to_file_api(file_model))
|
2019-12-27 13:50:03 -05:00
|
|
|
|
|
|
|
|
2020-01-31 10:39:19 -05:00
|
|
|
def update_file_info(file_id, body):
|
|
|
|
if file_id is None:
|
2020-06-03 15:03:22 -04:00
|
|
|
raise ApiError('no_such_file', 'Please provide a valid File ID.')
|
2020-01-31 10:39:19 -05:00
|
|
|
|
|
|
|
file_model = session.query(FileModel).filter_by(id=file_id).first()
|
|
|
|
|
|
|
|
if file_model is None:
|
2020-03-23 12:22:26 -04:00
|
|
|
raise ApiError('unknown_file_model', 'The file_model "' + file_id + '" is not recognized.')
|
2020-01-31 10:39:19 -05:00
|
|
|
|
|
|
|
file_model = FileModelSchema().load(body, session=session)
|
|
|
|
session.add(file_model)
|
|
|
|
session.commit()
|
2020-05-28 20:03:50 -04:00
|
|
|
return FileSchema().dump(to_file_api(file_model))
|
2020-01-31 10:39:19 -05:00
|
|
|
|
|
|
|
|
2019-12-27 13:50:03 -05:00
|
|
|
def delete_file(file_id):
|
2022-02-02 12:59:56 -05:00
|
|
|
UserFileService.delete_file(file_id)
|
2021-08-11 08:55:10 -04:00
|
|
|
|
|
|
|
|
2021-09-03 10:32:32 -04:00
|
|
|
def dmn_from_ss():
|
|
|
|
file = connexion.request.files['file']
|
2022-02-02 12:59:56 -05:00
|
|
|
result = UserFileService.dmn_from_spreadsheet(file)
|
2021-09-07 12:09:30 -04:00
|
|
|
return send_file(
|
|
|
|
io.BytesIO(result),
|
|
|
|
attachment_filename='temp_dmn.dmn',
|
|
|
|
mimetype='text/xml',
|
|
|
|
cache_timeout=-1, # Don't cache these files on the browser.
|
|
|
|
last_modified=datetime.now()
|
|
|
|
)
|