Skip to content

Commit f1b908b

Browse files
committed
[MIG] base_name_search_improved: Migration to 17.0
1 parent fde239a commit f1b908b

File tree

9 files changed

+157
-106
lines changed

9 files changed

+157
-106
lines changed

base_name_search_improved/README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Improved Name Search
77
!! This file is generated by oca-gen-addon-readme !!
88
!! changes will be overwritten. !!
99
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10-
!! source digest: sha256:88daa951eef68e162381052878cb525cb0c1e6b0a72cd2a1f9d138dca31bd6f4
10+
!! source digest: sha256:fe0fce7aeb356dfbf982cb648994712ec1907ee6ab73a221eee4cda4039e7a33
1111
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1212
1313
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
@@ -40,7 +40,7 @@ relaxed search also looks up for records containing all the words, so
4040
"John M. Brown" would be a match. It also tolerates words in a different
4141
order, so searching for "brown john" also works.
4242

43-
|image1|
43+
|image0|
4444

4545
Additionally, an Administrator can configure other fields to also lookup
4646
into. For example, Customers could be additionally searched by City or
@@ -64,7 +64,7 @@ tried. The specific methods used are:
6464
All results found are presented in that order, hopefully presenting them
6565
in order of relevance.
6666

67-
.. |image1| image:: https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image0.png
67+
.. |image0| image:: https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image0.png
6868
.. |image2| image:: https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image2.png
6969

7070
**Table of contents**

base_name_search_improved/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"name": "Improved Name Search",
55
"summary": "Friendlier search when typing in relation fields",
6-
"version": "16.0.1.0.0",
6+
"version": "17.0.1.0.0",
77
"category": "Uncategorized",
88
"website": "https://github.com/OCA/server-tools",
99
"author": "Daniel Reis, Odoo Community Association (OCA), ADHOC SA",

base_name_search_improved/hooks.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import logging
22

3-
from odoo import SUPERUSER_ID, api
4-
53
_logger = logging.getLogger(__name__)
64

75

8-
def uninstall_hook(cr, registry):
6+
def uninstall_hook(env):
97
_logger.info("Reverting Patches...")
10-
env = api.Environment(cr, SUPERUSER_ID, {})
11-
env["ir.model.fields"].with_context(_force_unlink=True).search(
12-
[("name", "=", "smart_search")]
13-
).unlink()
8+
fields_to_unlink = (
9+
env["ir.model.fields"]
10+
.with_context(_force_unlink=True)
11+
.search([("name", "=", "smart_search")])
12+
)
13+
if fields_to_unlink:
14+
fields_to_unlink.unlink()
1415
_logger.info("Done!")

base_name_search_improved/models/ir_model.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import logging
55
from ast import literal_eval
6+
from collections import defaultdict
67

78
from lxml import etree
89

@@ -55,12 +56,13 @@ def _get_name_search_domain(self):
5556
return []
5657

5758

58-
def _extend_name_results(self, domain, results, limit, name_get_uid):
59+
def _extend_name_results(self, domain, results, limit):
5960
result_count = len(results)
6061
if result_count < limit:
6162
domain += [("id", "not in", results)]
6263
rec_ids = self._search(
63-
domain, limit=limit - result_count, access_rights_uid=name_get_uid
64+
domain,
65+
limit=limit - result_count,
6466
)
6567
results.extend(rec_ids)
6668
return results
@@ -69,40 +71,35 @@ def _extend_name_results(self, domain, results, limit, name_get_uid):
6971
def patch_name_search():
7072
@api.model
7173
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
7375
):
7476
# Perform standard name search
7577
res = _name_search.origin(
7678
self,
7779
name=name,
78-
args=args,
79-
operator=operator,
80+
domain=domain,
8081
limit=limit,
81-
name_get_uid=name_get_uid,
82+
order=order,
8283
)
8384
if name and _get_use_smart_name_search(self.sudo()) and operator in ALLOWED_OPS:
8485
# _name_search.origin is a query, we need to convert it to a list
8586
res = self.browse(res).ids
8687
limit = limit or 0
8788

8889
# we add domain
89-
args = args or [] + _get_name_search_domain(self.sudo())
90+
args = domain or [] + _get_name_search_domain(self.sudo())
9091

9192
# Support a list of fields to search on
9293
all_names = _get_rec_names(self.sudo())
9394
base_domain = args or []
9495
# Try regular search on each additional search field
9596
for rec_name in all_names[1:]:
9697
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)
10099
# Try ordered word search on each of the search fields
101100
for rec_name in all_names:
102101
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)
106103
# Try unordered word search on each of the search fields
107104
# we only perform this search if we have at least one
108105
# separator character
@@ -116,9 +113,7 @@ def _name_search(
116113
word_domain and ["|"] + word_domain or word_domain
117114
) + [(rec_name, operator, word)]
118115
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)
122117

123118
return res
124119

@@ -130,8 +125,7 @@ class Base(models.AbstractModel):
130125

131126
# TODO perhaps better to create only the field when enabled on the model
132127
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
135129
)
136130

137131
def _compute_smart_search(self):
@@ -214,7 +208,7 @@ def _compute_smart_search_warning(self):
214208

215209
@api.constrains("name_search_ids", "name_search_domain", "add_smart_search")
216210
def update_search_wo_restart(self):
217-
self.clear_caches()
211+
self.env.registry.clear_cache()
218212

219213
@api.constrains("name_search_domain")
220214
def check_name_search_domain(self):
@@ -252,9 +246,16 @@ def _register_hook(self):
252246
"""
253247
_logger.info("Patching BaseModel for Smart Search")
254248

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+
255257
for model in self.sudo().search(self.ids or []):
256258
Model = self.env.get(model.model)
257259
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())
260261
return super()._register_hook()

base_name_search_improved/readme/CONFIGURE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ the top right search box, is not affected.
55
Additional search fields can be configured at Settings \> Technical \>
66
Database \> Models, using the "Name Search Fields" field.
77

8-
![](https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image1.png)
8+
![image1](https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image1.png)

base_name_search_improved/readme/DESCRIPTION.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ relaxed search also looks up for records containing all the words, so
1010
"John M. Brown" would be a match. It also tolerates words in a different
1111
order, so searching for "brown john" also works.
1212

13-
![](https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image0.png)
13+
![image0](https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image0.png)
1414

1515
Additionally, an Administrator can configure other fields to also lookup
1616
into. For example, Customers could be additionally searched by City or
1717
Phone number.
1818

19-
![](https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image2.png)
19+
![image2](https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image2.png)
2020

2121
How it works:
2222

0 commit comments

Comments
 (0)