forked from OCA/server-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.py
45 lines (38 loc) · 1.46 KB
/
hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Copyright 2016 Eficent Business and IT Consulting Services S.L.
# Copyright 2016 Serpent Consulting Services Pvt. Ltd.
# Copyright 2017 LasLabs Inc.
# Copyright 2021 Tecnativa - Jairo Llopis
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from functools import wraps
from odoo.osv import expression
def patch_leaf_trgm(original):
@wraps(original)
def _wrapper(self, leaf, model, alias):
left, operator, right = leaf
# No overload for other operators...
if operator != "%":
# Except translation "inselect" queries
if operator == "inselect":
right = (right[0].replace(" % ", " %% "), right[1])
leaf = (left, operator, right)
return original(self, leaf, model, alias)
# The field must exist
if left not in model._fields:
raise ValueError(
"Invalid field {!r} in domain term {!r}".format(left, leaf)
)
# Generate correct WHERE clause part
query = '("{}"."{}" %% {})'.format(
alias,
left,
model._fields[left].column_format,
)
params = [right]
return query, params
return _wrapper
def post_load():
"""Patch expression generators to enable the fuzzy search operator."""
expression.TERM_OPERATORS += ("%",)
expression.expression._expression__leaf_to_sql = patch_leaf_trgm(
expression.expression._expression__leaf_to_sql
)