|
|
@ -4,6 +4,29 @@ |
|
|
|
|
|
|
|
from openerp import models, fields, api |
|
|
|
from openerp import SUPERUSER_ID |
|
|
|
from openerp import tools |
|
|
|
|
|
|
|
|
|
|
|
# Extended name search is only used on some operators |
|
|
|
ALLOWED_OPS = set(['ilike', 'like']) |
|
|
|
|
|
|
|
|
|
|
|
@tools.ormcache(skiparg=0) |
|
|
|
def _get_rec_names(self): |
|
|
|
model = self.env['ir.model'].search( |
|
|
|
[('model', '=', str(self._model))]) |
|
|
|
rec_name = [self._rec_name] or [] |
|
|
|
other_names = model.name_search_ids.mapped('name') |
|
|
|
return rec_name + other_names |
|
|
|
|
|
|
|
|
|
|
|
def _extend_name_results(self, domain, results, limit): |
|
|
|
result_count = len(results) |
|
|
|
if result_count < limit: |
|
|
|
domain += [('id', 'not in', [x[0] for x in results])] |
|
|
|
recs = self.search(domain, limit=limit - result_count) |
|
|
|
results.extend(recs.name_get()) |
|
|
|
return results |
|
|
|
|
|
|
|
|
|
|
|
class ModelExtended(models.Model): |
|
|
@ -20,35 +43,27 @@ class ModelExtended(models.Model): |
|
|
|
@api.model |
|
|
|
def name_search(self, name='', args=None, |
|
|
|
operator='ilike', limit=100): |
|
|
|
# Regular name search |
|
|
|
# Perform standard name search |
|
|
|
res = name_search.origin( |
|
|
|
self, name=name, args=args, operator=operator, limit=limit) |
|
|
|
|
|
|
|
allowed_ops = ['ilike', 'like', '='] |
|
|
|
if not res and operator in allowed_ops and self._rec_name: |
|
|
|
enabled = self.env.context.get('name_search_extended', True) |
|
|
|
# Perform extended name search |
|
|
|
if enabled and operator in ALLOWED_OPS: |
|
|
|
# Support a list of fields to search on |
|
|
|
model = self.env['ir.model'].search( |
|
|
|
[('model', '=', str(self._model))]) |
|
|
|
other_names = model.name_search_ids.mapped('name') |
|
|
|
all_names = _get_rec_names(self) |
|
|
|
# Try regular search on each additional search field |
|
|
|
for rec_name in other_names: |
|
|
|
for rec_name in all_names[1:]: |
|
|
|
domain = [(rec_name, operator, name)] |
|
|
|
recs = self.search(domain, limit=limit) |
|
|
|
if recs: |
|
|
|
return recs.name_get() |
|
|
|
res = _extend_name_results(self, domain, res, limit) |
|
|
|
# Try ordered word search on each of the search fields |
|
|
|
for rec_name in [self._rec_name] + other_names: |
|
|
|
for rec_name in all_names: |
|
|
|
domain = [(rec_name, operator, name.replace(' ', '%'))] |
|
|
|
recs = self.search(domain, limit=limit) |
|
|
|
if recs: |
|
|
|
return recs.name_get() |
|
|
|
res = _extend_name_results(self, domain, res, limit) |
|
|
|
# Try unordered word search on each of the search fields |
|
|
|
for rec_name in [self._rec_name] + other_names: |
|
|
|
for rec_name in all_names: |
|
|
|
domain = [(rec_name, operator, x) |
|
|
|
for x in name.split() if x] |
|
|
|
recs = self.search(domain, limit=limit) |
|
|
|
if recs: |
|
|
|
return recs.name_get() |
|
|
|
res = _extend_name_results(self, domain, res, limit) |
|
|
|
return res |
|
|
|
return name_search |
|
|
|
|
|
|
|