Projet

Général

Profil

0001-workflows-force-inclusion-of-openformula-namespace-i.patch

Frédéric Péters, 11 juillet 2022 22:50

Télécharger (12,4 ko)

Voir les différences:

Subject: [PATCH] workflows: force inclusion of openformula namespace in ods
 documents (#66352)

 tests/form_pages/test_formdata.py |  48 ++++++++++++++++++++++++++++++
 tests/template.ods                | Bin 0 -> 8232 bytes
 wcs/wf/export_to_model.py         |  13 ++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 tests/template.ods
tests/form_pages/test_formdata.py
658 658
    assert resp.request.environ['PATH_INFO'].endswith(file2.filename)
659 659

  
660 660

  
661
def test_formdata_generated_document_ods_download(pub):
662
    create_user(pub)
663
    wf = Workflow(name='status')
664
    st1 = wf.add_status('Status1', 'st1')
665
    export_to = st1.add_action('export_to_model', id='_export_to')
666
    export_to.convert_to_pdf = False
667
    export_to.label = 'create doc'
668
    template_filename = os.path.join(os.path.dirname(__file__), '..', 'template.ods')
669
    with open(template_filename, 'rb') as fd:
670
        template = fd.read()
671
    upload = QuixoteUpload('/foo/template.ods', content_type='application/octet-stream')
672
    upload.fp = io.BytesIO()
673
    upload.fp.write(template)
674
    upload.fp.seek(0)
675
    export_to.model_file = UploadedFile(pub.app_dir, None, upload)
676
    export_to.by = ['_submitter']
677
    wf.store()
678

  
679
    FormDef.wipe()
680
    formdef = FormDef()
681
    formdef.name = 'test_formdata_generated_document_ods_download'
682
    formdef.url_name = 'test'
683
    formdef.workflow_id = wf.id
684
    formdef.fields = []
685
    formdef.store()
686
    formdef.data_class().wipe()
687

  
688
    resp = login(get_app(pub), username='foo', password='foo').get('/test/')
689
    resp = resp.forms[0].submit('submit')  # -> validation
690
    resp = resp.forms[0].submit('submit')  # -> submit
691
    resp = resp.follow()
692

  
693
    resp = resp.form.submit('button_export_to')
694

  
695
    resp = resp.follow()  # $form/$id/create_doc
696
    resp = resp.follow()  # $form/$id/create_doc/
697
    with io.BytesIO(resp.body) as generated_ods:
698
        with zipfile.ZipFile(generated_ods) as z_ods:
699
            for name in z_ods.namelist():
700
                if name != 'content.xml':
701
                    continue
702
                content_bytes = z_ods.read(name)
703
                assert b' xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" ' in content_bytes
704
                assert b'>test_formdata_generated_document_ods_download<' in content_bytes
705
                # check it's valid XML
706
                ET.tostring(ET.XML(content_bytes))
707

  
708

  
661 709
@pytest.mark.skipif(transform_to_pdf is None, reason='libreoffice not found')
662 710
def test_formdata_generated_document_odt_to_pdf_download(pub):
663 711
    create_user(pub)
wcs/wf/export_to_model.py
144 144
            root = ET.fromstring(content)
145 145
            process(root, new_images)
146 146
            content = ET.tostring(root)
147
            if (
148
                root.find(f'{{{OO_OFFICE_NS}}}body/{{{OO_OFFICE_NS}}}spreadsheet')
149
                and b'xmlns:of=' not in content
150
            ):
151
                # force xmlns:of namespace inclusion in spreadsheet files, as it may be
152
                # required for proper handling of table:formula attributes.
153
                # (there is no easy way to have ElementTree include namespace declarations
154
                # if there are no elements of that namespace)
155
                content = content.replace(
156
                    b':document-content ',
157
                    b':document-content xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" ',
158
                    1,
159
                )
147 160
            zout.writestr(filename, content)
148 161

  
149 162
        for filename in zin.namelist():
150
-