From e68c9e99b6286fa71067dcf32f1780ed02164c43 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Tue, 18 Dec 2012 10:26:17 +0100 Subject: [PATCH] Now supports non-indexed arrays of objects as GET parameters --- wsme/rest/args.py | 48 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/wsme/rest/args.py b/wsme/rest/args.py index 1b918da..8d4f79c 100644 --- a/wsme/rest/args.py +++ b/wsme/rest/args.py @@ -86,24 +86,50 @@ def array_from_params(datatype, params, path, hit_paths): return [ from_param(datatype.item_type, value) for value in params.getall(path)] - else: - indexes = set() - r = re.compile('^%s\[(?P\d+)\]' % re.escape(path)) + if iscomplex(datatype.item_type): + attributes = set() + r = re.compile('^%s\.(?P[^\.])' % re.escape(path)) for p in params.keys(): m = r.match(p) if m: - indexes.add(int(m.group('index'))) + attributes.add(m.group('attrname')) + if attributes: + value = [] + for attrdef in list_attributes(datatype.item_type): + attrpath = '%s.%s' % (path, attrdef.key) + hit_paths.add(attrpath) + attrvalues = params.getall(attrpath) + if len(value) < len(attrvalues): + value[-1:] = [ + datatype.item_type() + for i in xrange(len(attrvalues) - len(value)) + ] + for i, attrvalue in enumerate(attrvalues): + setattr( + value[i], + attrdef.key, + from_param(attrdef.datatype, attrvalue) + ) + return value - if not indexes: - return Unset + indexes = set() + r = re.compile('^%s\[(?P\d+)\]' % re.escape(path)) - indexes = list(indexes) - indexes.sort() + for p in params.keys(): + m = r.match(p) + if m: + indexes.add(int(m.group('index'))) - return [from_params(datatype.item_type, params, - '%s[%s]' % (path, index), hit_paths) - for index in indexes] + if not indexes: + return Unset + + indexes = list(indexes) + indexes.sort() + + return [from_params(datatype.item_type, params, + '%s[%s]' % (path, index), hit_paths) + for index in indexes] @from_params.when_type(DictType)