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.

82 lines
2.7 KiB

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