3
0
Fork 0

[IMP][8.0][web_dashboard_tile] Refactor (see changes in description) (#476)

Conflicts:
	web_dashboard_tile/__openerp__.py
	web_dashboard_tile/models/tile_tile.py
9.0
Iván Todorovich 2016-11-19 14:07:01 -03:00 committed by Nicolas Mac Rouillon
parent fb3f97df0e
commit 6524ddcbd4
2 changed files with 56 additions and 58 deletions

View File

@ -142,25 +142,24 @@ class TileTile(models.Model):
string='Error Details', string='Error Details',
compute='_compute_data') compute='_compute_data')
@api.multi @api.one
def _compute_data(self): def _compute_data(self):
eval_context = self._get_eval_context() if not self.active:
for rec in self:
if not rec.active:
return return
domain = rec.domain or '[]' model = self.env[self.model_id.model]
model = rec.env[rec.model_id.model] eval_context = self._get_eval_context()
domain = self.domain or '[]'
try: try:
count = model.search_count(eval(domain, eval_context)) count = model.search_count(eval(domain, eval_context))
except Exception as e: except Exception as e:
rec.primary_value = rec.secondary_value = 'ERR!' self.primary_value = self.secondary_value = 'ERR!'
rec.error = str(e) self.error = str(e)
return return
if any([ if any([
rec.primary_function and self.primary_function and
rec.primary_function != 'count', self.primary_function != 'count',
rec.secondary_function and self.secondary_function and
rec.secondary_function != 'count' self.secondary_function != 'count'
]): ]):
records = model.search(eval(domain, eval_context)) records = model.search(eval(domain, eval_context))
for f in ['primary_', 'secondary_']: for f in ['primary_', 'secondary_']:
@ -169,43 +168,42 @@ class TileTile(models.Model):
f_format = f + 'format' f_format = f + 'format'
f_value = f + 'value' f_value = f + 'value'
value = 0 value = 0
if rec[f_function] == 'count': if self[f_function] == 'count':
value = count value = count
elif rec[f_function]: elif self[f_function]:
func = FIELD_FUNCTIONS[rec[f_function]]['func'] func = FIELD_FUNCTIONS[self[f_function]]['func']
if func and rec[f_field_id] and count: if func and self[f_field_id] and count:
vals = [x[rec[f_field_id].name] for x in records] vals = [x[self[f_field_id].name] for x in records]
value = func(vals) value = func(vals)
if rec[f_function]: if self[f_function]:
try: try:
rec[f_value] = (rec[f_format] or '{:,}').format(value) self[f_value] = (self[f_format] or '{:,}').format(value)
except ValueError as e: except ValueError as e:
rec[f_value] = 'F_ERR!' self[f_value] = 'F_ERR!'
rec.error = str(e) self.error = str(e)
return return
else: else:
rec[f_value] = False self[f_value] = False
@api.multi @api.one
@api.onchange('primary_function', 'primary_field_id', @api.onchange('primary_function', 'primary_field_id',
'secondary_function', 'secondary_field_id') 'secondary_function', 'secondary_field_id')
def _compute_helper(self): def _compute_helper(self):
for rec in self:
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_helper = f + 'helper' f_helper = f + 'helper'
rec[f_helper] = '' self[f_helper] = ''
field_func = FIELD_FUNCTIONS.get(rec[f_function], {}) field_func = FIELD_FUNCTIONS.get(self[f_function], {})
help = field_func.get('help', False) help = field_func.get('help', False)
if help: if help:
if rec[f_function] != 'count' and rec[f_field_id]: if self[f_function] != 'count' and self[f_field_id]:
desc = rec[f_field_id].field_description desc = self[f_field_id].field_description
rec[f_helper] = help % desc self[f_helper] = help % desc
else: else:
rec[f_helper] = help self[f_helper] = help
@api.multi @api.one
def _compute_active(self): def _compute_active(self):
ima = self.env['ir.model.access'] ima = self.env['ir.model.access']
for rec in self: for rec in self: