From a141f9dadbd71afc8d4f286f5cf4b8ea5af10360 Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Mon, 28 Dec 2009 11:37:29 +0530 Subject: [PATCH 01/11] read_group method for group by bzr revid: nch@tinyerp.com-20091228060729-2tvfbsxxyq829ulb --- bin/osv/orm.py | 61 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 280b5020704..9a05def5377 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.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 . # ############################################################################## @@ -212,7 +212,7 @@ class browse_record(object): # create browse records for 'remote' objects for data in datas: if len(str(data['id']).split('-')) > 1: - data['id'] = int(str(data['id']).split('-')[0]) + data['id'] = int(str(data['id']).split('-')[0]) for n, f in ffields: if f._type in ('many2one', 'one2one'): if data[n]: @@ -348,6 +348,41 @@ class orm_template(object): CONCURRENCY_CHECK_FIELD = '__last_update' + def read_group(self, cr, user, domain, fields, order, offset=0, limit=None, context=None): + context = context or {} + # compute the where, order by, limit and offset clauses + (qu1, qu2, tables) = self._where_calc(cr, user, domain, context=context) + if len(qu1): + qu1 = ' where '+string.join(qu1, ' and ') + else: + qu1 = '' + if order: + self._check_qorder(order) + order_by = order or self._order + # sumof = ','.join(['sum('+field_name+')' for field_name,values in fields.items() if values['type'] in ('float','integer')]) # filter fields in args to only get float and int + sumof = ','.join([field_name for field_name,values in fields.items() if values['type'] in ('float','integer')]) # filter fields in args to only get float and int + + limit_str = limit and ' limit %d' % limit or '' + offset_str = offset and ' offset %d' % offset or '' + + # construct a clause for the rules : + d1, d2 = self.pool.get('ir.rule').domain_get(cr, user, self._name) + if d1: + qu1 = qu1 and qu1+' and '+d1 or ' where '+d1 + qu2 += d2 + + # execute the "main" query to fetch the ids we were searching for + cr.execute('select '+order_by+','+sumof+' from %s' % self._table + qu1+' group by '+order_by+','+sumof+limit_str+offset_str, qu2) + result = [] + for x in cr.fetchall(): + result.append({ + 'name':x[0], + 'domain':[(order_by,'=',x[0])], + 'fields': {x[0]:x[1]} + }) + print result + # return result + def _field_create(self, cr, context={}): cr.execute("SELECT id FROM ir_model WHERE model=%s", (self._name,)) if not cr.rowcount: @@ -465,7 +500,7 @@ class orm_template(object): elif field_type == 'boolean': return False return '' - + def selection_field(in_field): col_obj = self.pool.get(in_field.keys()[0]) if f[i] in col_obj._columns.keys(): @@ -473,8 +508,8 @@ class orm_template(object): elif f[i] in col_obj._inherits.keys(): selection_field(col_obj._inherits) else: - return False - + return False + lines = [] data = map(lambda x: '', range(len(fields))) @@ -501,7 +536,7 @@ class orm_template(object): else: r = r[f[i]] # To display external name of selection field when its exported - if not context.get('import_comp',False):# Allow external name only if its not import compatible + if not context.get('import_comp',False):# Allow external name only if its not import compatible cols = False if f[i] in self._columns.keys(): cols = self._columns[f[i]] @@ -1130,7 +1165,7 @@ class orm_template(object): # running -> done = signal_next (role Z) # running -> cancel = signal_cancel (role Z) - # As we don't know the object state, in this scenario, + # As we don't know the object state, in this scenario, # the button "signal_cancel" will be always shown as there is no restriction to cancel in draft # the button "signal_next" will be show if the user has any of the roles (X Y or Z) # The verification will be made later in workflow process... @@ -1929,7 +1964,7 @@ class orm(orm_template): cr.execute('ALTER TABLE "%s" RENAME "%s" TO "%s"' % ( self._table,f.oldname, k)) res = res_old res[0]['attname'] = k - + if not res: if not isinstance(f, fields.function) or f.store: @@ -2017,7 +2052,7 @@ class orm(orm_template): field_size = (65535 * f.digits[0]) + f.digits[0] + f.digits[1] if field_size != f_pg_size: field_size_change = True - + if f_pg_type != f_obj_type or field_size_change: if f_pg_type != f_obj_type: logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed type to %s." % (k, self._table, c[1])) @@ -2322,7 +2357,7 @@ class orm(orm_template): select = map(lambda x: isinstance(x,dict) and x['id'] or x, select) result = self._read_flat(cr, user, select, fields, context, load) - + for r in result: for key, v in r.items(): if v == None: @@ -2340,8 +2375,8 @@ class orm(orm_template): id_exist = cr.fetchone() if not id_exist: cr.execute('update "'+self._table+'" set "'+key+'"=NULL where "%s"=%s' %(key,''.join("'"+str(v)+"'"))) - r[key] = '' - + r[key] = '' + if isinstance(ids, (int, long, dict)): return result and result[0] or False return result From 718aa9c775a4f78e2d7a11dd0a61832cf37af379 Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Tue, 29 Dec 2009 16:50:14 +0530 Subject: [PATCH 02/11] [ADD]:added new functionality read_group' bzr revid: nch@tinyerp.com-20091229112014-wakcjr8xp0l8333v --- bin/osv/orm.py | 81 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 17 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 9a05def5377..b28b24406fc 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -348,7 +348,7 @@ class orm_template(object): CONCURRENCY_CHECK_FIELD = '__last_update' - def read_group(self, cr, user, domain, fields, order, offset=0, limit=None, context=None): + def read_group(self, cr, user, ids, domain, fields, order, offset=0, limit=None, context=None): context = context or {} # compute the where, order by, limit and offset clauses (qu1, qu2, tables) = self._where_calc(cr, user, domain, context=context) @@ -359,8 +359,10 @@ class orm_template(object): if order: self._check_qorder(order) order_by = order or self._order - # sumof = ','.join(['sum('+field_name+')' for field_name,values in fields.items() if values['type'] in ('float','integer')]) # filter fields in args to only get float and int - sumof = ','.join([field_name for field_name,values in fields.items() if values['type'] in ('float','integer')]) # filter fields in args to only get float and int + + float_int_fields = list(field_name for field_name,values in fields.items() if values['type'] in ('float','integer')) + sumof = ','.join(map(lambda field_name:'sum('+field_name+') as '+field_name+'',float_int_fields)) # filter fields in args to only get float and int + # sumof = ','.join([field_name for field_name,values in fields.items() if values['type'] in ('float','integer')]) # filter fields in args to only get float and int limit_str = limit and ' limit %d' % limit or '' offset_str = offset and ' offset %d' % offset or '' @@ -370,18 +372,64 @@ class orm_template(object): if d1: qu1 = qu1 and qu1+' and '+d1 or ' where '+d1 qu2 += d2 - + if not ids: # execute the "main" query to fetch the ids we were searching for - cr.execute('select '+order_by+','+sumof+' from %s' % self._table + qu1+' group by '+order_by+','+sumof+limit_str+offset_str, qu2) - result = [] - for x in cr.fetchall(): - result.append({ - 'name':x[0], - 'domain':[(order_by,'=',x[0])], - 'fields': {x[0]:x[1]} - }) - print result - # return result + #cr.execute('select distinct('+order_by+'),'+self._table+'.id,'+sumof+' from ' + ','.join(tables) +qu1+' group by '+order_by +','+self._table+'.id'+limit_str+offset_str) + cr.execute('select distinct('+order_by+'),'+sumof+' from ' + ','.join(tables) +qu1+' group by '+order_by +limit_str+offset_str) + res = cr.fetchall() + uniq_ids = () + if fields[order_by]['type'] == 'many2one': + uniq_ids =tuple(set(map(lambda x:str(x[0]),res))) + if uniq_ids and len(uniq_ids) == 1: + cond = ' where id = %s'%(uniq_ids) + else: + cond = ' where id in %s'%(uniq_ids,) + qury = 'select name,id from ' + fields[order_by]['relation'].replace('.','_') + cond + cr.execute(qury) + uniq_order_by_res = dict(map(lambda x:(x[1],x[0]),cr.fetchall())) + result = [] + b = {} + for p_ids in uniq_ids: + cr.execute('select id from '+','.join(tables)+' where +'+order_by+' = %s'%(p_ids,)) + res2 = cr.fetchall() + for val in res2: + if int(p_ids) not in b: + b[int(p_ids)] = [val[0]] + else: + b[int(p_ids)].append(val[0]) + old_id = [] + for x in res: + if x[0] in old_id: + continue + temp = {'id':x[0],order_by:(x[0],uniq_order_by_res[x[0]]),'group_child':b[x[0]]} + for sum in float_int_fields: + temp[sum] = x[float_int_fields.index(sum)+1] + for field in fields.keys(): + if field not in temp: + temp[field] = False + old_id.append(x[0]) + result.append(temp) + return result + else: + query_fields = ','.join(fields.keys()) + result = [] + ids = ','.join(map(lambda x:str(x),ids)) + cr.execute('select id,' + query_fields+' from '+','.join(tables)+' where id in (%s)'%(ids)) + pos_orderby = fields.keys().index(order_by) + res1 = cr.fetchall() + if res1 and len(res1): + part = str(res1[0][pos_orderby+1]) + cr.execute('select id,name from %s where id = %s'%(fields[order_by]['relation'].replace('.','_'),part)) + partner = cr.fetchall()[0] + for val in res1: + temp = {'id':val[0],'group_child':[]} + for field in fields.keys(): + if field == order_by: + temp[field] = partner + continue + temp[field] = val[fields.keys().index(field)+1] + result.append(temp) + return result def _field_create(self, cr, context={}): cr.execute("SELECT id FROM ir_model WHERE model=%s", (self._name,)) @@ -2357,7 +2405,6 @@ class orm(orm_template): select = map(lambda x: isinstance(x,dict) and x['id'] or x, select) result = self._read_flat(cr, user, select, fields, context, load) - for r in result: for key, v in r.items(): if v == None: @@ -2386,6 +2433,8 @@ class orm(orm_template): context = {} if not ids: return [] + if len(ids) and type(ids[0])==tuple: + ids = list(set(map(lambda x:x[0],ids))) ids = map(lambda x:int(x), ids) if fields_to_read == None: fields_to_read = self._columns.keys() @@ -3201,8 +3250,6 @@ class orm(orm_template): ','.join(tables) +qu1 + limit_str + offset_str, qu2) res = cr.fetchall() return res[0][0] - - # execute the "main" query to fetch the ids we were searching for cr.execute('select %s.id from ' % self._table + ','.join(tables) +qu1+' order by '+order_by+limit_str+offset_str, qu2) res = cr.fetchall() return [x[0] for x in res] From d264461befd558669783e9a1df96ef068a84bb4e Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Tue, 29 Dec 2009 18:24:07 +0530 Subject: [PATCH 03/11] [REF,IMP]:improved the read_group code bzr revid: nch@tinyerp.com-20091229125407-jt5fbvvhbcf0xhxv --- bin/osv/orm.py | 93 +++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 47 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index b28b24406fc..e56487d42d2 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -358,11 +358,12 @@ class orm_template(object): qu1 = '' if order: self._check_qorder(order) + order_by = order or self._order float_int_fields = list(field_name for field_name,values in fields.items() if values['type'] in ('float','integer')) + sumof = ','.join(map(lambda field_name:'sum('+field_name+') as '+field_name+'',float_int_fields)) # filter fields in args to only get float and int - # sumof = ','.join([field_name for field_name,values in fields.items() if values['type'] in ('float','integer')]) # filter fields in args to only get float and int limit_str = limit and ' limit %d' % limit or '' offset_str = offset and ' offset %d' % offset or '' @@ -372,63 +373,61 @@ class orm_template(object): if d1: qu1 = qu1 and qu1+' and '+d1 or ' where '+d1 qu2 += d2 + + def get_fulname(ids,table): + + if ids and not type(ids) in (list,tuple) or len(ids) == 1: + cond = ' where id = %s'%(ids) + else: + cond = ' where id in %s'%(ids,) + qury = 'select id,name from ' + table + cond + cr.execute(qury) + return cr.fetchall() + if not ids: - # execute the "main" query to fetch the ids we were searching for - #cr.execute('select distinct('+order_by+'),'+self._table+'.id,'+sumof+' from ' + ','.join(tables) +qu1+' group by '+order_by +','+self._table+'.id'+limit_str+offset_str) - cr.execute('select distinct('+order_by+'),'+sumof+' from ' + ','.join(tables) +qu1+' group by '+order_by +limit_str+offset_str) - res = cr.fetchall() - uniq_ids = () - if fields[order_by]['type'] == 'many2one': - uniq_ids =tuple(set(map(lambda x:str(x[0]),res))) - if uniq_ids and len(uniq_ids) == 1: - cond = ' where id = %s'%(uniq_ids) - else: - cond = ' where id in %s'%(uniq_ids,) - qury = 'select name,id from ' + fields[order_by]['relation'].replace('.','_') + cond - cr.execute(qury) - uniq_order_by_res = dict(map(lambda x:(x[1],x[0]),cr.fetchall())) result = [] - b = {} - for p_ids in uniq_ids: - cr.execute('select id from '+','.join(tables)+' where +'+order_by+' = %s'%(p_ids,)) - res2 = cr.fetchall() - for val in res2: - if int(p_ids) not in b: - b[int(p_ids)] = [val[0]] + cr.execute('select distinct('+order_by+'),'+sumof+' from ' + ','.join(tables) +qu1+' group by '+order_by +limit_str+offset_str) + parent_res = cr.fetchall() + uniq_ids = tuple(map(lambda x:str(x[0]),parent_res)) + if len(uniq_ids): + uniq_order_by_res = get_fulname(uniq_ids,fields[order_by]['relation'].replace('.','_')) + uniq_order_by_res = dict(uniq_order_by_res) + child_ids_dict = {} + # get child ids + for parent_id in uniq_ids: + cr.execute('select id from '+','.join(tables)+' where +'+order_by+' = %s'%(parent_id,)) + child_ids = cr.fetchall() + for val in child_ids: + if int(parent_id) not in child_ids_dict: + child_ids_dict[int(parent_id)] = [val[0]] else: - b[int(p_ids)].append(val[0]) - old_id = [] - for x in res: - if x[0] in old_id: - continue - temp = {'id':x[0],order_by:(x[0],uniq_order_by_res[x[0]]),'group_child':b[x[0]]} + child_ids_dict[int(parent_id)].append(val[0]) + #create [{},{}] for parent ids i.e for group by field + for x in parent_res: + parent_val_dict = {'id':x[0],order_by:(x[0],uniq_order_by_res[x[0]]),'group_child':child_ids_dict[x[0]]} for sum in float_int_fields: - temp[sum] = x[float_int_fields.index(sum)+1] + parent_val_dict[sum] = x[float_int_fields.index(sum)+1] for field in fields.keys(): - if field not in temp: - temp[field] = False - old_id.append(x[0]) - result.append(temp) + if field not in parent_val_dict: + parent_val_dict[field] = False + result.append(parent_val_dict) return result else: - query_fields = ','.join(fields.keys()) + # process for getting child values for all fields in the view result = [] ids = ','.join(map(lambda x:str(x),ids)) - cr.execute('select id,' + query_fields+' from '+','.join(tables)+' where id in (%s)'%(ids)) - pos_orderby = fields.keys().index(order_by) - res1 = cr.fetchall() - if res1 and len(res1): - part = str(res1[0][pos_orderby+1]) - cr.execute('select id,name from %s where id = %s'%(fields[order_by]['relation'].replace('.','_'),part)) - partner = cr.fetchall()[0] - for val in res1: - temp = {'id':val[0],'group_child':[]} + cr.execute('select id,' + ','.join(fields.keys())+' from '+','.join(tables)+' where id in (%s)'%(ids)) + child_res = cr.fetchall() + for val in child_res: + child_dict = {'id':val[0],'group_child':[]} for field in fields.keys(): - if field == order_by: - temp[field] = partner + if fields[field]['type'] == 'many2one': + pos = fields.keys().index(field) + part = str(val[pos+1]) + child_dict[field] = get_fulname(part,fields[field]['relation'].replace('.','_'))[0] continue - temp[field] = val[fields.keys().index(field)+1] - result.append(temp) + child_dict[field] = val[fields.keys().index(field)+1] + result.append(child_dict) return result def _field_create(self, cr, context={}): From d65c7d8776505cccd921ca6239a62be47832a269 Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Tue, 5 Jan 2010 14:15:30 +0530 Subject: [PATCH 04/11] [IMP]:read_group bzr revid: nch@tinyerp.com-20100105084530-zjc1nx1iyije91sa --- bin/osv/orm.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index e56487d42d2..afe516104d6 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -411,6 +411,7 @@ class orm_template(object): if field not in parent_val_dict: parent_val_dict[field] = False result.append(parent_val_dict) + print "returning.........",result return result else: # process for getting child values for all fields in the view @@ -2432,8 +2433,6 @@ class orm(orm_template): context = {} if not ids: return [] - if len(ids) and type(ids[0])==tuple: - ids = list(set(map(lambda x:x[0],ids))) ids = map(lambda x:int(x), ids) if fields_to_read == None: fields_to_read = self._columns.keys() From 83bcb89d40d1e1671ad465d208d37cb94ef25671 Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Wed, 6 Jan 2010 18:57:48 +0530 Subject: [PATCH 05/11] [IMP]:improved the read_group method bzr revid: nch@tinyerp.com-20100106132748-o75tot3byxghk6t2 --- bin/osv/orm.py | 108 +++++++++++++++++-------------------------------- 1 file changed, 36 insertions(+), 72 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index afe516104d6..aa6803bf622 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -348,88 +348,52 @@ class orm_template(object): CONCURRENCY_CHECK_FIELD = '__last_update' - def read_group(self, cr, user, ids, domain, fields, order, offset=0, limit=None, context=None): + def read_group(self, cr, user, ids, fields, groupby, context=None): context = context or {} - # compute the where, order by, limit and offset clauses - (qu1, qu2, tables) = self._where_calc(cr, user, domain, context=context) - if len(qu1): - qu1 = ' where '+string.join(qu1, ' and ') - else: - qu1 = '' - if order: - self._check_qorder(order) - - order_by = order or self._order - - float_int_fields = list(field_name for field_name,values in fields.items() if values['type'] in ('float','integer')) - - sumof = ','.join(map(lambda field_name:'sum('+field_name+') as '+field_name+'',float_int_fields)) # filter fields in args to only get float and int - - limit_str = limit and ' limit %d' % limit or '' - offset_str = offset and ' offset %d' % offset or '' + result = [] + if not ids:return + qu1 = ' where id in (' + ','.join([str(id) for id in ids]) + ')' + qu2 = '' # construct a clause for the rules : d1, d2 = self.pool.get('ir.rule').domain_get(cr, user, self._name) if d1: qu1 = qu1 and qu1+' and '+d1 or ' where '+d1 qu2 += d2 - def get_fulname(ids,table): + group_by_table = fields[groupby]['relation'].replace('.','_') - if ids and not type(ids) in (list,tuple) or len(ids) == 1: - cond = ' where id = %s'%(ids) - else: - cond = ' where id in %s'%(ids,) - qury = 'select id,name from ' + table + cond - cr.execute(qury) - return cr.fetchall() + float_int_fields = list(field_name for field_name,values in fields.items() if values['type'] in ('float','integer')) + sumof = ','.join(map(lambda field_name:'sum('+field_name+') as '+field_name+'',float_int_fields)) - if not ids: - result = [] - cr.execute('select distinct('+order_by+'),'+sumof+' from ' + ','.join(tables) +qu1+' group by '+order_by +limit_str+offset_str) - parent_res = cr.fetchall() - uniq_ids = tuple(map(lambda x:str(x[0]),parent_res)) - if len(uniq_ids): - uniq_order_by_res = get_fulname(uniq_ids,fields[order_by]['relation'].replace('.','_')) - uniq_order_by_res = dict(uniq_order_by_res) - child_ids_dict = {} - # get child ids - for parent_id in uniq_ids: - cr.execute('select id from '+','.join(tables)+' where +'+order_by+' = %s'%(parent_id,)) - child_ids = cr.fetchall() - for val in child_ids: - if int(parent_id) not in child_ids_dict: - child_ids_dict[int(parent_id)] = [val[0]] - else: - child_ids_dict[int(parent_id)].append(val[0]) - #create [{},{}] for parent ids i.e for group by field - for x in parent_res: - parent_val_dict = {'id':x[0],order_by:(x[0],uniq_order_by_res[x[0]]),'group_child':child_ids_dict[x[0]]} - for sum in float_int_fields: - parent_val_dict[sum] = x[float_int_fields.index(sum)+1] - for field in fields.keys(): - if field not in parent_val_dict: - parent_val_dict[field] = False - result.append(parent_val_dict) - print "returning.........",result - return result - else: - # process for getting child values for all fields in the view - result = [] - ids = ','.join(map(lambda x:str(x),ids)) - cr.execute('select id,' + ','.join(fields.keys())+' from '+','.join(tables)+' where id in (%s)'%(ids)) - child_res = cr.fetchall() - for val in child_res: - child_dict = {'id':val[0],'group_child':[]} - for field in fields.keys(): - if fields[field]['type'] == 'many2one': - pos = fields.keys().index(field) - part = str(val[pos+1]) - child_dict[field] = get_fulname(part,fields[field]['relation'].replace('.','_'))[0] - continue - child_dict[field] = val[fields.keys().index(field)+1] - result.append(child_dict) - return result + cr.execute('select '+groupby+','+sumof+' from ' + self._table +qu1+' group by '+groupby+qu2) + parent_res = cr.fetchall() + groupby_ids = map(lambda x:x[0],parent_res) + groupby_name = dict(self.pool.get(fields[groupby]['relation']).name_get(cr,user,groupby_ids,context)) + + child_ids_dict = {} + # get child ids + for parent_id in groupby_ids: + chqu1 =' where id in (' + ','.join([str(id) for id in ids]) + ') and ' + cr.execute('select id from '+self._table+chqu1 +groupby+' = %s'%(parent_id,)) + child_ids = cr.fetchall() + print child_ids + for val in child_ids: + if parent_id not in child_ids_dict: + child_ids_dict[parent_id] = [val[0]] + else: + child_ids_dict[parent_id].append(val[0]) + #create [{},{}] for parent ids i.e for group by field + for x in parent_res: + parent_val_dict = {'id':x[0],groupby:(x[0],groupby_name[x[0]]),'group_child':child_ids_dict[x[0]]} + for sum in float_int_fields: + parent_val_dict[sum] = x[float_int_fields.index(sum)+1] + for field in fields.keys(): + if field not in parent_val_dict: + parent_val_dict[field] = False + result.append(parent_val_dict) + print "returning.........",result + return result def _field_create(self, cr, context={}): cr.execute("SELECT id FROM ir_model WHERE model=%s", (self._name,)) From d904c93dd70aa3d67b69cd404932845e8d952e20 Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Fri, 8 Jan 2010 17:56:40 +0530 Subject: [PATCH 06/11] [FIX]:passed group header id as None bzr revid: nch@tinyerp.com-20100108122640-kiyc8d2a3wyvw9ox --- bin/osv/orm.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index aa6803bf622..fe39fca616b 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -377,7 +377,6 @@ class orm_template(object): chqu1 =' where id in (' + ','.join([str(id) for id in ids]) + ') and ' cr.execute('select id from '+self._table+chqu1 +groupby+' = %s'%(parent_id,)) child_ids = cr.fetchall() - print child_ids for val in child_ids: if parent_id not in child_ids_dict: child_ids_dict[parent_id] = [val[0]] @@ -385,7 +384,7 @@ class orm_template(object): child_ids_dict[parent_id].append(val[0]) #create [{},{}] for parent ids i.e for group by field for x in parent_res: - parent_val_dict = {'id':x[0],groupby:(x[0],groupby_name[x[0]]),'group_child':child_ids_dict[x[0]]} + parent_val_dict = {'id':None,groupby:(x[0],groupby_name[x[0]]),'group_child':child_ids_dict[x[0]]} for sum in float_int_fields: parent_val_dict[sum] = x[float_int_fields.index(sum)+1] for field in fields.keys(): From ca8aca1fa38ddcba62e8c7345f2ad302f9873e83 Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Sat, 9 Jan 2010 12:14:44 +0530 Subject: [PATCH 07/11] [IMP]:groupby on date field bzr revid: nch@tinyerp.com-20100109064444-uqpmnowdk282pn0a --- bin/osv/orm.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index fe39fca616b..159d083604e 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -361,21 +361,23 @@ class orm_template(object): qu1 = qu1 and qu1+' and '+d1 or ' where '+d1 qu2 += d2 - group_by_table = fields[groupby]['relation'].replace('.','_') - float_int_fields = list(field_name for field_name,values in fields.items() if values['type'] in ('float','integer')) sumof = ','.join(map(lambda field_name:'sum('+field_name+') as '+field_name+'',float_int_fields)) - cr.execute('select '+groupby+','+sumof+' from ' + self._table +qu1+' group by '+groupby+qu2) parent_res = cr.fetchall() groupby_ids = map(lambda x:x[0],parent_res) - groupby_name = dict(self.pool.get(fields[groupby]['relation']).name_get(cr,user,groupby_ids,context)) + groupby_name = {} + if fields[groupby]['type'] == 'many2one': + groupby_name = dict(self.pool.get(fields[groupby]['relation']).name_get(cr,user,groupby_ids,context)) + chqu1 =' where id in (' + ','.join([str(id) for id in ids]) + ') and ' + else: + chqu1 =' where ' child_ids_dict = {} # get child ids + for parent_id in groupby_ids: - chqu1 =' where id in (' + ','.join([str(id) for id in ids]) + ') and ' - cr.execute('select id from '+self._table+chqu1 +groupby+' = %s'%(parent_id,)) + cr.execute('select id from '+self._table+chqu1 +groupby+" = '%s'"%(parent_id,)) child_ids = cr.fetchall() for val in child_ids: if parent_id not in child_ids_dict: @@ -383,15 +385,30 @@ class orm_template(object): else: child_ids_dict[parent_id].append(val[0]) #create [{},{}] for parent ids i.e for group by field + import datetime + curr_date = datetime.date.today() + yesterday = (curr_date - datetime.timedelta(days=1)).strftime('%Y-%m-%d') + lastweek = (curr_date + datetime.timedelta(weeks=-1)).strftime('%Y-%m-%d') + date_format = 'Old' for x in parent_res: - parent_val_dict = {'id':None,groupby:(x[0],groupby_name[x[0]]),'group_child':child_ids_dict[x[0]]} + if fields[groupby]['type'] in ('date','datetime'): + if x[0] == curr_date.strftime('%Y-%m-%d'): + date_format = 'Today' + elif x[0] == yesterday: + date_format = 'Yesterday' + elif (x[0] < yesterday) and (x[0] >= lastweek): + date_format = 'LastWeek' + val = date_format + else: + val = (x[0],groupby_name[x[0]]) + + parent_val_dict = {'id':None,groupby:val,'group_child':child_ids_dict[x[0]]} for sum in float_int_fields: parent_val_dict[sum] = x[float_int_fields.index(sum)+1] for field in fields.keys(): if field not in parent_val_dict: parent_val_dict[field] = False result.append(parent_val_dict) - print "returning.........",result return result def _field_create(self, cr, context={}): From acac978830f9b953197438632d4df9b84d7b7cc1 Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Mon, 11 Jan 2010 16:00:11 +0530 Subject: [PATCH 08/11] [IMP]:read group for date fields bzr revid: nch@tinyerp.com-20100111103011-t6r3urfx50zghfro --- bin/osv/orm.py | 80 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 159d083604e..7da3e17a573 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -349,10 +349,11 @@ class orm_template(object): CONCURRENCY_CHECK_FIELD = '__last_update' def read_group(self, cr, user, ids, fields, groupby, context=None): + print ":::::::::::::",fields[groupby],groupby context = context or {} - result = [] if not ids:return - + if fields[groupby]['type'] not in ('many2one,date,datetime'): + raise Exception(_("Type Not supported for Group By: %s :Only many2one,date and datetime are supported ") %(fields[groupby]['type'],)) qu1 = ' where id in (' + ','.join([str(id) for id in ids]) + ')' qu2 = '' # construct a clause for the rules : @@ -367,15 +368,13 @@ class orm_template(object): parent_res = cr.fetchall() groupby_ids = map(lambda x:x[0],parent_res) groupby_name = {} + child_ids_dict = {} + if fields[groupby]['type'] == 'many2one': groupby_name = dict(self.pool.get(fields[groupby]['relation']).name_get(cr,user,groupby_ids,context)) - chqu1 =' where id in (' + ','.join([str(id) for id in ids]) + ') and ' - else: - chqu1 =' where ' + chqu1 =' where id in (' + ','.join([str(id) for id in ids]) + ') and ' - child_ids_dict = {} # get child ids - for parent_id in groupby_ids: cr.execute('select id from '+self._table+chqu1 +groupby+" = '%s'"%(parent_id,)) child_ids = cr.fetchall() @@ -385,31 +384,54 @@ class orm_template(object): else: child_ids_dict[parent_id].append(val[0]) #create [{},{}] for parent ids i.e for group by field - import datetime - curr_date = datetime.date.today() - yesterday = (curr_date - datetime.timedelta(days=1)).strftime('%Y-%m-%d') - lastweek = (curr_date + datetime.timedelta(weeks=-1)).strftime('%Y-%m-%d') - date_format = 'Old' - for x in parent_res: - if fields[groupby]['type'] in ('date','datetime'): - if x[0] == curr_date.strftime('%Y-%m-%d'): + result = [] + if fields[groupby]['type'] in ('date','datetime'): + curr_date = datetime.date.today() + yesterday = (curr_date - datetime.timedelta(days=1)).strftime('%Y-%m-%d') + lastweek = (curr_date + datetime.timedelta(weeks=-1)).strftime('%Y-%m-%d') + date_result = {'Today':{'group_child':[]},'Yesterday':{'group_child':[]}, + 'LastWeek':{'group_child':[]},'Old':{'group_child':[]}} + for x in parent_res: + db_date = x[0][:10] + if db_date == curr_date.strftime('%Y-%m-%d'): date_format = 'Today' - elif x[0] == yesterday: + elif db_date == yesterday: date_format = 'Yesterday' - elif (x[0] < yesterday) and (x[0] >= lastweek): + elif (db_date < yesterday) and (db_date >= lastweek): date_format = 'LastWeek' - val = date_format - else: - val = (x[0],groupby_name[x[0]]) - - parent_val_dict = {'id':None,groupby:val,'group_child':child_ids_dict[x[0]]} - for sum in float_int_fields: - parent_val_dict[sum] = x[float_int_fields.index(sum)+1] - for field in fields.keys(): - if field not in parent_val_dict: - parent_val_dict[field] = False - result.append(parent_val_dict) - return result + else: + date_format = 'Old' + date_result[date_format].update({'id':None,groupby:date_format}) + date_result[date_format]['group_child'] += child_ids_dict[x[0]] + for sum in float_int_fields: + if sum not in date_result[date_format]: + date_result[date_format][sum] = x[float_int_fields.index(sum)+1] + if float_int_fields.index(sum): + date_result[date_format][sum] += x[float_int_fields.index(sum)+1] + for key,val in date_result.items(): + if len(date_result[key]) == 1: + del date_result[key] + continue + for field in fields.keys(): + if field not in val: + val[field] = False + if key in ('Today','Yesterday') and len(result): + result.insert(0,val) + else: + result.insert(-1,val) + print result + return result + else: + for x in parent_res: + parent_val_dict = {'id':None,groupby:(x[0],groupby_name[x[0]]),'group_child':child_ids_dict[x[0]]} + for sum in float_int_fields: + parent_val_dict[sum] = x[float_int_fields.index(sum)+1] + for field in fields.keys(): + if field not in parent_val_dict: + parent_val_dict[field] = False + result.append(parent_val_dict) + print result + return result def _field_create(self, cr, context={}): cr.execute("SELECT id FROM ir_model WHERE model=%s", (self._name,)) From b221e232599d28d6a0c9d9ceb8ee4e7d2539e696 Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Mon, 18 Jan 2010 18:10:04 +0530 Subject: [PATCH 09/11] [IMP]:read_group bzr revid: nch@tinyerp.com-20100118124004-m35ihvp4padm5ri3 --- bin/osv/orm.py | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 7da3e17a573..9f25796ed1d 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -349,7 +349,6 @@ class orm_template(object): CONCURRENCY_CHECK_FIELD = '__last_update' def read_group(self, cr, user, ids, fields, groupby, context=None): - print ":::::::::::::",fields[groupby],groupby context = context or {} if not ids:return if fields[groupby]['type'] not in ('many2one,date,datetime'): @@ -363,10 +362,16 @@ class orm_template(object): qu2 += d2 float_int_fields = list(field_name for field_name,values in fields.items() if values['type'] in ('float','integer')) - sumof = ','.join(map(lambda field_name:'sum('+field_name+') as '+field_name+'',float_int_fields)) - cr.execute('select '+groupby+','+sumof+' from ' + self._table +qu1+' group by '+groupby+qu2) + sum = {} + + cr.execute('select '+groupby+' from ' + self._table +qu1+' group by '+groupby+qu2) parent_res = cr.fetchall() + try: + parent_res = [(m[0],m[1]) for m in parent_res if m[0]] + except: + parent_res = [(m[0],) for m in parent_res if m[0]] groupby_ids = map(lambda x:x[0],parent_res) + groupby_name = {} child_ids_dict = {} @@ -384,6 +389,7 @@ class orm_template(object): else: child_ids_dict[parent_id].append(val[0]) #create [{},{}] for parent ids i.e for group by field + result = [] if fields[groupby]['type'] in ('date','datetime'): curr_date = datetime.date.today() @@ -403,11 +409,16 @@ class orm_template(object): date_format = 'Old' date_result[date_format].update({'id':None,groupby:date_format}) date_result[date_format]['group_child'] += child_ids_dict[x[0]] - for sum in float_int_fields: - if sum not in date_result[date_format]: - date_result[date_format][sum] = x[float_int_fields.index(sum)+1] - if float_int_fields.index(sum): - date_result[date_format][sum] += x[float_int_fields.index(sum)+1] + float_int_sum = self.read(cr, user, child_ids_dict[x[0]], float_int_fields, context) + for value in float_int_sum: + for field,val in value.items(): + if field == 'id':continue + if field not in sum: + sum[field] = 0.0 + sum[field] += val + for k,v in sum.items(): + date_result[date_format][k] = v + sum = {} for key,val in date_result.items(): if len(date_result[key]) == 1: del date_result[key] @@ -419,18 +430,24 @@ class orm_template(object): result.insert(0,val) else: result.insert(-1,val) - print result return result else: for x in parent_res: - parent_val_dict = {'id':None,groupby:(x[0],groupby_name[x[0]]),'group_child':child_ids_dict[x[0]]} - for sum in float_int_fields: - parent_val_dict[sum] = x[float_int_fields.index(sum)+1] + parent_val_dict = {'id':str(x[0])+':Gpby',groupby:(x[0],groupby_name[x[0]]),'group_child':child_ids_dict[x[0]]} + float_int_sum = self.read(cr, user, child_ids_dict[x[0]], float_int_fields, context) + for value in float_int_sum: + for field,val in value.items(): + if field == 'id':continue + if field not in sum: + sum[field] = 0.0 + sum[field] += val + for k,v in sum.items(): + parent_val_dict[k] = v + sum = {} for field in fields.keys(): if field not in parent_val_dict: parent_val_dict[field] = False result.append(parent_val_dict) - print result return result def _field_create(self, cr, context={}): From fbe0cd494f95a20ca30a2274f15bf64825e1116f Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Tue, 19 Jan 2010 11:23:05 +0530 Subject: [PATCH 10/11] [IMP]:readgroup bzr revid: nch@tinyerp.com-20100119055305-h36ot0qc2lmeh1cg --- bin/osv/orm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 9f25796ed1d..30b03608bb6 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -351,7 +351,7 @@ class orm_template(object): def read_group(self, cr, user, ids, fields, groupby, context=None): context = context or {} if not ids:return - if fields[groupby]['type'] not in ('many2one,date,datetime'): + if fields[groupby]['type'] not in ('many2one','date','datetime'): raise Exception(_("Type Not supported for Group By: %s :Only many2one,date and datetime are supported ") %(fields[groupby]['type'],)) qu1 = ' where id in (' + ','.join([str(id) for id in ids]) + ')' qu2 = '' From 3a3a5731c0665527fbbb03d9a32db559913a3154 Mon Sep 17 00:00:00 2001 From: "nch@tinyerp.com" <> Date: Tue, 19 Jan 2010 13:59:07 +0530 Subject: [PATCH 11/11] [IMP]:read_group bzr revid: nch@tinyerp.com-20100119082907-2hdt70viea5eu1ek --- bin/osv/orm.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index a3892e281a4..134929fcefa 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -351,12 +351,15 @@ class orm_template(object): def read_group(self, cr, user, ids, fields, groupby, context=None): context = context or {} if not ids:return + if fields[groupby]['type'] not in ('many2one','date','datetime'): raise Exception(_("Type Not supported for Group By: %s :Only many2one,date and datetime are supported ") %(fields[groupby]['type'],)) + qu1 = ' where id in (' + ','.join([str(id) for id in ids]) + ')' qu2 = '' # construct a clause for the rules : - d1, d2 = self.pool.get('ir.rule').domain_get(cr, user, self._name) + d1, d2, d3 = self.pool.get('ir.rule').domain_get(cr, user, self._name) + if d1: qu1 = qu1 and qu1+' and '+d1 or ' where '+d1 qu2 += d2 @@ -377,6 +380,7 @@ class orm_template(object): if fields[groupby]['type'] == 'many2one': groupby_name = dict(self.pool.get(fields[groupby]['relation']).name_get(cr,user,groupby_ids,context)) + chqu1 =' where id in (' + ','.join([str(id) for id in ids]) + ') and ' # get child ids