[IMP] add 'median' function;

pull/111/head
Sylvain LE GAL 2015-04-10 02:26:49 +02:00
parent 30fbca6a3d
commit cff4997c51
4 changed files with 15 additions and 2 deletions

View File

@ -47,7 +47,7 @@ and add them as short cut.
the current user doesn't have access to the given model);
* The tile displays items count of a given model restricted to a given domain;
* Optionnaly, the tile can display the result of a function of a field;
* Function is one of sum/avg/min/max;
* Function is one of sum/avg/min/max/median;
* Field must be integer or float;
Screenshot:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 287 B

After

Width:  |  Height:  |  Size: 340 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 287 B

View File

@ -31,6 +31,14 @@ class tile(orm.Model):
_name = 'tile.tile'
_order = 'sequence, name'
def median(self, aList):
# https://docs.python.org/3/library/statistics.html#statistics.median
# TODO : refactor, using statistics.median when Odoo will be available
# in Python 3.4
even = (0 if len(aList) % 2 else 1) + 1
half = (len(aList) - 1) / 2
return sum(sorted(aList)[half:half + even]) / float(even)
def _get_tile_info(self, cr, uid, ids, fields, args, context=None):
ima_obj = self.pool['ir.model.access']
res = {}
@ -72,6 +80,9 @@ class tile(orm.Model):
elif r.field_function == 'avg':
value = sum(vals) / len(vals)
helper = _("Average value of '%s'" % desc)
elif r.field_function == 'median':
value = self.median(vals)
helper = _("Median value of '%s'" % desc)
res[r.id].update({
'computed_value': value,
'helper': helper,
@ -111,7 +122,9 @@ class tile(orm.Model):
('min', 'Minimum'),
('max', 'Maximum'),
('sum', 'Sum'),
('avg', 'Average')], 'Function'),
('avg', 'Average'),
('median', 'Median'),
], 'Function'),
'field_id': fields.many2one(
'ir.model.fields', 'Field',
domain="[('model_id', '=', model_id),"