Merge pull request #374 from sartography/zip-documents-379
Zip documents #379
This commit is contained in:
commit
9605bd3c1b
|
@ -1,6 +1,7 @@
|
|||
from crc import session
|
||||
from crc.api.common import ApiError
|
||||
from crc.models.file import FileModel, FileDataModel
|
||||
from crc.api.file import to_file_api
|
||||
from crc.models.file import FileModel, FileDataModel, FileSchema
|
||||
from crc.scripts.script import Script
|
||||
from crc.services.file_service import FileService
|
||||
from crc.services.study_service import StudyService
|
||||
|
@ -46,10 +47,10 @@ class GetZippedFiles(Script):
|
|||
zfw.writestr(file_name, file_data.data)
|
||||
|
||||
with open(temp_file.name, mode='rb') as handle:
|
||||
file_model = FileService().add_workflow_file(workflow_id, None, task.name, zip_filename,
|
||||
file_model = FileService().add_workflow_file(workflow_id, None, task.get_name(), zip_filename,
|
||||
'application/zip', handle.read())
|
||||
return file_model
|
||||
|
||||
# return file_model
|
||||
return FileSchema().dump(to_file_api(file_model))
|
||||
else:
|
||||
raise ApiError(code='missing_file_ids',
|
||||
message='You must include a list of file_ids.')
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
<?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:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_d37e36c" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.2.0">
|
||||
<bpmn:process id="Process_SayHello" name="Say Hello" isExecutable="true">
|
||||
<bpmn:startEvent id="StartEvent_1">
|
||||
<bpmn:outgoing>Flow_1o4d6ut</bpmn:outgoing>
|
||||
</bpmn:startEvent>
|
||||
<bpmn:sequenceFlow id="Flow_1o4d6ut" sourceRef="StartEvent_1" targetRef="Activity_GetFileIDs" />
|
||||
<bpmn:userTask id="Activity_GetFileIDs" name="Get File IDs" camunda:formKey="GetName">
|
||||
<bpmn:extensionElements>
|
||||
<camunda:formData>
|
||||
<camunda:formField id="file_id" label="File ID" type="long">
|
||||
<camunda:properties>
|
||||
<camunda:property id="repeat" value="file_ids" />
|
||||
</camunda:properties>
|
||||
<camunda:validation>
|
||||
<camunda:constraint name="required" config="True" />
|
||||
</camunda:validation>
|
||||
</camunda:formField>
|
||||
</camunda:formData>
|
||||
</bpmn:extensionElements>
|
||||
<bpmn:incoming>Flow_1o4d6ut</bpmn:incoming>
|
||||
<bpmn:outgoing>Flow_1ga7oo5</bpmn:outgoing>
|
||||
</bpmn:userTask>
|
||||
<bpmn:sequenceFlow id="Flow_1ga7oo5" sourceRef="Activity_GetFileIDs" targetRef="Activity_GetZip" />
|
||||
<bpmn:scriptTask id="Activity_GetZip" name="Get Zip">
|
||||
<bpmn:incoming>Flow_1ga7oo5</bpmn:incoming>
|
||||
<bpmn:outgoing>Flow_1uyi5u2</bpmn:outgoing>
|
||||
<bpmn:script>to_zip = []
|
||||
for file_id in file_ids:
|
||||
to_zip.append(file_id['file_id'])
|
||||
zip_file = get_zipped_files(file_ids=to_zip)</bpmn:script>
|
||||
</bpmn:scriptTask>
|
||||
<bpmn:sequenceFlow id="Flow_1uyi5u2" sourceRef="Activity_GetZip" targetRef="Activity_DisplayZip" />
|
||||
<bpmn:manualTask id="Activity_DisplayZip" name="Display Zipped Files">
|
||||
<bpmn:documentation># To Zip
|
||||
{{ to_zip }}
|
||||
|
||||
# Zip
|
||||
{{ zip_file }}</bpmn:documentation>
|
||||
<bpmn:incoming>Flow_1uyi5u2</bpmn:incoming>
|
||||
<bpmn:outgoing>Flow_0l42ysq</bpmn:outgoing>
|
||||
</bpmn:manualTask>
|
||||
<bpmn:endEvent id="Event_1sfhw21">
|
||||
<bpmn:incoming>Flow_0l42ysq</bpmn:incoming>
|
||||
</bpmn:endEvent>
|
||||
<bpmn:sequenceFlow id="Flow_0l42ysq" sourceRef="Activity_DisplayZip" targetRef="Event_1sfhw21" />
|
||||
</bpmn:process>
|
||||
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
|
||||
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_SayHello">
|
||||
<bpmndi:BPMNEdge id="Flow_0l42ysq_di" bpmnElement="Flow_0l42ysq">
|
||||
<di:waypoint x="662" y="117" />
|
||||
<di:waypoint x="712" y="117" />
|
||||
</bpmndi:BPMNEdge>
|
||||
<bpmndi:BPMNEdge id="Flow_1uyi5u2_di" bpmnElement="Flow_1uyi5u2">
|
||||
<di:waypoint x="500" y="117" />
|
||||
<di:waypoint x="562" y="117" />
|
||||
</bpmndi:BPMNEdge>
|
||||
<bpmndi:BPMNEdge id="Flow_1ga7oo5_di" bpmnElement="Flow_1ga7oo5">
|
||||
<di:waypoint x="340" y="117" />
|
||||
<di:waypoint x="400" y="117" />
|
||||
</bpmndi:BPMNEdge>
|
||||
<bpmndi:BPMNEdge id="Flow_1o4d6ut_di" bpmnElement="Flow_1o4d6ut">
|
||||
<di:waypoint x="188" y="117" />
|
||||
<di:waypoint x="240" y="117" />
|
||||
</bpmndi:BPMNEdge>
|
||||
<bpmndi:BPMNShape id="Activity_1jh8vhm_di" bpmnElement="Activity_DisplayZip">
|
||||
<dc:Bounds x="562" y="77" width="100" height="80" />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNShape id="Activity_0ftuz3s_di" bpmnElement="Activity_GetFileIDs">
|
||||
<dc:Bounds x="240" y="77" width="100" height="80" />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
|
||||
<dc:Bounds x="152" y="99" width="36" height="36" />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNShape id="Activity_1qhfe35_di" bpmnElement="Activity_GetZip">
|
||||
<dc:Bounds x="400" y="77" width="100" height="80" />
|
||||
</bpmndi:BPMNShape>
|
||||
<bpmndi:BPMNShape id="Event_1sfhw21_di" bpmnElement="Event_1sfhw21">
|
||||
<dc:Bounds x="712" y="99" width="36" height="36" />
|
||||
</bpmndi:BPMNShape>
|
||||
</bpmndi:BPMNPlane>
|
||||
</bpmndi:BPMNDiagram>
|
||||
</bpmn:definitions>
|
|
@ -1,10 +1,7 @@
|
|||
from tests.base_test import BaseTest
|
||||
|
||||
from crc import session
|
||||
from crc.models.file import FileModel, FileDataModel
|
||||
from crc.models.workflow import WorkflowModel
|
||||
from crc.models.study import StudyModel
|
||||
from crc.scripts.get_zipped_files import GetZippedFiles
|
||||
from crc.models.file import FileDataModel
|
||||
from crc.services.file_service import FileService
|
||||
|
||||
import io
|
||||
|
@ -16,40 +13,38 @@ class TestGetZippedFiles(BaseTest):
|
|||
|
||||
def test_get_zipped_files(self):
|
||||
self.load_example_data()
|
||||
study = session.query(StudyModel).order_by(StudyModel.id.desc()).first()
|
||||
workflow = session.query(WorkflowModel).first()
|
||||
|
||||
workflow = self.create_workflow('get_zip_file')
|
||||
study_id = workflow.study_id
|
||||
workflow_api = self.get_workflow_api(workflow)
|
||||
task = workflow_api.next_task
|
||||
|
||||
# Add files to use in the test
|
||||
FileService.add_workflow_file(workflow_id=workflow.id,
|
||||
model_1 = FileService.add_workflow_file(workflow_id=workflow.id,
|
||||
name="document_1.png", content_type="text",
|
||||
task_spec_name=task.name,
|
||||
binary_data=b'1234', irb_doc_code='Study_Protocol_Document')
|
||||
FileService.add_workflow_file(workflow_id=workflow.id,
|
||||
model_2 = FileService.add_workflow_file(workflow_id=workflow.id,
|
||||
name="document_2.txt", content_type="text",
|
||||
task_spec_name=task.name,
|
||||
binary_data=b'1234', irb_doc_code='Study_App_Doc')
|
||||
FileService.add_workflow_file(workflow_id=workflow.id,
|
||||
model_3 = FileService.add_workflow_file(workflow_id=workflow.id,
|
||||
name="document_3.pdf", content_type="text",
|
||||
task_spec_name=task.name,
|
||||
binary_data=b'1234', irb_doc_code='Admin_AdvChklst')
|
||||
binary_data=b'1234', irb_doc_code='AD_Consent_Model')
|
||||
|
||||
# Gather the file_ids
|
||||
file_ids = []
|
||||
files = session.query(FileModel).filter(FileModel.irb_doc_code != '').all()
|
||||
for file in files:
|
||||
file_ids.append(file.id)
|
||||
file_ids = [{'file_id': model_1.id}, {'file_id': model_2.id}, {'file_id': model_3.id}]
|
||||
workflow_api = self.complete_form(workflow, task, {'file_ids': file_ids})
|
||||
next_task = workflow_api.next_task
|
||||
file_model_id = next_task.data['zip_file']['id']
|
||||
|
||||
# Send files to the zipper, receive file_model in return
|
||||
file_model = GetZippedFiles().do_task(task, study.id, workflow.id, file_ids=file_ids, filename='another_attachment.zip')
|
||||
file_data = session.query(FileDataModel).filter(FileDataModel.file_model_id == file_model.id).first()
|
||||
file_data = session.query(FileDataModel).filter(FileDataModel.file_model_id == file_model_id).first()
|
||||
|
||||
# Test what we get back in the file_model
|
||||
# Test what we get back in the zipped file
|
||||
with zipfile.ZipFile(io.BytesIO(file_data.data), 'r') as zf:
|
||||
self.assertIsInstance(zf, zipfile.ZipFile)
|
||||
for name in zf.namelist():
|
||||
info = zf.getinfo(name)
|
||||
self.assertIn(os.path.basename(info.filename), ['1 Protocol document_1.png', '1 Application document_2.txt', '1 document_3.pdf'])
|
||||
self.assertIn(os.path.basename(info.filename), [f'{study_id} Protocol document_1.png', f'{study_id} Application document_2.txt', f'{study_id} Model document_3.pdf'])
|
||||
file = zf.read(name)
|
||||
self.assertEqual(b'1234', file)
|
||||
|
|
Loading…
Reference in New Issue