From 325bcd1c5b5cb44f3165287620b957ea68f18537 Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Mon, 8 Mar 2010 13:59:39 +0530 Subject: [PATCH 1/2] [IMP]:change the way to calculate avg/sum bzr revid: nch@tinyerp.com-20100308082939-crtgxay16njypjt7 --- bin/osv/orm.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 0165d4e7a4a..5904ab5cf3a 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -1871,13 +1871,9 @@ class orm(orm_template): fields_pre = [f for f in float_int_fields if f == self.CONCURRENCY_CHECK_FIELD or (f in self._columns and getattr(self._columns[f], '_classic_write'))] - avg_fields = context.get('avg',[]) for f in fields_pre: if f not in ['id','sequence']: - if f in avg_fields: - flist += ',avg('+f+') as '+f - else: - flist += ',sum('+f+') as '+f + flist += ',sum('+f+') as '+f cr.execute('select min(%s.id) as id,' % self._table + flist + ' from ' + ','.join(tables) + where_clause + ' group by '+ groupby + limit_str + offset_str, where_params) alldata = {} From 8856a549f738f983795d29da330044568147828a Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Mon, 8 Mar 2010 15:10:36 +0530 Subject: [PATCH 2/2] [IMP]:Avg/sum for groupby bzr revid: nch@tinyerp.com-20100308094036-hpvzsvlygup7s0e7 --- bin/osv/fields.py | 20 +++++++++++--------- bin/osv/orm.py | 5 +++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/bin/osv/fields.py b/bin/osv/fields.py index 5d373a168ea..ce02de20a27 100644 --- a/bin/osv/fields.py +++ b/bin/osv/fields.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- ############################################################################## -# +# # OpenERP, Open Source Management Solution # Copyright (C) 2004-2009 Tiny SPRL (). # @@ -15,7 +15,7 @@ # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . +# along with this program. If not, see . # ############################################################################## @@ -80,6 +80,7 @@ class _column(object): self.view_load = 0 self.select = select self.selectable = True + self.group_operator = args.get('group_operator', False) for a in args: if args[a]: setattr(self, a, args[a]) @@ -177,6 +178,7 @@ class float(_column): self.digits = digits self.digits_compute = digits_compute + def digits_change(self, cr): if self.digits_compute: t = self.digits_compute(cr) @@ -320,8 +322,8 @@ class many2one(_column): except except_orm: record_name = {} record_name[record] = '// Access Denied //' - names.update(record_name) - + names.update(record_name) + for r in res.keys(): if res[r] and res[r] in names: res[r] = (res[r], names[res[r]]) @@ -629,7 +631,7 @@ class function(_column): self._multi = multi if 'relation' in args: self._obj = args['relation'] - + self.digits = args.get('digits', (16,2)) self.digits_compute = args.get('digits_compute', None) @@ -642,7 +644,7 @@ class function(_column): if not fnct_search and not store: self.selectable = False - + if store: self._classic_read = True self._classic_write = True @@ -681,14 +683,14 @@ class function(_column): if self._type == "many2one" : # Filtering only integer/long values if passed res_ids = [x for x in res.values() if x and isinstance(x, (int,long))] - + if res_ids: obj_model = obj.pool.get(self._obj) dict_names = dict(obj_model.name_get(cr, user, res_ids, context)) for r in res.keys(): if res[r] and res[r] in dict_names: res[r] = (res[r], dict_names[res[r]]) - + if self._type == 'binary' and context.get('bin_size', False): # convert the data returned by the function with the size of that data... res = dict(map( get_nice_size, res.items())) @@ -838,7 +840,7 @@ class dummy(function): def _fnct_read(self, obj, cr, uid, ids, field_name, args, context=None): return {} - + def __init__(self, *arg, **args): self.arg = arg self._relations = [] diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 5904ab5cf3a..7f412be0a7a 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -1007,7 +1007,7 @@ class orm_template(object): if fields and f not in fields: continue res[f] = {'type': self._columns[f]._type} - for arg in ('string', 'readonly', 'states', 'size', 'required', + for arg in ('string', 'readonly', 'states', 'size', 'required', 'group_operator', 'change_default', 'translate', 'help', 'select', 'selectable'): if getattr(self._columns[f], arg): res[f][arg] = getattr(self._columns[f], arg) @@ -1873,7 +1873,8 @@ class orm(orm_template): or (f in self._columns and getattr(self._columns[f], '_classic_write'))] for f in fields_pre: if f not in ['id','sequence']: - flist += ',sum('+f+') as '+f + operator = fget[f].get('group_operator','sum') + flist += ','+operator+'('+f+') as '+f cr.execute('select min(%s.id) as id,' % self._table + flist + ' from ' + ','.join(tables) + where_clause + ' group by '+ groupby + limit_str + offset_str, where_params) alldata = {}