Commit e33f1b6c authored by Jérome Perrin's avatar Jérome Perrin

Formulator: repair FormToXML for py2

In 1b555dbf (py2/py3: Make Products code compatible with both python2
and python3 (nexedi/erp5!1751)., 2024-07-16) we changed the code in a
way that on python2 it would first node text to a python2 str and only
later convert it to unicode, but setting text to a str during the first
step can fail with:

    ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters

This change to set directly unicode on py2.
parent 007de00c
...@@ -21,9 +21,7 @@ def formToXML(form, prologue=1): ...@@ -21,9 +21,7 @@ def formToXML(form, prologue=1):
else: else:
value = 'false' value = 'false'
sub_element = SubElement(form_as_xml, id) sub_element = SubElement(form_as_xml, id)
sub_element.text = str(value) sub_element.text = str2unicode(str(value), encoding)
if six.PY2:
sub_element.text = str2unicode(sub_element.text, encoding)
groups = SubElement(form_as_xml, 'groups') groups = SubElement(form_as_xml, 'groups')
# export form groups # export form groups
for group in form.get_groups(include_empty=1): for group in form.get_groups(include_empty=1):
...@@ -59,24 +57,19 @@ def formToXML(form, prologue=1): ...@@ -59,24 +57,19 @@ def formToXML(form, prologue=1):
if not isinstance(value, six.string_types): if not isinstance(value, six.string_types):
value = str(value) value = str(value)
value_element = SubElement(values_element, key) value_element = SubElement(values_element, key)
value_element.text = str(value) value_element.text = str2unicode(str(value), encoding)
if six.PY2:
value_element.text = str2unicode(value_element.text, encoding)
tales_element = SubElement(field_element, 'tales') tales_element = SubElement(field_element, 'tales')
items = sorted(field.tales.items()) items = sorted(field.tales.items())
for key, value in items: for key, value in items:
if value: if value:
tale_element = SubElement(tales_element, key) tale_element = SubElement(tales_element, key)
tale_element.text = str(value._text) tale_element.text = str2unicode(str(value._text), encoding)
if six.PY2:
tale_element.text = str2unicode(tale_element.text, encoding)
messages = SubElement(field_element, 'messages') messages = SubElement(field_element, 'messages')
for message_key in field.get_error_names(): for message_key in field.get_error_names():
message_element = SubElement(messages, 'message', name=message_key) message_element = SubElement(messages, 'message', name=message_key)
message_element.text = field.get_error_message(message_key) message_element.text = str2unicode(field.get_error_message(message_key), encoding)
if six.PY2:
message_element.text = str2unicode(message_element.text, encoding)
# Special attribute for ProxyFields *delegated_list* # Special attribute for ProxyFields *delegated_list*
delegated_list = getattr(field, 'delegated_list', []) delegated_list = getattr(field, 'delegated_list', [])
if delegated_list: if delegated_list:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment