From 88c3181305388fa4e4bd2267348cf7ad55080b66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 16 Dec 2015 09:58:10 +0100 Subject: [PATCH] workflows: encode model file in base64 in import/export (#9350) --- tests/test_workflow_import.py | 20 +++++++++++++++++--- wcs/wf/export_to_model.py | 9 +++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tests/test_workflow_import.py b/tests/test_workflow_import.py index 063c05f..8a99c8a 100644 --- a/tests/test_workflow_import.py +++ b/tests/test_workflow_import.py @@ -214,19 +214,33 @@ def test_export_to_model_action(): export_to = ExportToModel() export_to.label = 'test' - upload = Upload('/foo/bar', content_type='text/rtf') + upload = Upload('/foo/bar', content_type='application/vnd.oasis.opendocument.text') + file_content = '''PK\x03\x04\x14\x00\x00\x08\x00\x00\'l\x8eG^\xc62\x0c\'\x00''' upload.fp = StringIO.StringIO() - upload.fp.write('HELLO WORLD') + upload.fp.write(file_content) upload.fp.seek(0) export_to.model_file = UploadedFile(publisher.WcsPublisher.APP_DIR, None, upload) st1.items.append(export_to) export_to.parent = st1 assert wf.possible_status[0].items[0].model_file.base_filename == 'bar' - assert wf.possible_status[0].items[0].model_file.base_filename == 'bar' wf2 = assert_import_export_works(wf) assert wf2.possible_status[0].items[0].model_file.base_filename == 'bar' + assert wf2.possible_status[0].items[0].model_file.get_file().read() == file_content + # and test with an empty file + export_to = ExportToModel() + export_to.label = 'test' + upload = Upload('/foo/bar', content_type='text/rtf') + file_content = '' + upload.fp = StringIO.StringIO() + upload.fp.write(file_content) + upload.fp.seek(0) + export_to.model_file = UploadedFile(publisher.WcsPublisher.APP_DIR, None, upload) + st1.items = [export_to] + export_to.parent = st1 + wf2 = assert_import_export_works(wf) + assert wf2.possible_status[0].items[0].model_file.get_file().read() == file_content def test_export_roles(): wf = Workflow(name='roles') diff --git a/wcs/wf/export_to_model.py b/wcs/wf/export_to_model.py index f0ec887..e01fcfb 100644 --- a/wcs/wf/export_to_model.py +++ b/wcs/wf/export_to_model.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, see . +import base64 from StringIO import StringIO from xml.etree import ElementTree as ET import zipfile @@ -331,14 +332,18 @@ class ExportToModel(WorkflowStatusItem): el = ET.SubElement(xml_item, 'model_file') ET.SubElement(el, 'base_filename').text = self.model_file.base_filename ET.SubElement(el, 'content_type').text = self.model_file.content_type - ET.SubElement(el, 'content').text = self.model_file.get_file().read() + ET.SubElement(el, 'b64_content').text = base64.encodestring( + self.model_file.get_file().read()) def model_file_init_with_xml(self, elem, charset, include_id=False): if elem is None: return base_filename = elem.find('base_filename').text content_type = elem.find('content_type').text - content = elem.find('content').text + if elem.find('b64_content') is not None: + content = base64.decodestring(elem.find('b64_content').text) + if elem.find('content') is not None: + content = elem.find('content').text ids = (self.parent.parent.id, self.parent.id, self.id) filename = 'export_to_model-%s-%s-%s.upload' % ids -- 2.6.4