Allow both a keyword argument, a single argument when calling delete_file. And call it "delete_file" to be more conssitent with existing scripts.
This commit is contained in:
parent
611c72f431
commit
41310b6879
|
@ -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)
|
|
@ -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)
|
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" id="Definitions_3d948db" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="3.5.0">
|
||||
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" id="Definitions_3d948db" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="3.7.3">
|
||||
<bpmn:process id="Process_44b3aca" isExecutable="true">
|
||||
<bpmn:startEvent id="StartEvent_1">
|
||||
<bpmn:outgoing>Flow_1j6i6nv</bpmn:outgoing>
|
||||
|
@ -21,36 +21,36 @@
|
|||
<bpmn:scriptTask id="Activity_DeleteIRBDocument" name="Delete IRB Document">
|
||||
<bpmn:incoming>SequenceFlow_1mmief6</bpmn:incoming>
|
||||
<bpmn:outgoing>Flow_1rexoi9</bpmn:outgoing>
|
||||
<bpmn:script>delete_irb_document(irb_document=irb_document)</bpmn:script>
|
||||
<bpmn:script>delete_file(irb_document)</bpmn:script>
|
||||
</bpmn:scriptTask>
|
||||
<bpmn:sequenceFlow id="SequenceFlow_1mmief6" sourceRef="Activity_WhichIRBDocument" targetRef="Activity_DeleteIRBDocument" />
|
||||
</bpmn:process>
|
||||
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
|
||||
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_44b3aca">
|
||||
<bpmndi:BPMNEdge id="Flow_1j6i6nv_di" bpmnElement="Flow_1j6i6nv">
|
||||
<di:waypoint x="215" y="117" />
|
||||
<di:waypoint x="260" y="117" />
|
||||
<bpmndi:BPMNEdge id="SequenceFlow_1mmief6_di" bpmnElement="SequenceFlow_1mmief6">
|
||||
<di:waypoint x="360" y="117" />
|
||||
<di:waypoint x="400" y="117" />
|
||||
</bpmndi:BPMNEdge>
|
||||
<bpmndi:BPMNEdge id="Flow_1rexoi9_di" bpmnElement="Flow_1rexoi9">
|
||||
<di:waypoint x="500" y="117" />
|
||||
<di:waypoint x="562" y="117" />
|
||||
</bpmndi:BPMNEdge>
|
||||
<bpmndi:BPMNEdge id="Flow_1j6i6nv_di" bpmnElement="Flow_1j6i6nv">
|
||||
<di:waypoint x="215" y="117" />
|
||||
<di:waypoint x="260" y="117" />
|
||||
</bpmndi:BPMNEdge>
|
||||
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
|
||||
<dc:Bounds x="179" y="99" width="36" height="36" />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNShape id="Event_06rfn6m_di" bpmnElement="Event_06rfn6m">
|
||||
<dc:Bounds x="562" y="99" width="36" height="36" />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNShape id="Activity_176crcy_di" bpmnElement="Activity_WhichIRBDocument">
|
||||
<dc:Bounds x="260" y="77" width="100" height="80" />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNShape id="Activity_0om2kg5_di" bpmnElement="Activity_DeleteIRBDocument">
|
||||
<dc:Bounds x="400" y="77" width="100" height="80" />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNShape id="Event_06rfn6m_di" bpmnElement="Event_06rfn6m">
|
||||
<dc:Bounds x="562" y="99" width="36" height="36" />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNEdge id="SequenceFlow_1mmief6_di" bpmnElement="SequenceFlow_1mmief6">
|
||||
<di:waypoint x="360" y="117" />
|
||||
<di:waypoint x="400" y="117" />
|
||||
</bpmndi:BPMNEdge>
|
||||
</bpmndi:BPMNPlane>
|
||||
</bpmndi:BPMNDiagram>
|
||||
</bpmn:definitions>
|
||||
|
|
Loading…
Reference in New Issue