3
3
4
4
import logging
5
5
from ast import literal_eval
6
+ from collections import defaultdict
6
7
7
8
from lxml import etree
8
9
@@ -55,12 +56,13 @@ def _get_name_search_domain(self):
55
56
return []
56
57
57
58
58
- def _extend_name_results (self , domain , results , limit , name_get_uid ):
59
+ def _extend_name_results (self , domain , results , limit ):
59
60
result_count = len (results )
60
61
if result_count < limit :
61
62
domain += [("id" , "not in" , results )]
62
63
rec_ids = self ._search (
63
- domain , limit = limit - result_count , access_rights_uid = name_get_uid
64
+ domain ,
65
+ limit = limit - result_count ,
64
66
)
65
67
results .extend (rec_ids )
66
68
return results
@@ -69,40 +71,35 @@ def _extend_name_results(self, domain, results, limit, name_get_uid):
69
71
def patch_name_search ():
70
72
@api .model
71
73
def _name_search (
72
- self , name = "" , args = None , operator = "ilike" , limit = 100 , name_get_uid = None
74
+ self , name = "" , domain = None , operator = "ilike" , limit = 100 , order = None
73
75
):
74
76
# Perform standard name search
75
77
res = _name_search .origin (
76
78
self ,
77
79
name = name ,
78
- args = args ,
79
- operator = operator ,
80
+ domain = domain ,
80
81
limit = limit ,
81
- name_get_uid = name_get_uid ,
82
+ order = order ,
82
83
)
83
84
if name and _get_use_smart_name_search (self .sudo ()) and operator in ALLOWED_OPS :
84
85
# _name_search.origin is a query, we need to convert it to a list
85
86
res = self .browse (res ).ids
86
87
limit = limit or 0
87
88
88
89
# we add domain
89
- args = args or [] + _get_name_search_domain (self .sudo ())
90
+ args = domain or [] + _get_name_search_domain (self .sudo ())
90
91
91
92
# Support a list of fields to search on
92
93
all_names = _get_rec_names (self .sudo ())
93
94
base_domain = args or []
94
95
# Try regular search on each additional search field
95
96
for rec_name in all_names [1 :]:
96
97
domain = [(rec_name , operator , name )]
97
- res = _extend_name_results (
98
- self , base_domain + domain , res , limit , name_get_uid
99
- )
98
+ res = _extend_name_results (self , base_domain + domain , res , limit )
100
99
# Try ordered word search on each of the search fields
101
100
for rec_name in all_names :
102
101
domain = [(rec_name , operator , name .replace (" " , "%" ))]
103
- res = _extend_name_results (
104
- self , base_domain + domain , res , limit , name_get_uid
105
- )
102
+ res = _extend_name_results (self , base_domain + domain , res , limit )
106
103
# Try unordered word search on each of the search fields
107
104
# we only perform this search if we have at least one
108
105
# separator character
@@ -116,9 +113,7 @@ def _name_search(
116
113
word_domain and ["|" ] + word_domain or word_domain
117
114
) + [(rec_name , operator , word )]
118
115
domain = (domain and ["&" ] + domain or domain ) + word_domain
119
- res = _extend_name_results (
120
- self , base_domain + domain , res , limit , name_get_uid
121
- )
116
+ res = _extend_name_results (self , base_domain + domain , res , limit )
122
117
123
118
return res
124
119
@@ -130,8 +125,7 @@ class Base(models.AbstractModel):
130
125
131
126
# TODO perhaps better to create only the field when enabled on the model
132
127
smart_search = fields .Char (
133
- compute = "_compute_smart_search" ,
134
- search = "_search_smart_search" ,
128
+ compute = "_compute_smart_search" , search = "_search_smart_search" , translate = False
135
129
)
136
130
137
131
def _compute_smart_search (self ):
@@ -214,7 +208,7 @@ def _compute_smart_search_warning(self):
214
208
215
209
@api .constrains ("name_search_ids" , "name_search_domain" , "add_smart_search" )
216
210
def update_search_wo_restart (self ):
217
- self .clear_caches ()
211
+ self .env . registry . clear_cache ()
218
212
219
213
@api .constrains ("name_search_domain" )
220
214
def check_name_search_domain (self ):
@@ -252,9 +246,16 @@ def _register_hook(self):
252
246
"""
253
247
_logger .info ("Patching BaseModel for Smart Search" )
254
248
249
+ patched_models = defaultdict (set )
250
+
251
+ def patch (model , name , method ):
252
+ if model not in patched_models [name ]:
253
+ ModelClass = type (model )
254
+ method .origin = getattr (ModelClass , name )
255
+ setattr (ModelClass , name , method )
256
+
255
257
for model in self .sudo ().search (self .ids or []):
256
258
Model = self .env .get (model .model )
257
259
if Model is not None and not Model ._abstract :
258
- Model ._patch_method ("_name_search" , patch_name_search ())
259
-
260
+ patch (Model , "_name_search" , patch_name_search ())
260
261
return super ()._register_hook ()
0 commit comments