handle dict and UserType as input from forms

This commit is contained in:
Christophe de Vienne 2012-02-16 19:01:28 +01:00
parent dddf0c4896
commit ff919cbba5

View File

@ -3,7 +3,7 @@ import re
from simplegeneric import generic from simplegeneric import generic
from wsme.types import iscomplex, list_attributes, Unset from wsme.types import iscomplex, list_attributes, Unset, UserType
from wsme.utils import parse_isodate, parse_isotime, parse_isodatetime from wsme.utils import parse_isodate, parse_isotime, parse_isodatetime
@ -30,6 +30,12 @@ def datetime_from_param(datatype, value):
return parse_isodatetime(value) if value else None return parse_isodatetime(value) if value else None
@from_param.when_type(UserType)
def usertype_from_param(datatype, value):
return datatype.frombasetype(
from_param(datatype.basetype, value))
@generic @generic
def from_params(datatype, params, path): def from_params(datatype, params, path):
if iscomplex(datatype): if iscomplex(datatype):
@ -74,3 +80,23 @@ def array_from_params(datatype, params, path):
return [from_params(datatype[0], params, '%s[%s]' % (path, index)) return [from_params(datatype[0], params, '%s[%s]' % (path, index))
for index in indexes] for index in indexes]
@from_params.when_type(dict)
def dict_from_params(datatype, params, path):
keys = set()
r = re.compile('^%s\[(?P<key>\w+)\]' % path)
for p in params.keys():
m = r.match(p)
if m:
keys.add(from_param(datatype.keys()[0], m.group('key')))
if not keys:
return Unset
return dict((
(key, from_params(datatype.values()[0],
params, '%s[%s]' % (path, key)))
for key in keys))