Browse Source

[IMP] make one search_read call instead of two calls

pull/283/merge
Sylvain LE GAL 7 years ago
parent
commit
dc9c69a96f
  1. 30
      web_dashboard_tile/models/tile_tile.py

30
web_dashboard_tile/models/tile_tile.py

@ -155,38 +155,30 @@ class TileTile(models.Model):
self.primary_value = self.secondary_value = 'ERR!' self.primary_value = self.secondary_value = 'ERR!'
self.error = str(e) self.error = str(e)
return return
if any([
self.primary_function and
self.primary_function != 'count',
self.secondary_function and
self.secondary_function != 'count'
]):
records = model.search(eval(domain, eval_context))
fields = [f.name for f in [
self.primary_field_id, self.secondary_field_id] if f]
read_vals = model.search_read(eval(domain, eval_context), fields)
for f in ['primary_', 'secondary_']: for f in ['primary_', 'secondary_']:
f_function = f+'function' f_function = f+'function'
f_field_id = f+'field_id' f_field_id = f+'field_id'
f_format = f+'format' f_format = f+'format'
f_value = f+'value' f_value = f+'value'
value = 0 value = 0
if self[f_function] == 'count':
value = count
elif self[f_function]:
func = FIELD_FUNCTIONS[self[f_function]]['func']
if func and self[f_field_id] and count:
field_name = self[f_field_id].name
read_vals = records.search_read(
[('id', 'in', records.ids)], [field_name])
vals = [x[field_name] for x in read_vals]
if not self[f_function]:
self[f_value] = False
else:
if self[f_function] == 'count':
value = count
else:
func = FIELD_FUNCTIONS[self[f_function]]['func']
vals = [x[self[f_field_id].name] for x in read_vals]
value = func(vals) value = func(vals)
if self[f_function]:
try: try:
self[f_value] = (self[f_format] or '{:,}').format(value) self[f_value] = (self[f_format] or '{:,}').format(value)
except ValueError as e: except ValueError as e:
self[f_value] = 'F_ERR!' self[f_value] = 'F_ERR!'
self.error = str(e) self.error = str(e)
return return
else:
self[f_value] = False
@api.one @api.one
@api.onchange('primary_function', 'primary_field_id', @api.onchange('primary_function', 'primary_field_id',

Loading…
Cancel
Save