|
| 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 | +# Copyright 2021 Tecnativa - Jairo Llopis |
| 5 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 6 | + |
| 7 | +from odoo.osv import expression |
| 8 | +from functools import wraps |
| 9 | + |
| 10 | + |
| 11 | +def patch_leaf_trgm(original): |
| 12 | + @wraps(original) |
| 13 | + def _wrapper(self, eleaf): |
| 14 | + leaf = eleaf.leaf |
| 15 | + left, operator, right = leaf |
| 16 | + model = eleaf.model |
| 17 | + # No overload for other operators... |
| 18 | + if operator != "%": |
| 19 | + # Except translation "inselect" queries |
| 20 | + if operator == 'inselect': |
| 21 | + right = (right[0].replace(' % ', ' %% '), right[1]) |
| 22 | + eleaf.leaf = (left, operator, right) |
| 23 | + return original(self, eleaf) |
| 24 | + # The field must exist |
| 25 | + if left not in model._fields: |
| 26 | + raise ValueError("Invalid field %r in domain term %r" % (left, leaf)) |
| 27 | + # Generate correct WHERE clause part |
| 28 | + table_alias = eleaf.generate_alias() |
| 29 | + query = '("{}"."{}" %% {})'.format( |
| 30 | + table_alias, |
| 31 | + left, |
| 32 | + model._fields[left].column_format, |
| 33 | + ) |
| 34 | + params = [right] |
| 35 | + return query, params |
| 36 | + |
| 37 | + return _wrapper |
| 38 | + |
| 39 | + |
| 40 | +def post_load(): |
| 41 | + """Patch expression generators to enable the fuzzy search operator.""" |
| 42 | + expression.TERM_OPERATORS += ("%",) |
| 43 | + expression.expression._expression__leaf_to_sql = patch_leaf_trgm( |
| 44 | + expression.expression._expression__leaf_to_sql |
| 45 | + ) |
0 commit comments