From 4190ab393b0758be142df296f31a34f9cd944338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 18 Jul 2016 17:54:56 +0200 Subject: [PATCH] workflows: allow setting files to assembled dictionaries (#12629) --- tests/test_workflows.py | 31 +++++++++++++++++++++++++++++++ wcs/wf/backoffice_fields.py | 38 ++++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/tests/test_workflows.py b/tests/test_workflows.py index 81998a1..48a2c34 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -1733,3 +1733,34 @@ def test_set_backoffice_field_file(pub): formdata = formdef.data_class().get(formdata.id) assert formdata.data['bo1'].base_filename == 'xxx.xml' + + # check storing a file from an assembled dictionary + item = SetBackofficeFieldsWorkflowStatusItem() + item.fields = [{'field_id': 'bo1', + 'value': '={"content": "hello world", "filename": "hello.txt"}'}] + item.perform(formdata) + + formdata = formdef.data_class().get(formdata.id) + assert formdata.data['bo1'].base_filename == 'hello.txt' + assert formdata.data['bo1'].get_content() == 'hello world' + + item = SetBackofficeFieldsWorkflowStatusItem() + item.fields = [{'field_id': 'bo1', + 'value': '={"b64_content": "SEVMTE8gV09STEQ=", "filename": "hello.txt"}'}] + item.perform(formdata) + + formdata = formdef.data_class().get(formdata.id) + assert formdata.data['bo1'].base_filename == 'hello.txt' + assert formdata.data['bo1'].get_content() == 'HELLO WORLD' + + # check wrong value + del formdata.data['bo1'] + formdata.store() + assert not 'bo1' in formdata.data + + item = SetBackofficeFieldsWorkflowStatusItem() + item.fields = [{'field_id': 'bo1', 'value': '="HELLO"'}] + item.perform(formdata) + + formdata = formdef.data_class().get(formdata.id) + assert not 'bo1' in formdata.data diff --git a/wcs/wf/backoffice_fields.py b/wcs/wf/backoffice_fields.py index 2ed5d0a..f409e59 100644 --- a/wcs/wf/backoffice_fields.py +++ b/wcs/wf/backoffice_fields.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 import sys import xml.etree.ElementTree as ET @@ -83,18 +84,47 @@ class SetBackofficeFieldsWorkflowStatusItem(WorkflowStatusItem): title=_('Fields Update'), value=self.fields, workflow=self.parent.parent) + def get_file_value(self, new_value): + if isinstance(new_value, PicklableUpload): + return new_value + + if isinstance(new_value, NamedAttachmentsSubstitutionProxy): + upload = PicklableUpload(new_value.filename, new_value.content_type) + upload.receive([new_value.content]) + return upload + + if isinstance(new_value, dict): + # if value is a dictionary we expect it to have a content or + # b64_content key and a filename keys and an optional + # content_type key. + if 'b64_content' in new_value: + new_value['content'] = base64.decodestring(new_value['b64_content']) + if 'filename' in new_value and 'content' in new_value: + upload = PicklableUpload(new_value['filename'], + new_value.get('content_type') or 'application/octet-stream') + upload.receive(new_value['content']) + return upload + + raise ValueError('invalid data for file type (%r)' % new_value) + def perform(self, formdata): if not self.fields: return for field in self.fields: try: + formdef_field = [x for x in formdata.formdef.fields + if 'bo%s' % x.id == field['field_id']][0] + except IndexError: + continue + + try: new_value = self.compute(field['value'], raises=True) + if formdef_field.type == 'file': + new_value = self.get_file_value(new_value) except: get_publisher().notify_of_exception(sys.exc_info()) - if isinstance(new_value, NamedAttachmentsSubstitutionProxy): - upload = PicklableUpload(new_value.filename, new_value.content_type) - upload.receive([new_value.content]) - new_value = upload + continue + formdata.data['%s' % field['field_id']] = new_value formdata.store() -- 2.8.1