openerp学习笔记 按客户电话、名称模糊查找选择客户(name_search)及客户名称自定义显示(name_get)
#同时按手机、电话、名称模糊查找选择客户
def
name_search(self, cr, user, name, args=None, operator='ilike', context=None,
limit=100):
if not
args:
args
= []
args =
args[:]
ids =
[]
if
name:
ids
= self.search(cr, user, [('mobile', 'ilike', name)]+args, limit=limit,
context=context)
if not
ids:
ids = self.search(cr, user, [('phone', 'ilike', name)]+ args, limit=limit,
context=context)
if not
ids:
ids = self.search(cr, user, [('name', operator, name)]+ args, limit=limit,
context=context)
else:
ids
= self.search(cr, user, args, limit=limit,
context=context)
return
self.name_get(cr, user, ids, context=context)
#关联客户时将客户名称自定义显示为“名称 手机”
def
name_get(self, cr, uid, ids,
context=None):
if not
ids:
return []
if isinstance(ids, (int,
long)):
ids = [ids]
reads = self.read(cr,
uid, ids, ['name', 'mobile'],
context=context)
res =
[]
for record in
reads:
name =
record['name']
if
record['mobile']:
name = name + ' ' +
record['mobile']
res.append((record['id'], name))
return res
其它代码参考:
#res_partner.name_search 合作伙伴查找选择
def
name_search(self, cr, uid, name, args=None, operator='ilike', context=None,
limit=100):
if not
args:
args
= []
if name and operator in ('=',
'ilike', '=ilike', 'like',
'=like'):
# search on the name of the contacts and of its
company
search_name =
name
if
operator in ('ilike',
'like'):
search_name = '%%%s%%' %
name
if
operator in ('=ilike',
'=like'):
operator =
operator[1:]
query_args = {'name':
search_name}
limit_str =
''
if
limit:
limit_str = ' limit
%(limit)s'
query_args['limit'] =
limit
#
TODO: simplify this in trunk with _rec_name='display_name', once
display_name
# becomes a stored
field
cr.execute('''SELECT partner.id FROM res_partner
partner
LEFT JOIN res_partner company ON partner.parent_id =
company.id
WHERE partner.email ''' + operator +''' %(name)s
OR
CASE WHEN company.id IS NULL OR partner.is_company
THEN
partner.name
ELSE
company.name || ', ' ||
partner.name
END
''' + operator + ' %(name)s ' + limit_str,
query_args)
ids = map(lambda x: x[0],
cr.fetchall())
ids = self.search(cr, uid, [('id', 'in', ids)] + args, limit=limit,
context=context)
if
ids:
return self.name_get(cr, uid, ids,
context)
return
super(res_partner,self).name_search(cr, uid, name, args, operator=operator,
context=context, limit=limit)
#res_partner.name_get 合作伙伴名称显示
def name_get(self, cr, uid, ids,
context=None):
if context is
None:
context = {}
if isinstance(ids,
(int,
long)):
ids = [ids]
res =
[]
for record in self.browse(cr,
uid, ids,
context=context):
name =
record.name
if record.parent_id and not
record.is_company:
name = "%s, %s" % (record.parent_id.name,
name)
if
context.get('show_address'):
name = name + "\n" + self._display_address(cr, uid, record,
without_company=True,
context=context)
name =
name.replace('\n\n','\n')
name =
name.replace('\n\n','\n')
if context.get('show_email') and
record.email:
name = "%s <%s>" % (name,
record.email)
res.append((record.id, name))
return res
#proudct_product.name_search 产品查找选择
def
name_search(self, cr, user, name='', args=None, operator='ilike', context=None,
limit=100):
if not
args:
args
= []
if
name:
ids
= self.search(cr, user, [('default_code','=',name)]+ args, limit=limit,
context=context)
if not
ids:
ids = self.search(cr, user, [('ean13','=',name)]+ args, limit=limit,
context=context)
if not
ids:
# Do not merge the 2 next lines into one single search, SQL search performance
would be
abysmal
# on a database with thousands of matching products, due to the huge
merge+unique needed for
the
# OR operator (and given the fact that the 'name' lookup results come from the
ir.translation
table
# Performing a quick memory merge of ids in Python will give much better
performance
ids =
set()
ids.update(self.search(cr, user, args + [('default_code',operator,name)],
limit=limit,
context=context))
if not limit or len(ids) <
limit:
# we may underrun the limit because of dupes in the results, that's
fine
ids.update(self.search(cr, user, args + [('name',operator,name)], limit=(limit
and (limit-len(ids)) or False) ,
context=context))
ids =
list(ids)
if not
ids:
ptrn =
re.compile('(\[(.*?)\])')
res =
ptrn.search(name)
if
res:
ids = self.search(cr, user, [('default_code','=', res.group(2))] + args,
limit=limit, context=context)
else:
ids
= self.search(cr, user, args, limit=limit,
context=context)
result =
self.name_get(cr, user, ids,
context=context)
return result
#proudct_product.name_get 产品名称显示
def
name_get(self, cr, user, ids,
context=None):
if context is
None:
context = {}
if isinstance(ids,
(int,
long)):
ids = [ids]
if not
len(ids):
return []
def
_name_get(d):
name =
d.get('name','')
code =
d.get('default_code',False)
if
code:
name = '[%s] %s' %
(code,name)
if
d.get('variants'):
name = name + ' - %s' %
(d['variants'],)
return (d['id'], name)
partner_id = context.get('partner_id', False)
result =
[]
for product in self.browse(cr,
user, ids,
context=context):
sellers = filter(lambda x: x.name.id == partner_id,
product.seller_ids)
if
sellers:
for s in
sellers:
mydict =
{
'id':
product.id,
'name': s.product_name or
product.name,
'default_code': s.product_code or
product.default_code,
'variants':
product.variants
}
result.append(_name_get(mydict))
else:
mydict =
{
'id':
product.id,
'name':
product.name,
'default_code':
product.default_code,
'variants':
product.variants
}
result.append(_name_get(mydict))
return result