summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Morel <xmo@openerp.com>2014-09-11 11:23:13 +0200
committerXavier Morel <xmo@openerp.com>2014-10-06 19:13:45 +0200
commitc5dca416daf8441f57564fdd77beeaa1b27f5bdb (patch)
tree6a8326dd95d6e1b45212cbc9fa288e8b7b23876a
parentd5e3d121e3733de189605da7f830ff3d7c20b8d8 (diff)
[IMP] qweb: foreach handling
* fix mapping handling to match JS impl: current value set as _value instead of being lost * add handling of integer parameter * only set _size and _last if current iterable is sized
-rw-r--r--openerp/addons/base/ir/ir_qweb.py21
1 files changed, 16 insertions, 5 deletions
diff --git a/openerp/addons/base/ir/ir_qweb.py b/openerp/addons/base/ir/ir_qweb.py
index 1508baff9d8..8504c155ac4 100644
--- a/openerp/addons/base/ir/ir_qweb.py
+++ b/openerp/addons/base/ir/ir_qweb.py
@@ -362,29 +362,40 @@ class QWeb(orm.AbstractModel):
inner = widget.format(template_attributes['esc'], options, qwebcontext)
return self.render_element(element, template_attributes, generated_attributes, qwebcontext, inner)
+ def _iterate(self, iterable):
+ if isinstance (iterable, collections.Mapping):
+ return iterable.iteritems()
+
+ return itertools.izip(*itertools.tee(iterable))
+
def render_tag_foreach(self, element, template_attributes, generated_attributes, qwebcontext):
expr = template_attributes["foreach"]
enum = self.eval_object(expr, qwebcontext)
if enum is None:
template = qwebcontext.get('__template__')
raise QWebException("foreach enumerator %r is not defined while rendering template %r" % (expr, template), template=template)
+ if isinstance(enum, int):
+ enum = range(enum)
varname = template_attributes['as'].replace('.', '_')
copy_qwebcontext = qwebcontext.copy()
- size = -1
+
+ size = None
if isinstance(enum, collections.Sized):
size = len(enum)
- copy_qwebcontext["%s_size" % varname] = size
+ copy_qwebcontext["%s_size" % varname] = size
+
copy_qwebcontext["%s_all" % varname] = enum
ru = []
- for index, item in enumerate(enum):
+ for index, (item, value) in enumerate(self._iterate(enum)):
copy_qwebcontext.update({
varname: item,
- '%s_value' % varname: item,
+ '%s_value' % varname: value,
'%s_index' % varname: index,
'%s_first' % varname: index == 0,
- '%s_last' % varname: index + 1 == size,
})
+ if size is not None:
+ copy_qwebcontext['%s_last' % varname] = index + 1 == size
if index % 2:
copy_qwebcontext.update({
'%s_parity' % varname: 'odd',