From 41310b687966c93fcf7ff0703f03ece8141538d8 Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 8 Apr 2021 13:42:17 -0400 Subject: [PATCH] Allow both a keyword argument, a single argument when calling delete_file. And call it "delete_file" to be more conssitent with existing scripts. --- crc/scripts/delete_file.py | 61 +++++++++++++++++++ crc/scripts/delete_irb_document.py | 45 -------------- .../add_delete_irb_document.bpmn | 24 ++++---- 3 files changed, 73 insertions(+), 57 deletions(-) create mode 100644 crc/scripts/delete_file.py delete mode 100644 crc/scripts/delete_irb_document.py diff --git a/crc/scripts/delete_file.py b/crc/scripts/delete_file.py new file mode 100644 index 00000000..eefdf442 --- /dev/null +++ b/crc/scripts/delete_file.py @@ -0,0 +1,61 @@ +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 + + +class DeleteFile(Script): + + @staticmethod + def process_document_deletion(doc_code, workflow_id, task): + if FileService.is_allowed_document(doc_code): + result = session.query(FileModel).filter( + FileModel.workflow_id == workflow_id, FileModel.irb_doc_code == 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 {doc_code} was found for this workflow.', + task=task) + else: + raise ApiError.from_task(code='invalid_document_code', + message=f'{doc_code} is not a valid document code', + task=task) + + def get_codes(self, task, args, kwargs): + if 'code' in kwargs: + if isinstance(kwargs['code'], list): + codes = kwargs['code'] + else: + codes = [kwargs['code']] + else: + codes = [] + for arg in args: + if isinstance(arg, list): + codes.extend(arg) + else: + codes.append(arg) + + if codes is None or len(codes) == 0: + raise ApiError.from_task("invalid_argument", "Please provide a valid document code to delete. " + "No valid arguments found.", task=task) + return codes + + def get_description(self): + return """Delete an IRB document from a workflow""" + + def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs): + doc_codes = self.get_codes(task, args, kwargs) + for code in doc_codes: + result = session.query(FileModel).filter( + FileModel.workflow_id == workflow_id, FileModel.irb_doc_code == code).all() + if not result: + return False + return True + + def do_task(self, task, study_id, workflow_id, *args, **kwargs): + doc_codes = self.get_codes(task, args, kwargs) + for doc_code in doc_codes: + self.process_document_deletion(doc_code, workflow_id, task) diff --git a/crc/scripts/delete_irb_document.py b/crc/scripts/delete_irb_document.py deleted file mode 100644 index 3d0d6752..00000000 --- a/crc/scripts/delete_irb_document.py +++ /dev/null @@ -1,45 +0,0 @@ -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 - - -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""" - - def do_task_validate_only(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() - if result: - return True - return False - - def do_task(self, task, study_id, workflow_id, *args, **kwargs): - - irb_doc_code = kwargs['irb_document'] - 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) diff --git a/tests/data/add_delete_irb_document/add_delete_irb_document.bpmn b/tests/data/add_delete_irb_document/add_delete_irb_document.bpmn index e927b6e2..0f7f2661 100644 --- a/tests/data/add_delete_irb_document/add_delete_irb_document.bpmn +++ b/tests/data/add_delete_irb_document/add_delete_irb_document.bpmn @@ -1,5 +1,5 @@ - + Flow_1j6i6nv @@ -21,36 +21,36 @@ SequenceFlow_1mmief6 Flow_1rexoi9 - delete_irb_document(irb_document=irb_document) + delete_file(irb_document) - - - + + + + + + + + + + - - - - - - -