diff --git a/bin/osv/fields.py b/bin/osv/fields.py index b3baa648b0b..f655ca01d8b 100644 --- a/bin/osv/fields.py +++ b/bin/osv/fields.py @@ -230,6 +230,7 @@ class binary(_column): get = get_memory + class selection(_column): _type = 'selection' @@ -331,7 +332,7 @@ class many2one(_column): context = {} obj = obj_src.pool.get(self._obj) self._table = obj_src.pool.get(self._obj)._table - if type(values)==type([]): + if type(values) == type([]): for act in values: if act[0] == 0: id_new = obj.create(cr, act[2]) @@ -495,7 +496,7 @@ class many2many(_column): d1, d2 = obj.pool.get('ir.rule').domain_get(cr, user, obj._name) if d1: - d1 = ' and '+d1 + d1 = ' and ' + d1 cr.execute('SELECT '+self._rel+'.'+self._id2+','+self._rel+'.'+self._id1+' \ FROM '+self._rel+' , '+obj._table+' \ @@ -531,7 +532,7 @@ class many2many(_column): d1, d2 = obj.pool.get('ir.rule').domain_get(cr, user, obj._name) if d1: - d1 = ' and '+d1 + d1 = ' and ' + d1 cr.execute('delete from '+self._rel+' where '+self._id1+'=%d AND '+self._id2+' IN (SELECT '+self._rel+'.'+self._id2+' FROM '+self._rel+', '+obj._table+' WHERE '+self._rel+'.'+self._id1+'=%d AND '+self._rel+'.'+self._id2+' = '+obj._table+'.id '+ d1 +')', [id, id]+d2) for act_nbr in act[2]: @@ -638,92 +639,93 @@ class function(_column): class related(function): def _fnct_search(self, tobj, cr, uid, obj=None, name=None, context=None): - where_flag=0 - where=" where" - query = "select %s.id from %s"%(obj._table,obj._table) - relation_child=obj._name - relation=obj._name + where_flag = 0 + where = " where" + query = "select %s.id from %s" % (obj._table, obj._table) + relation_child = obj._name + relation = obj._name for i in range(len(self._arg)): - field_detail=self._field_get(cr,uid,obj,relation,self._arg[i]) - relation=field_detail[0] - if field_detail[1] in ('one2many','many2many'): - obj_child=obj.pool.get(field_detail[2][self._arg[i]]['relation']) - field_detail_child=obj_child.fields_get(cr,uid,) + field_detail = self._field_get(cr, uid, obj, relation, self._arg[i]) + relation = field_detail[0] + if field_detail[1] in ('one2many', 'many2many'): + obj_child = obj.pool.get(field_detail[2][self._arg[i]]['relation']) + field_detail_child = obj_child.fields_get(cr, uid,) - fields_filter = dict(filter(lambda x:x[1].get('relation',False) + fields_filter = dict(filter(lambda x: x[1].get('relation', False) and x[1].get('relation') == relation_child - and x[1].get('type')=='many2one',field_detail_child.items())) - query +=" inner join %s on %s.id = %s.%s"%(obj_child._table,obj._table,obj_child._table,fields_filter.keys()[0]) - relation_child=relation + and x[1].get('type')=='many2one', field_detail_child.items())) + query += " inner join %s on %s.id = %s.%s" % (obj_child._table, obj._table, obj_child._table, fields_filter.keys()[0]) + relation_child = relation elif field_detail[1] in ('many2one'): - obj_child=obj.pool.get(field_detail[2][self._arg[i]]['relation']) - obj_child2=obj.pool.get(relation_child) - if obj_child._name==obj_child2._name: + obj_child = obj.pool.get(field_detail[2][self._arg[i]]['relation']) + obj_child2 = obj.pool.get(relation_child) + if obj_child._name == obj_child2._name: # select res_partner.id from res_partner where res_partner.parent_id in(select id from res_partner where res_partner.date >= '2008-10-01'); # where +=" %s.id = %s.%s in (select id from %s where %s.%s %s %s"%(obj_child._table,obj_child2._table,self._arg[i]) pass else: - query +=" inner join %s on %s.id = %s.%s"%(obj_child._table,obj_child._table,obj_child2._table,self._arg[i]) - relation_child=field_detail[0] + query += " inner join %s on %s.id = %s.%s" % (obj_child._table, obj_child._table, obj_child2._table, self._arg[i]) + relation_child = field_detail[0] if i == (len(self._arg)-1): if obj_child._inherits: - obj_child_inherits=obj.pool.get(obj_child._inherits.keys()[0]) - query +=" inner join %s on %s.id = %s.%s"%(obj_child_inherits._table,obj_child_inherits._table,obj_child._table,obj_child._inherits.values()[0]) - obj_child=obj_child_inherits - where +=" %s.%s %s '%%%s%%' and"%(obj_child._table,obj_child._rec_name,context[0][1],context[0][2]) + obj_child_inherits = obj.pool.get(obj_child._inherits.keys()[0]) + query += " inner join %s on %s.id = %s.%s" % (obj_child_inherits._table, obj_child_inherits._table, obj_child._table, obj_child._inherits.values()[0]) + obj_child = obj_child_inherits + where += " %s.%s %s '%%%s%%' and" % (obj_child._table, obj_child._rec_name, context[0][1], context[0][2]) else: - obj_child=obj.pool.get(relation_child) + obj_child = obj.pool.get(relation_child) if field_detail[1] in ('char'): - where +=" %s.%s %s '%%%s%%' and"%(obj_child._table,self._arg[i],context[0][1],context[0][2]) + where += " %s.%s %s '%%%s%%' and" % (obj_child._table, self._arg[i], context[0][1], context[0][2]) if field_detail[1] in ('date'): - where +=" %s.%s %s '%s' and"%(obj_child._table,self._arg[i],context[0][1],context[0][2]) - if field_detail[1] in ['integer','long','float']: - where +=" %s.%s %s '%d' and"%(obj_child._table,self._arg[i],context[0][1],context[0][2]) - query+=where.rstrip('and') + where += " %s.%s %s '%s' and" % (obj_child._table, self._arg[i], context[0][1], context[0][2]) + if field_detail[1] in ['integer', 'long', 'float']: + where += " %s.%s %s '%d' and" % (obj_child._table, self._arg[i], context[0][1], context[0][2]) + query += where.rstrip('and') cr.execute(query) - ids=[] + ids = [] for id in cr.fetchall(): ids.append(id[0]) - return [('id','in',ids)] + return [('id', 'in', ids)] # def _fnct_write(self,obj,cr, uid, ids,values, field_name, args, context=None): # raise 'Not Implemented Yet' - def _fnct_read(self,obj,cr, uid, ids, field_name, args, context=None): + def _fnct_read(self, obj, cr, uid, ids, field_name, args, context=None): if not ids: return {} - relation=obj._name - res={} - objlst = obj.browse(cr,uid,ids) + relation = obj._name + res = {} + objlst = obj.browse(cr, uid, ids) for data in objlst: - t_data=data - relation=obj._name + t_data = data + relation = obj._name for i in range(len(self.arg)): - field_detail=self._field_get(cr,uid,obj,relation,self.arg[i]) - relation=field_detail[0] + field_detail = self._field_get(cr, uid, obj, relation, self.arg[i]) + relation = field_detail[0] if not t_data[self.arg[i]]: t_data = False break - if field_detail[1] in ('one2many','many2many'): - t_data=t_data[self.arg[i]][0] + if field_detail[1] in ('one2many', 'many2many'): + t_data = t_data[self.arg[i]][0] else: - t_data=t_data[self.arg[i]] + t_data = t_data[self.arg[i]] if type(t_data) == type(objlst[0]): - res[data.id]=t_data.id + res[data.id] = t_data.id else: - res[data.id]=t_data + res[data.id] = t_data return res - def __init__(self,*arg,**args): + def __init__(self, *arg, **args): self.arg = arg - super(related, self).__init__(self._fnct_read, arg, fnct_inv_arg=arg,method=True, fnct_search=self._fnct_search,**args) + super(related, self).__init__(self._fnct_read, arg, fnct_inv_arg=arg, method=True, fnct_search=self._fnct_search, **args) # TODO: call field_get on the object, not in the DB def _field_get(self, cr, uid, obj, model_name, prop): - fields=obj.pool.get(model_name).fields_get(cr,uid,) - if fields.get(prop,False): - return(fields[prop].get('relation',False),fields[prop].get('type',False),fields) + fields = obj.pool.get(model_name).fields_get(cr, uid,) + if fields.get(prop, False): + return(fields[prop].get('relation', False), fields[prop].get('type', False), fields) else: - raise 'Field %s not exist in %s'%(prop,model_name) + raise 'Field %s not exist in %s' % (prop, model_name) + # --------------------------------------------------------- # Serialized fields