workflows: update export to models for py3 (#36515)

This commit is contained in:
Frédéric Péters 2019-11-15 16:54:02 +01:00
parent 85a4024417
commit 129306e47d
2 changed files with 13 additions and 6 deletions

View File

@ -3321,7 +3321,7 @@ def test_formdata_generated_document_odt_download(pub, odt_template):
with open(os.path.join(os.path.dirname(__file__), 'template-out.odt'), 'rb') as f:
body = resp.click(odt_template, index=0).follow().body
assert_equal_zip(BytesIO(body), f)
assert resp.click('test.rtf', index=0).follow().body == 'HELLO NEW WORLD'
assert resp.click('test.rtf', index=0).follow().body == b'HELLO NEW WORLD'
def test_formdata_generated_document_odt_download_with_substitution_variable(pub):
create_user_and_admin(pub)
@ -3399,7 +3399,7 @@ def test_formdata_generated_document_odt_download_with_substitution_variable(pub
body = resp.click('template.odt', index=0).follow().body
assert_equal_zip(BytesIO(body), f)
response2 = resp.click('test.rtf', index=0).follow()
assert response2.body == 'HELLO NEW WORLD'
assert response2.body == b'HELLO NEW WORLD'
# Test attachment substitution variables
variables = formdef.data_class().select()[0].get_substitution_variables()
assert 'attachments' in variables

View File

@ -23,6 +23,7 @@ import subprocess
import tempfile
import shutil
from django.utils import six
from django.utils.encoding import force_bytes, force_text
from django.utils.six import BytesIO, StringIO
@ -403,11 +404,11 @@ class ExportToModel(WorkflowStatusItem):
try:
# force ezt_only=True because an RTF file may contain {{ characters
# and would be seen as a Django template
return StringIO(template_on_formdata(
return BytesIO(force_bytes(template_on_formdata(
formdata,
force_text(self.model_file.get_file().read()),
ezt_format=ezt.FORMAT_RTF,
ezt_only=True))
ezt_only=True)))
except TemplateError as e:
url = formdata.get_url()
get_logger().error('error in template for export to model [%s]: %s' % (url, str(e)))
@ -463,11 +464,17 @@ class ExportToModel(WorkflowStatusItem):
# we would also need to copy its style and what not).
current_tail = node.tail or ''
node.tail = None
as_str = ET.tostring(node).replace('\n\n',
as_str = force_str(ET.tostring(node)).replace('\n\n',
2 * ('<nsa:line-break xmlns:nsa="%(ns)s"/>' % {'ns': OO_TEXT_NS}))
as_node = ET.fromstring(as_str)
node.text = as_node.text
node._children = as_node._children
if six.PY2:
node._children = as_node._children
else:
for child in node.getchildren():
node.remove(child)
for child in as_node.getchildren():
node.append(child)
node.tail = current_tail
outstream = BytesIO()