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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
###############################################################################
#
# Copyright (C) 2007-TODAY OpenERP SA. All Rights Reserved.
#
# $Id$
#
# Developed by OpenERP (http://openerp.com) and Axelor (http://axelor.com).
#
# The OpenERP web client is distributed under the "OpenERP Public License".
# It's based on Mozilla Public License Version (MPL) 1.1 with following
# restrictions:
#
# - All names, links and logos of OpenERP must be kept as in original
# distribution without any changes in all software screens, especially
# in start-up page and the software header, even if the application
# source code has been changed or updated or code has been added.
#
# You can see the MPL licence at: http://www.mozilla.org/MPL/MPL-1.1.html
#
###############################################################################
import itertools
import re
from openerp import validators
import formencode
import openobject
def _make_dict(data, is_params=False):
"""If is_params is True then generates a TinyDict otherwise generates a valid
dictionary from the given data to be used with OpenERP.
@param data: data in the form of {'a': 1, 'b/x': 1, 'b/y': 2}
@param is_params: if True generate TinyDict instead of standard dict
@return: TinyDict or dict
"""
res = (is_params or {}) and TinyDict()
for name, value in data.items():
#XXX: safari 3.0 submits selection field even if no `name` attribute
if not name:
continue
if isinstance(name, basestring) and '/' in name:
names = name.split('/')
res.setdefault(names[0], (is_params or {}) and TinyDict()).update({"/".join(names[1:]): value})
else:
res[name] = value
for k, v in res.items():
if isinstance(v, dict):
if not is_params and '__id' in v:
id = v.pop('__id') or 0
id = int(id)
values = _make_dict(v, is_params)
if values and any(values.itervalues()):
res[k] = [(id and 1, id, values)]
else:
res[k] = []
else:
res[k] = _make_dict(v, is_params and isinstance(v, TinyDict))
return res
class TinyDict(dict):
"""A dictionary class that allows accessing it's items as it's attributes.
It also converts stringified Boolean, None, Number or secuence to python object.
This class is mainly used by Controllers to get special `_terp_` arguments and
to generate valid dictionary of data fields from the controller keyword arguments.
"""
def __init__(self, **kwargs):
super(TinyDict, self).__init__()
for k, v in kwargs.items():
if (isinstance(v, dict) and not isinstance(v, TinyDict)):
v = TinyDict(**v)
self[k] = v
def _eval(self, value):
if isinstance(value, list):
for i, v in enumerate(value):
value[i] = self._eval(v)
return value
if not isinstance(value, basestring):
return value
pat = re.compile('^(True|False|None|-?\d+(\.\d+)?|\[.*?\]|\(.*?\)|\{.*?\})$', re.M)
if pat.match(value):
try:
return eval(value)
except:
pass
return value
def __setattr__(self, name, value):
name = '_terp_%s' % name
value = self._eval(value)
self[name] = value
def __getattr__(self, name):
nm = '_terp_%s' % name
return self.get(nm, self.get(name, None))
def __setitem__(self, name, value):
value = self._eval(value)
super(TinyDict, self).__setitem__(name, value)
def update(self, d=(), **kwargs):
if isinstance(d, dict):
seq = d.iteritems()
else:
seq = d
for k, v in itertools.chain(seq, kwargs.iteritems()):
self[k] = v
def updateAttrs(self, d=(), **kwattrs):
""" Updates the TinyDict's attrs in bulk, as if using attr access (rather than item access which can be
performed via setitem)
"""
if isinstance(d, dict):
seq = d.iteritems()
else:
seq = d
for k, v in itertools.chain(seq, kwattrs.iteritems()):
setattr(self, k, v)
def chain_get(self, name, default=None):
names = re.split('\.|/', ustr(name))
value = super(TinyDict, self).get(names[0], default)
for n in names[1:]:
if isinstance(value, TinyDict):
value = value.get(n, default)
return value
@staticmethod
def split(kwargs):
"""A helper function to extract special parameters from the given kwargs.
@param kwargs: dict of keyword arguments
@rtype: tuple
@return: tuple of dicts, (TinyDict, dict of data)
"""
params = TinyDict()
data = {}
for n, v in kwargs.items():
if n.find('_terp_') > -1:
params[n] = v
else:
data[n] = v
return _make_dict(params, True), _make_dict(data, False)
def make_plain(self, prefix=''):
res = {}
def _plain(data, prefix):
for k, v in data.items():
if isinstance(v, dict) and not k.startswith('_terp_'):
_plain(v, prefix + k +'/')
else:
res[prefix + k] = v
_plain(self, prefix)
return res
def make_dict(self):
res = {}
for k, v in self.items():
if isinstance(v, TinyDict):
v = v.make_dict()
res[k] = v
return res
def __getstate__(self):
return self.__dict__
def __setstate__(self, d):
self.__dict__.update(d)
_VALIDATORS = {
'date': lambda *a: validators.DateTime(kind="date"),
'time': lambda *a: validators.DateTime(kind="time"),
'datetime': lambda *a: validators.DateTime(kind="datetime"),
'float_time': lambda *a: validators.FloatTime(),
'float': lambda *a: validators.Float(),
'integer': lambda *a: validators.Int(),
'selection': lambda *a: validators.Selection(),
'char': lambda *a: validators.String(),
'boolean': lambda *a: validators.Bool(),
'reference': lambda *a: validators.Reference(),
'binary': lambda *a: validators.Binary(),
'text': lambda *a: validators.String(),
'text_tag': lambda *a: validators.String(),
'many2many': lambda *a: validators.many2many(),
'one2many': lambda *a: validators.one2many(),
'many2one': lambda *a: validators.many2one(),
'email' : lambda *a: validators.Email(),
'url' : lambda *a: validators.URL(),
'picture': lambda *a: validators.Binary(),
}
class TinyFormError(formencode.api.Invalid):
def __init__(self, field, msg, value):
formencode.api.Invalid.__init__(self, msg, value, state=None, error_list=None, error_dict=None)
self.field = field
class TinyForm(object):
"""An utility class to convert:
1. local form data to the server data (throws exception if any)
2. server data to the local data
Using validators.
"""
def __init__(self, **kwargs):
self.data = {}
for k, v in kwargs.items():
if '_terp_' not in k:
try:
v = eval(v)
except:
pass
self.data['_terp_form/' + k] = v
def _convert(self, form=True, safe=False):
kw = {}
for name, attrs in self.data.items():
if not isinstance(attrs, dict):
kw[name] = attrs
continue
kind = attrs.get('type', 'char')
value = attrs.get('value')
required = attrs.get('required', False)
if kind == "one2many":
try:
value = eval(value)
if value:
if not isinstance(value, list):
value = [value]
from openerp.utils import rpc
proxy = rpc.RPCProxy(attrs['relation'])
res = proxy.read(value, [], rpc.session.context)
res1 = proxy.fields_get(False, rpc.session.context)
for values in res:
for key, val in values.items():
if key in res1.keys():
if res1[key]['type'] == 'many2many':
values[key] = [(6, 0, val)]
value = []
for r in res:
id = r.pop('id')
value += [(1, id, r)]
else:
value = []
except:
pass
elif kind not in _VALIDATORS:
kind = 'char'
v = _VALIDATORS.get(kind, openobject.validators.DefaultValidator)()
if kind == "float" and attrs.get("digit"):
v = validators.Float(digit=attrs.get("digit"))
v.not_empty = (required or False) and True
try:
if form:
value = v.to_python(value, None)
else:
value = v.from_python(value, None)
except formencode.api.Invalid, e:
if form and not safe:
raise TinyFormError(name.replace('_terp_form/', ''), e.msg, e.value)
kw[name] = value
# Prevent auto conversion from TinyDict
_eval = TinyDict._eval
TinyDict._eval = lambda self, v: v
try:
params, data = TinyDict.split(kw)
params = params.form or {}
return TinyDict(**params)
finally:
TinyDict._eval = _eval
def from_python(self):
return self._convert(False)
def to_python(self, safe=False):
return self._convert(True, safe=safe)
if __name__ == "__main__":
kw = {'_terp_view_ids': "[False, 45]",
'view_ids/_terp_view_ids': '[False, False]',
'view_ids/child/_terp_view_ids': '[112, 111]'
}
params, data = TinyDict.split(kw)
params.domain = "[1]"
params.setdefault('domain', 'something...')
params.context = "{}"
params['context'] = "{'id': False}"
print params
print params.view_ids
print params.chain_get('view_ids')
print params.chain_get('view_ids.child')
print params.chain_get('view_ids.child').view_ids
# vim: ts=4 sts=4 sw=4 si et
|