From 8e9c3a451054dd6e9b865e5d2843ed3a7e998981 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 24 Jun 2021 21:18:31 +0200 Subject: [PATCH 2/2] workflows: allow Django templates to produce images New feature is tested agains the new |qrcode filter. --- tests/test_workflows.py | 45 +++++++++++++++++++++++++++++++++++++++ tox.ini | 1 + wcs/wf/export_to_model.py | 17 ++++++++------- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/tests/test_workflows.py b/tests/test_workflows.py index f60e78ea..bc598aad 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -4,6 +4,7 @@ import io import json import os import shutil +import tempfile import time import urllib.parse import zipfile @@ -3617,6 +3618,50 @@ def test_export_to_model_image(pub): ) +def test_export_to_model_qrcode(pub): + formdef = FormDef() + formdef.name = 'baz' + formdef.fields = [] + formdef.store() + + formdata = formdef.data_class()() + formdata.data = {} + formdata.just_created() + formdata.store() + pub.substitutions.feed(formdata) + + item = ExportToModel() + item.convert_to_pdf = False + item.method = 'non-interactive' + template_filename = os.path.join(os.path.dirname(__file__), 'template-with-qrcode.odt') + with open(template_filename, 'rb') as fd: + template = fd.read() + upload = QuixoteUpload('/foo/template.odt', content_type='application/octet-stream') + upload.fp = io.BytesIO() + upload.fp.write(template) + upload.fp.seek(0) + item.model_file = UploadedFile(pub.app_dir, None, upload) + item.attach_to_history = True + + item.perform(formdata) + + assert formdata.evolution[-1].parts[-1].base_filename == 'template.odt' + with zipfile.ZipFile(formdata.evolution[-1].parts[0].filename, mode='r') as zfile: + image_filename = [name for name in zfile.namelist() if name.endswith('.jpg')][0] + with zfile.open(image_filename, 'r') as image_fd: + with open('/tmp/image.png', 'wb') as fd_write: + fd_write.write(image_fd.read()) + image_fd.seek(0) + img = Image.open(image_fd) + from pyzbar.pyzbar import ZBarSymbol + from pyzbar.pyzbar import decode as decode_qrcode + + assert ( + decode_qrcode(img, symbols=[ZBarSymbol.QRCODE])[0].data.decode() + == 'http://example.net/backoffice/management/baz/1/' + ) + + def test_export_to_model_backoffice_field(pub): wf = Workflow(name='email with attachments') wf.backoffice_fields_formdef = WorkflowBackofficeFieldsFormDef(wf) diff --git a/tox.ini b/tox.ini index 443b963f..3faee916 100644 --- a/tox.ini +++ b/tox.ini @@ -44,6 +44,7 @@ deps = pylint Quixote>=3.0,<3.2 pre-commit + pyzbar commands = py.test -v {env:COVERAGE:} --junitxml=junit-{envname}.xml {posargs:tests/} pylint: ./pylint.sh wcs/ tests/ diff --git a/wcs/wf/export_to_model.py b/wcs/wf/export_to_model.py index 14db62d9..601fd0b3 100644 --- a/wcs/wf/export_to_model.py +++ b/wcs/wf/export_to_model.py @@ -588,17 +588,18 @@ class ExportToModel(WorkflowStatusItem): if node.tag == DRAW_FRAME: name = node.attrib.get(DRAW_NAME) - if not self.get_expression(name)['type'] == 'python': - continue # variable image - try: - variable_image = self.compute(name) - except Exception: - continue - if not hasattr(variable_image, 'get_content'): + pub = get_publisher() + with pub.complex_data(): + try: + variable_image = self.compute(name, allow_complex=True) + except Exception: + continue + complex_variable_image = get_publisher().get_cached_complex_data(variable_image) + if not hasattr(complex_variable_image, 'get_content'): continue image = [x for x in node if x.tag == DRAW_IMAGE][0] - new_images[image.attrib.get(XLINK_HREF)] = variable_image + new_images[image.attrib.get(XLINK_HREF)] = complex_variable_image for attr in ('text', 'tail'): if not getattr(node, attr): -- 2.32.0.rc0