From 4ec2704522743ba6ce4addde72fa8d87ca4c82dc Mon Sep 17 00:00:00 2001 From: "hda@tinyerp.com" <> Date: Fri, 3 Oct 2008 16:25:11 +0530 Subject: [PATCH] implemented search on fields.related not fully functional it will work for basic fields bzr revid: hda@tinyerp.com-20081003105511-o181pt3l5w2tj2pe --- bin/osv/fields.py | 60 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/bin/osv/fields.py b/bin/osv/fields.py index 6ddef4a5338..b3baa648b0b 100644 --- a/bin/osv/fields.py +++ b/bin/osv/fields.py @@ -638,15 +638,53 @@ class function(_column): class related(function): def _fnct_search(self, tobj, cr, uid, obj=None, name=None, context=None): - raise 'Not Implemented Yet' -# field_detail=self._field_get(cr,uid,obj,obj._name,name) -# print field_detail -# if field_detail[1] in ('many2one'): -# ids=obj.pool.get(field_detail[0] or obj._name).search(cr,uid,[('name','ilike',context[0][2])]) -# print ids -# return [('id','in',[5,6,7])] -# return True + 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,) + 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 + 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: +# 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] + 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]) + else: + 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]) + 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') + cr.execute(query) + ids=[] + for id in cr.fetchall(): + ids.append(id[0]) + return [('id','in',ids)] # def _fnct_write(self,obj,cr, uid, ids,values, field_name, args, context=None): # raise 'Not Implemented Yet' @@ -681,11 +719,11 @@ class related(function): # 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,[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)) + return(fields[prop].get('relation',False),fields[prop].get('type',False),fields) else: - raise 'Fields %s not exist in %s'%(prop,model_name) + raise 'Field %s not exist in %s'%(prop,model_name) # --------------------------------------------------------- # Serialized fields