diff --git a/web_dashboard_tile/__openerp__.py b/web_dashboard_tile/__openerp__.py index e98d42ce..d0f8cf41 100644 --- a/web_dashboard_tile/__openerp__.py +++ b/web_dashboard_tile/__openerp__.py @@ -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: diff --git a/web_dashboard_tile/static/src/img/avg.png b/web_dashboard_tile/static/src/img/avg.png index 61d5dd7c..2f534e93 100644 Binary files a/web_dashboard_tile/static/src/img/avg.png and b/web_dashboard_tile/static/src/img/avg.png differ diff --git a/web_dashboard_tile/static/src/img/median.png b/web_dashboard_tile/static/src/img/median.png new file mode 100644 index 00000000..61d5dd7c Binary files /dev/null and b/web_dashboard_tile/static/src/img/median.png differ diff --git a/web_dashboard_tile/tile.py b/web_dashboard_tile/tile.py index f206dc97..d081dd0c 100644 --- a/web_dashboard_tile/tile.py +++ b/web_dashboard_tile/tile.py @@ -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),"