Added some checks to do_task to look for valid irb_doc_code and whether or not we found a file

Added code for do_task_validate
This commit is contained in:
mike cullerton 2021-04-05 13:15:09 -04:00
parent 9317e49e65
commit f0f5cff8d7
1 changed files with 22 additions and 5 deletions

View File

@ -1,4 +1,5 @@
from crc import session
from crc.api.common import ApiError
from crc.models.file import FileModel
from crc.scripts.script import Script
from crc.services.file_service import FileService
@ -10,11 +11,27 @@ class DeleteIRBDocument(Script):
return """Delete an IRB document from a workflow"""
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
pass
irb_document = kwargs['irb_document']
result = session.query(FileModel).filter(
FileModel.workflow_id == workflow_id, FileModel.irb_doc_code == irb_document).all()
if result:
return True
return False
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
irb_document = kwargs['irb_document']
result = session.query(FileModel).filter(FileModel.workflow_id==workflow_id, FileModel.irb_doc_code==irb_document).all()
for file in result:
FileService.delete_file(file.id)
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)