1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from . import XMLObjects
from Products.Formulator.TALESField import TALESMethod
from Products.Formulator.MethodField import Method
import six
def XMLToForm(s, form, override_encoding=None):
"""Takes an xml string and changes formulator form accordingly.
Heavily inspired by code from Nikolay Kim.
If override_encoding is set, form data is read assuming given
encoding instead of the one in the XML data itself. The form will
have to be modified afterwards to this stored_encoding itself.
"""
top = XMLObjects.XMLToObjectsFromString(s)
# wipe out groups
form.groups = {'Default':[]}
form.group_list = ['Default']
if override_encoding is None:
try:
unicode_mode = top.first.form.first.unicode_mode.text
except AttributeError:
unicode_mode = 'false'
# retrieve encoding information from XML
if unicode_mode == 'true':
# just use unicode strings being read in
encoding = None
else:
# store strings as specified encoding
try:
encoding = top.first.form.first.stored_encoding.text
except AttributeError:
encoding = 'ISO-8859-1'
else:
if override_encoding == 'unicode':
encoding = None
else:
encoding = override_encoding
# get the settings
settings = [field.id for field in form.settings_form.get_fields()]
for setting in settings:
value = getattr(top.first.form.first, setting, None)
if value is None:
continue
if setting == 'unicode_mode':
v = value.text == 'true'
elif setting == 'row_length':
v = int(value.text)
else:
v = encode(value.text, encoding)
setattr(form, setting, v)
# create groups
has_default = 0
for group in top.first.form.first.groups.elements.group:
# get group title and create group
group_title = encode(group.first.title.text, encoding)
if group_title == 'Default':
has_default = 1
form.add_group(group_title)
# create fields in group
if not hasattr(group.first.fields.elements, 'field'):
# empty <fields> element
continue
for entry in group.first.fields.elements.field:
id = str(encode(entry.first.id.text, encoding))
meta_type = encode(entry.first.type.text, encoding)
try:
form._delObject(id)
except (KeyError, AttributeError):
pass
form.manage_addField(id, '', meta_type)
field = form._getOb(id)
if group_title != 'Default':
form.move_field_group([id], 'Default', group_title)
# set values
values = entry.first.values
for name in values.getElementNames():
value = getattr(values.first, name)
if value.attributes.get('type') == 'float':
field.values[name] = float(value.text)
elif value.attributes.get('type') == 'int':
field.values[name] = int(value.text)
elif value.attributes.get('type') == 'method': # XXX Patch
field.values[name] = Method(value.text) # XXX Patch
elif value.attributes.get('type') == 'list':
# XXX bare eval here (this may be a security leak ?)
field.values[name] = eval(
encode(value.text, encoding))
else:
field.values[name] = encode(value.text, encoding)
# set tales
tales = entry.first.tales
for name in tales.getElementNames():
field.tales[name] = TALESMethod(
encode(getattr(tales.first, name).text, encoding))
# set messages
if hasattr(entry.first, 'messages'):
messages = entry.first.messages
entries = getattr(messages.elements, 'message', [])
for entry in entries:
name = entry.attributes.get('name')
text = encode(entry.text, encoding)
field.message_values[name] = text
# set delegated_list, mean ProxyField
is_proxy_field = False
if hasattr(entry.first, 'delegated_list'):
delegated_list_element = entry.first.delegated_list
delegated_list = delegated_list_element.getElementNames()
field.delegated_list = delegated_list
is_proxy_field = True
# for persistence machinery
field.values = field.values
field.tales = field.tales
field.message_values = field.message_values
if is_proxy_field:
field.delegated_list = field.delegated_list
# delete default group
if not has_default:
form.move_group_down('Default')
form.remove_group('Default')
def encode(text, encoding):
if six.PY3 or encoding is None:
return text
else:
return text.encode(encoding)