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.

85 lines
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Eficent Business and IT Consulting Services S.L.
  3. # © 2016 Serpent Consulting Services Pvt. Ltd.
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. import logging
  6. from openerp import models, api
  7. from openerp.osv import expression
  8. _logger = logging.getLogger(__name__)
  9. def patch_leaf_trgm(method):
  10. def decorate_leaf_to_sql(self, eleaf):
  11. model = eleaf.model
  12. leaf = eleaf.leaf
  13. left, operator, right = leaf
  14. table_alias = '"%s"' % (eleaf.generate_alias())
  15. if operator == '%':
  16. sql_operator = '%%'
  17. params = []
  18. if left in model._columns:
  19. formats = model._columns[left]._symbol_set[0]
  20. column = '%s.%s' % (table_alias, expression._quote(left))
  21. query = '(%s %s %s)' % (column, sql_operator, formats)
  22. elif left in expression.MAGIC_COLUMNS:
  23. query = "(%s.\"%s\" %s %%s)" % (
  24. table_alias, left, sql_operator)
  25. params = right
  26. else: # Must not happen
  27. raise ValueError(
  28. "Invalid field %r in domain term %r" % (left, leaf))
  29. if left in model._columns:
  30. params = model._columns[left]._symbol_set[1](right)
  31. if isinstance(params, basestring):
  32. params = [params]
  33. return query, params
  34. elif operator == 'inselect':
  35. right = (right[0].replace(' % ', ' %% '), right[1])
  36. eleaf.leaf = (left, operator, right)
  37. return method(self, eleaf)
  38. decorate_leaf_to_sql.__decorated__ = True
  39. return decorate_leaf_to_sql
  40. def patch_generate_order_by(method):
  41. @api.model
  42. def decorate_generate_order_by(self, order_spec, query):
  43. if order_spec and order_spec.startswith('similarity('):
  44. return ' ORDER BY ' + order_spec
  45. return method(self, order_spec, query)
  46. decorate_generate_order_by.__decorated__ = True
  47. return decorate_generate_order_by
  48. class IrModel(models.Model):
  49. _inherit = 'ir.model'
  50. def _register_hook(self, cr, ids=None):
  51. # We have to prevent wrapping the function twice to avoid recursion
  52. # errors
  53. if not hasattr(expression.expression._expression__leaf_to_sql,
  54. '__decorated__'):
  55. expression.expression._expression__leaf_to_sql = patch_leaf_trgm(
  56. expression.expression._expression__leaf_to_sql)
  57. if '%' not in expression.TERM_OPERATORS:
  58. expression.TERM_OPERATORS += ('%',)
  59. if not hasattr(models.BaseModel._generate_order_by,
  60. '__decorated__'):
  61. models.BaseModel._generate_order_by = patch_generate_order_by(
  62. models.BaseModel._generate_order_by)
  63. return super(IrModel, self)._register_hook(cr)