Put api endpoints back into api/file.py

This commit is contained in:
mike cullerton 2022-04-20 10:34:56 -04:00
parent 7f8ea483c9
commit 89a2650cc8
2 changed files with 114 additions and 115 deletions

View File

@ -793,76 +793,6 @@ paths:
items:
$ref: "#/components/schemas/WorkflowSpecCategory"
# /document:
# parameters:
# - name: workflow_id
# in: query
# required: false
# description: ID of the associated workflow
# schema:
# type: integer
# - name: task_spec_name
# in: query
# required: false
# description: Name of the Task Spec
# schema:
# type: string
# - name: irb_doc_code
# in: query
# required: false
# description: IRB Document Code. Make sure this matches a document in the irb_documents.xslx reference sheet.
# schema:
# type: string
# - name: study_id
# in: query
# required: false
# description: ID of the associated Study
# schema:
# type: integer
# post:
# operationId: crc.api.file_refactor.add_file
# summary: Add a new document
# tags:
# - Documents
# requestBody:
# content:
# multipart/form-data:
# schema:
# type: object
# properties:
# file:
# type: string
# format: binary
# responses:
# '200':
# description: Document metadata
# content:
# application/json:
# schema:
# $ref: "#/components/schemas/File"
# /document/{document_id}/data:
# parameters:
# - name: document_id
# in: path
# required: true
# description: The id of the requested Document
# schema:
# type: integer
# get:
# operationId: crc.api.file_refactor.get_file_data
# summary: Returns only the file contents
# tags:
# - Documents
# responses:
# '200':
# description: Returns the actual file
# content:
# application/octet-stream:
# schema:
# type: string
# format: binary
# example: '<?xml version="1.0" encoding="UTF-8"?><bpmn:definitions></bpmn:definitions>'
/file:
parameters:
- name: workflow_id
@ -890,7 +820,7 @@ paths:
schema:
type: integer
get:
operationId: crc.api.file_refactor.get_files
operationId: crc.api.file.get_files
summary: Provides a list of files that match the given parameters (such as a spec id) IMPORTANT, only includes metadata, not the file content.
tags:
- Files
@ -905,7 +835,7 @@ paths:
$ref: "#/components/schemas/File"
post:
operationId: crc.api.file_refactor.add_file
operationId: crc.api.file.add_file
summary: Add a new file
tags:
- Files
@ -934,7 +864,7 @@ paths:
schema:
type: integer
get:
operationId: crc.api.file_refactor.get_file_info
operationId: crc.api.file.get_file_info
summary: Returns metadata about a file.
tags:
- Files
@ -946,7 +876,7 @@ paths:
schema:
$ref: "#/components/schemas/File"
put:
operationId: crc.api.file_refactor.update_file_info
operationId: crc.api.file.update_file_info
summary: Updates existing file info with the given parameters.
tags:
- Files
@ -963,7 +893,7 @@ paths:
schema:
$ref: "#/components/schemas/File"
delete:
operationId: crc.api.file_refactor.delete_file
operationId: crc.api.file.delete_file
summary: Removes an existing file.
tags:
- Files
@ -991,7 +921,7 @@ paths:
schema :
type : integer
get :
operationId : crc.api.file_refactor.get_file_data_link
operationId : crc.api.file.get_file_data_link
summary : Returns only the file contents
security: []
tags :
@ -1020,7 +950,7 @@ paths:
schema:
type: integer
get:
operationId: crc.api.file_refactor.get_file_data
operationId: crc.api.file.get_file_data
summary: Returns only the file contents
tags:
- Files
@ -1034,7 +964,7 @@ paths:
format: binary
example: '<?xml version="1.0" encoding="UTF-8"?><bpmn:definitions></bpmn:definitions>'
put:
operationId: crc.api.file_refactor.update_file_data
operationId: crc.api.file.update_file_data
summary: Update only the file contents for a given file
tags:
- Files

View File

@ -7,6 +7,7 @@ from flask import send_file
from crc import session
from crc.api.common import ApiError
from crc.api.user import verify_token
from crc.models.file import File, FileSchema, FileModel, FileModelSchema
# from crc.models.file import DocumentModel, FileSchema, File, FileModel, FileModelSchema, FileDataModel
from crc.models.workflow import WorkflowModel
from crc.services.document_service import DocumentService
@ -15,14 +16,26 @@ from crc.services.user_file_service import UserFileService
def to_file_api(file_model):
...
doc_dictionary = DocumentService.get_dictionary()
return File.from_file_model(file_model, doc_dictionary)
# file_data_model = UserFileService.get_file_data(file_model.id)
# return File.from_models(file_model, file_data_model,
# DocumentService.get_dictionary())
def get_files(workflow_id=None, form_field_key=None, study_id=None):
...
def get_files(workflow_id=None, irb_doc_code=None, study_id=None):
if workflow_id is None:
raise ApiError('missing_parameter',
'Please specify a workflow_id with an optional form_field_key')
if study_id is not None:
file_models = UserFileService.get_files_for_study(study_id=study_id, irb_doc_code=irb_doc_code)
else:
file_models = UserFileService.get_files(workflow_id=workflow_id,
irb_doc_code=irb_doc_code)
files = (to_file_api(model) for model in file_models)
return FileSchema(many=True).dump(files)
# if workflow_id is None:
# raise ApiError('missing_parameter',
# 'Please specify a workflow_id with an optional form_field_key')
@ -37,8 +50,33 @@ def get_files(workflow_id=None, form_field_key=None, study_id=None):
# return FileSchema(many=True).dump(files)
def add_file(workflow_id=None, task_spec_name=None, form_field_key=None):
...
def add_file(workflow_id=None, task_spec_name=None, irb_doc_code=None):
file = connexion.request.files['file']
if workflow_id:
workflow = session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first()
if workflow is None:
raise ApiError('invalid_workflow',
f'Unable to find a workflow with id {workflow_id}')
if irb_doc_code is None:
raise ApiError('invalid_doc_code',
'When adding a workflow related file, you must specify an irb_doc_code')
if task_spec_name is None:
raise ApiError('invalid_task_spec_name',
'When adding a workflow related file, you must specify a task_spec_name')
file_model = UserFileService.add_workflow_file(workflow_id=workflow_id,
irb_doc_code=irb_doc_code,
task_spec_name=task_spec_name,
name=file.filename,
content_type=file.content_type,
binary_data=file.stream.read())
# We calculate the document statuses once per request, but a script may execute more quickly
# So force a refresh of the document status here.
# TODO: Finish refactoring this
# StudyService.get_documents_status(study_id=workflow.study_id, force=True)
else:
raise ApiError('missing_workflow_id', 'You must supply a workflow_id.')
return FileSchema().dump(to_file_api(file_model))
# file = connexion.request.files['file']
# if workflow_id:
# # TODO: should 'WorkflowModel.id == WorkflowModel.id' be 'WorkflowModel.id == workflow_id'?
@ -66,23 +104,45 @@ def add_file(workflow_id=None, task_spec_name=None, form_field_key=None):
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:
# raise ApiError('no_such_file', f'The file id you provided ({file_id}) does not exist')
# file_model = UserFileService.update_file(file_model, file.stream.read(), file.content_type)
# return FileSchema().dump(to_file_api(file_model))
file_model = session.query(FileModel).filter_by(id=file_id).with_for_update().first()
file = connexion.request.files['file']
if file_model is None:
raise ApiError('no_such_file', f'The file id you provided ({file_id}) does not exist')
file_model = UserFileService.update_file(file_model, file.stream.read(), file.content_type)
return FileSchema().dump(to_file_api(file_model))
def get_file_data_by_hash(md5_hash):
...
file_model = session.query(FileModel).filter(FileModel.md5_hash == md5_hash).first()
# if document_model is not None:
# return send_file(
# io.BytesIO(document_model.data),
# attachment_filename=document_model.name,
# mimetype=document_model.content_type,
# cache_timeout=-1 # Don't cache these files on the browser.
# )
# else:
# raise ApiError('missing_file_model', f'Could not find a document_model with the hash you provided.')
return get_file_data(file_model.id)
# filedatamodel = session.query(FileDataModel).filter(FileDataModel.md5_hash == md5_hash).first()
# return get_file_data(filedatamodel.file_model_id, version=filedatamodel.version)
def get_file_data(file_id, version=None):
...
def get_file_data(file_id):
file_model = session.query(FileModel).filter(FileModel.id==file_id).first()
if file_model is not None:
# file_data_model = UserFileService.get_file_data(file_id, version)
# if file_data_model is not None:
return send_file(
io.BytesIO(file_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')
# file_model = session.query(FileModel).filter(FileModel.id==file_id).first()
# if file_model is not None:
# file_data_model = UserFileService.get_file_data(file_id, version)
@ -99,8 +159,20 @@ def get_file_data(file_id, version=None):
# raise ApiError('missing_file_model', f'The file id you provided ({file_id}) does not exist')
def get_file_data_link(file_id, auth_token, version=None):
...
def get_file_data_link(file_id, auth_token):
if not verify_token(auth_token):
raise ApiError('not_authenticated', 'You need to include an authorization token in the URL with this')
file_model = session.query(FileModel).filter(FileModel.id==file_id).first()
if file_model is None:
raise ApiError('no_such_file', f'The file id you provided ({file_id}) does not exist')
return send_file(
io.BytesIO(file_model.data),
attachment_filename=file_model.name,
mimetype=file_model.content_type,
cache_timeout=-1, # Don't cache these files on the browser.
last_modified=file_model.date_created,
as_attachment=True
)
# if not verify_token(auth_token):
# raise ApiError('not_authenticated', 'You need to include an authorization token in the URL with this')
# file_model = session.query(FileModel).filter(FileModel.id==file_id).first()
@ -118,32 +190,29 @@ def get_file_data_link(file_id, auth_token, version=None):
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:
# raise ApiError('no_such_file', f'The file id you provided ({file_id}) does not exist', status_code=404)
# return FileSchema().dump(to_file_api(file_model))
file_model = session.query(FileModel).filter_by(id=file_id).with_for_update().first()
if file_model is None:
raise ApiError('no_such_file', f'The file id you provided ({file_id}) does not exist', status_code=404)
return FileSchema().dump(to_file_api(file_model))
def update_file_info(file_id, body):
...
# if file_id is None:
# raise ApiError('no_such_file', 'Please provide a valid File ID.')
#
# file_model = session.query(FileModel).filter_by(id=file_id).first()
#
# if file_model is None:
# raise ApiError('unknown_file_model', 'The file_model "' + file_id + '" is not recognized.')
#
# file_model = FileModelSchema().load(body, session=session)
# session.add(file_model)
# session.commit()
# return FileSchema().dump(to_file_api(file_model))
if file_id is None:
raise ApiError('no_such_file', 'Please provide a valid File ID.')
file_model = session.query(FileModel).filter_by(id=file_id).first()
if file_model is None:
raise ApiError('unknown_file_model', 'The file_model "' + file_id + '" is not recognized.')
file_model = FileModelSchema().load(body, session=session)
session.add(file_model)
session.commit()
return FileSchema().dump(to_file_api(file_model))
def delete_file(file_id):
...
# UserFileService.delete_file(file_id)
UserFileService.delete_file(file_id)
def dmn_from_ss():