Projet

Général

Profil

0002-workflows-allow-Django-templates-to-produce-images.patch

Benjamin Dauvergne, 25 juin 2021 15:28

Télécharger (4,45 ko)

Voir les différences:

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(-)
tests/test_workflows.py
4 4
import json
5 5
import os
6 6
import shutil
7
import tempfile
7 8
import time
8 9
import urllib.parse
9 10
import zipfile
......
3617 3618
    )
3618 3619

  
3619 3620

  
3621
def test_export_to_model_qrcode(pub):
3622
    formdef = FormDef()
3623
    formdef.name = 'baz'
3624
    formdef.fields = []
3625
    formdef.store()
3626

  
3627
    formdata = formdef.data_class()()
3628
    formdata.data = {}
3629
    formdata.just_created()
3630
    formdata.store()
3631
    pub.substitutions.feed(formdata)
3632

  
3633
    item = ExportToModel()
3634
    item.convert_to_pdf = False
3635
    item.method = 'non-interactive'
3636
    template_filename = os.path.join(os.path.dirname(__file__), 'template-with-qrcode.odt')
3637
    with open(template_filename, 'rb') as fd:
3638
        template = fd.read()
3639
    upload = QuixoteUpload('/foo/template.odt', content_type='application/octet-stream')
3640
    upload.fp = io.BytesIO()
3641
    upload.fp.write(template)
3642
    upload.fp.seek(0)
3643
    item.model_file = UploadedFile(pub.app_dir, None, upload)
3644
    item.attach_to_history = True
3645

  
3646
    item.perform(formdata)
3647

  
3648
    assert formdata.evolution[-1].parts[-1].base_filename == 'template.odt'
3649
    with zipfile.ZipFile(formdata.evolution[-1].parts[0].filename, mode='r') as zfile:
3650
        image_filename = [name for name in zfile.namelist() if name.endswith('.jpg')][0]
3651
        with zfile.open(image_filename, 'r') as image_fd:
3652
            with open('/tmp/image.png', 'wb') as fd_write:
3653
                fd_write.write(image_fd.read())
3654
            image_fd.seek(0)
3655
            img = Image.open(image_fd)
3656
            from pyzbar.pyzbar import ZBarSymbol
3657
            from pyzbar.pyzbar import decode as decode_qrcode
3658

  
3659
            assert (
3660
                decode_qrcode(img, symbols=[ZBarSymbol.QRCODE])[0].data.decode()
3661
                == 'http://example.net/backoffice/management/baz/1/'
3662
            )
3663

  
3664

  
3620 3665
def test_export_to_model_backoffice_field(pub):
3621 3666
    wf = Workflow(name='email with attachments')
3622 3667
    wf.backoffice_fields_formdef = WorkflowBackofficeFieldsFormDef(wf)
tox.ini
44 44
    pylint
45 45
    Quixote>=3.0,<3.2
46 46
    pre-commit
47
    pyzbar
47 48
commands =
48 49
    py.test -v {env:COVERAGE:} --junitxml=junit-{envname}.xml {posargs:tests/}
49 50
    pylint: ./pylint.sh wcs/ tests/
wcs/wf/export_to_model.py
588 588

  
589 589
                if node.tag == DRAW_FRAME:
590 590
                    name = node.attrib.get(DRAW_NAME)
591
                    if not self.get_expression(name)['type'] == 'python':
592
                        continue
593 591
                    # variable image
594
                    try:
595
                        variable_image = self.compute(name)
596
                    except Exception:
597
                        continue
598
                    if not hasattr(variable_image, 'get_content'):
592
                    pub = get_publisher()
593
                    with pub.complex_data():
594
                        try:
595
                            variable_image = self.compute(name, allow_complex=True)
596
                        except Exception:
597
                            continue
598
                        complex_variable_image = get_publisher().get_cached_complex_data(variable_image)
599
                    if not hasattr(complex_variable_image, 'get_content'):
599 600
                        continue
600 601
                    image = [x for x in node if x.tag == DRAW_IMAGE][0]
601
                    new_images[image.attrib.get(XLINK_HREF)] = variable_image
602
                    new_images[image.attrib.get(XLINK_HREF)] = complex_variable_image
602 603

  
603 604
                for attr in ('text', 'tail'):
604 605
                    if not getattr(node, attr):
605
-