You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Daniel Reis
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp import models, fields, api
  5. from openerp import SUPERUSER_ID
  6. from openerp import tools
  7. # Extended name search is only used on some operators
  8. ALLOWED_OPS = set(['ilike', 'like'])
  9. @tools.ormcache(skiparg=0)
  10. def _get_rec_names(self):
  11. "List of fields to search into"
  12. model = self.env['ir.model'].search(
  13. [('model', '=', str(self._model))])
  14. rec_name = [self._rec_name] or []
  15. other_names = model.name_search_ids.mapped('name')
  16. return rec_name + other_names
  17. def _extend_name_results(self, domain, results, limit):
  18. result_count = len(results)
  19. if result_count < limit:
  20. domain += [('id', 'not in', [x[0] for x in results])]
  21. recs = self.search(domain, limit=limit - result_count)
  22. results.extend(recs.name_get())
  23. return results
  24. class ModelExtended(models.Model):
  25. _inherit = 'ir.model'
  26. name_search_ids = fields.Many2many(
  27. 'ir.model.fields',
  28. string='Name Search Fields')
  29. def _register_hook(self, cr, ids=None):
  30. def make_name_search():
  31. @api.model
  32. def name_search(self, name='', args=None,
  33. operator='ilike', limit=100):
  34. # Perform standard name search
  35. res = name_search.origin(
  36. self, name=name, args=args, operator=operator, limit=limit)
  37. enabled = self.env.context.get('name_search_extended', True)
  38. # Perform extended name search
  39. # Note: Empty name causes error on
  40. # Customer->More->Portal Access Management
  41. if name and enabled and operator in ALLOWED_OPS:
  42. # Support a list of fields to search on
  43. all_names = _get_rec_names(self)
  44. base_domain = args or []
  45. # Try regular search on each additional search field
  46. for rec_name in all_names[1:]:
  47. domain = [(rec_name, operator, name)]
  48. res = _extend_name_results(
  49. self, base_domain + domain, res, limit)
  50. # Try ordered word search on each of the search fields
  51. for rec_name in all_names:
  52. domain = [(rec_name, operator, name.replace(' ', '%'))]
  53. res = _extend_name_results(
  54. self, base_domain + domain, res, limit)
  55. # Try unordered word search on each of the search fields
  56. for rec_name in all_names:
  57. domain = [(rec_name, operator, x)
  58. for x in name.split() if x]
  59. res = _extend_name_results(
  60. self, base_domain + domain, res, limit)
  61. return res
  62. return name_search
  63. if ids is None:
  64. ids = self.search(cr, SUPERUSER_ID, [])
  65. for model in self.browse(cr, SUPERUSER_ID, ids):
  66. Model = self.pool.get(model.model)
  67. if Model:
  68. Model._patch_method('name_search', make_name_search())
  69. return super(ModelExtended, self)._register_hook(cr)