You can now delete a document or list of documents.
Also, refactored the fancy bits into `process_document_deletion`
This commit is contained in:
parent
33cef2bd2a
commit
f8428378e2
|
@ -7,6 +7,23 @@ from crc.services.file_service import FileService
|
|||
|
||||
class DeleteIRBDocument(Script):
|
||||
|
||||
@staticmethod
|
||||
def process_document_deletion(irb_doc_code, workflow_id, task):
|
||||
if FileService.is_allowed_document(irb_doc_code):
|
||||
result = session.query(FileModel).filter(
|
||||
FileModel.workflow_id == workflow_id, FileModel.irb_doc_code == irb_doc_code).all()
|
||||
if isinstance(result, list) and len(result) > 0 and isinstance(result[0], FileModel):
|
||||
for file in result:
|
||||
FileService.delete_file(file.id)
|
||||
else:
|
||||
raise ApiError.from_task(code='no_document_found',
|
||||
message=f'No document of type {irb_doc_code} was found for this workflow.',
|
||||
task=task)
|
||||
else:
|
||||
raise ApiError.from_task(code='invalid_irb_document',
|
||||
message=f'{irb_doc_code} is not a valid IRB document code',
|
||||
task=task)
|
||||
|
||||
def get_description(self):
|
||||
return """Delete an IRB document from a workflow"""
|
||||
|
||||
|
@ -21,17 +38,8 @@ class DeleteIRBDocument(Script):
|
|||
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
|
||||
|
||||
irb_doc_code = kwargs['irb_document']
|
||||
if FileService.is_allowed_document(irb_doc_code):
|
||||
result = session.query(FileModel).filter(
|
||||
FileModel.workflow_id == workflow_id, FileModel.irb_doc_code == irb_doc_code).all()
|
||||
if isinstance(result, list) and len(result) > 0 and isinstance(result[0], FileModel):
|
||||
for file in result:
|
||||
FileService.delete_file(file.id)
|
||||
else:
|
||||
raise ApiError.from_task(code='no_document_found',
|
||||
message=f'No document of type {irb_doc_code} was found for this workflow.',
|
||||
task=task)
|
||||
else:
|
||||
raise ApiError.from_task(code='invalid_irb_document',
|
||||
message=f'{irb_doc_code} is not a valid IRB document code',
|
||||
task=task)
|
||||
if isinstance(irb_doc_code, str):
|
||||
self.process_document_deletion(irb_doc_code, workflow_id, task)
|
||||
elif isinstance(irb_doc_code, list):
|
||||
for doc_code in irb_doc_code:
|
||||
self.process_document_deletion(doc_code, workflow_id, task)
|
||||
|
|
Loading…
Reference in New Issue