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.

91 lines
2.9 KiB

  1. # Copyright 2016 Eficent Business and IT Consulting Services S.L.
  2. # Copyright 2016 Serpent Consulting Services Pvt. Ltd.
  3. # Copyright 2017 LasLabs Inc.
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. import logging
  6. from odoo import _, api, models
  7. from odoo.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._fields:
  19. column = '%s.%s' % (table_alias, expression._quote(left))
  20. query = '(%s %s %s)' % (
  21. column,
  22. sql_operator,
  23. model._fields[left].column_format,
  24. )
  25. elif left in models.MAGIC_COLUMNS:
  26. query = "(%s.\"%s\" %s %%s)" % (
  27. table_alias, left, sql_operator)
  28. params = right
  29. else: # Must not happen
  30. raise ValueError(_(
  31. "Invalid field %r in domain term %r" % (left, leaf)
  32. ))
  33. if left in model._fields:
  34. params = str(right)
  35. if isinstance(params, str):
  36. params = [params]
  37. return query, params
  38. elif operator == 'inselect':
  39. right = (right[0].replace(' % ', ' %% '), right[1])
  40. eleaf.leaf = (left, operator, right)
  41. return method(self, eleaf)
  42. decorate_leaf_to_sql.__decorated__ = True
  43. return decorate_leaf_to_sql
  44. def patch_generate_order_by(method):
  45. @api.model
  46. def decorate_generate_order_by(self, order_spec, query):
  47. if order_spec and order_spec.startswith('similarity('):
  48. return ' ORDER BY ' + order_spec
  49. return method(self, order_spec, query)
  50. decorate_generate_order_by.__decorated__ = True
  51. return decorate_generate_order_by
  52. class IrModel(models.Model):
  53. _inherit = 'ir.model'
  54. @api.model_cr
  55. def _register_hook(self):
  56. # We have to prevent wrapping the function twice to avoid recursion
  57. # errors
  58. if not hasattr(expression.expression._expression__leaf_to_sql,
  59. '__decorated__'):
  60. expression.expression._expression__leaf_to_sql = patch_leaf_trgm(
  61. expression.expression._expression__leaf_to_sql)
  62. if '%' not in expression.TERM_OPERATORS:
  63. expression.TERM_OPERATORS += ('%',)
  64. if not hasattr(models.BaseModel._generate_order_by,
  65. '__decorated__'):
  66. models.BaseModel._generate_order_by = patch_generate_order_by(
  67. models.BaseModel._generate_order_by)
  68. return super(IrModel, self)._register_hook()