Skip to content

Commit

Permalink
[MIG] base_name_search_improved: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rov-adhoc authored and lef-adhoc committed Nov 26, 2024
1 parent d807245 commit 81aad35
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 106 deletions.
6 changes: 3 additions & 3 deletions base_name_search_improved/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Improved Name Search
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:88daa951eef68e162381052878cb525cb0c1e6b0a72cd2a1f9d138dca31bd6f4
!! source digest: sha256:fe0fce7aeb356dfbf982cb648994712ec1907ee6ab73a221eee4cda4039e7a33
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down Expand Up @@ -40,7 +40,7 @@ relaxed search also looks up for records containing all the words, so
"John M. Brown" would be a match. It also tolerates words in a different
order, so searching for "brown john" also works.

|image1|
|image0|

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

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

**Table of contents**
Expand Down
2 changes: 1 addition & 1 deletion base_name_search_improved/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Improved Name Search",
"summary": "Friendlier search when typing in relation fields",
"version": "16.0.1.0.0",
"version": "17.0.1.0.0",
"category": "Uncategorized",
"website": "https://github.com/OCA/server-tools",
"author": "Daniel Reis, Odoo Community Association (OCA), ADHOC SA",
Expand Down
15 changes: 8 additions & 7 deletions base_name_search_improved/hooks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import logging

from odoo import SUPERUSER_ID, api

_logger = logging.getLogger(__name__)


def uninstall_hook(cr, registry):
def uninstall_hook(env):
_logger.info("Reverting Patches...")
env = api.Environment(cr, SUPERUSER_ID, {})
env["ir.model.fields"].with_context(_force_unlink=True).search(
[("name", "=", "smart_search")]
).unlink()
fields_to_unlink = (
env["ir.model.fields"]
.with_context(_force_unlink=True)
.search([("name", "=", "smart_search")])
)
if fields_to_unlink:
fields_to_unlink.unlink()
_logger.info("Done!")
43 changes: 22 additions & 21 deletions base_name_search_improved/models/ir_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import logging
from ast import literal_eval
from collections import defaultdict

from lxml import etree

Expand Down Expand Up @@ -55,12 +56,13 @@ def _get_name_search_domain(self):
return []


def _extend_name_results(self, domain, results, limit, name_get_uid):
def _extend_name_results(self, domain, results, limit):
result_count = len(results)
if result_count < limit:
domain += [("id", "not in", results)]
rec_ids = self._search(
domain, limit=limit - result_count, access_rights_uid=name_get_uid
domain,
limit=limit - result_count,
)
results.extend(rec_ids)
return results
Expand All @@ -69,40 +71,35 @@ def _extend_name_results(self, domain, results, limit, name_get_uid):
def patch_name_search():
@api.model
def _name_search(
self, name="", args=None, operator="ilike", limit=100, name_get_uid=None
self, name="", domain=None, operator="ilike", limit=100, order=None
):
# Perform standard name search
res = _name_search.origin(
self,
name=name,
args=args,
operator=operator,
domain=domain,
limit=limit,
name_get_uid=name_get_uid,
order=order,
)
if name and _get_use_smart_name_search(self.sudo()) and operator in ALLOWED_OPS:
# _name_search.origin is a query, we need to convert it to a list
res = self.browse(res).ids
limit = limit or 0

# we add domain
args = args or [] + _get_name_search_domain(self.sudo())
args = domain or [] + _get_name_search_domain(self.sudo())

# Support a list of fields to search on
all_names = _get_rec_names(self.sudo())
base_domain = args or []
# Try regular search on each additional search field
for rec_name in all_names[1:]:
domain = [(rec_name, operator, name)]
res = _extend_name_results(
self, base_domain + domain, res, limit, name_get_uid
)
res = _extend_name_results(self, base_domain + domain, res, limit)
# Try ordered word search on each of the search fields
for rec_name in all_names:
domain = [(rec_name, operator, name.replace(" ", "%"))]
res = _extend_name_results(
self, base_domain + domain, res, limit, name_get_uid
)
res = _extend_name_results(self, base_domain + domain, res, limit)
# Try unordered word search on each of the search fields
# we only perform this search if we have at least one
# separator character
Expand All @@ -116,9 +113,7 @@ def _name_search(
word_domain and ["|"] + word_domain or word_domain
) + [(rec_name, operator, word)]
domain = (domain and ["&"] + domain or domain) + word_domain
res = _extend_name_results(
self, base_domain + domain, res, limit, name_get_uid
)
res = _extend_name_results(self, base_domain + domain, res, limit)

return res

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

# TODO perhaps better to create only the field when enabled on the model
smart_search = fields.Char(
compute="_compute_smart_search",
search="_search_smart_search",
compute="_compute_smart_search", search="_search_smart_search", translate=False
)

def _compute_smart_search(self):
Expand Down Expand Up @@ -214,7 +208,7 @@ def _compute_smart_search_warning(self):

@api.constrains("name_search_ids", "name_search_domain", "add_smart_search")
def update_search_wo_restart(self):
self.clear_caches()
self.env.registry.clear_cache()

@api.constrains("name_search_domain")
def check_name_search_domain(self):
Expand Down Expand Up @@ -252,9 +246,16 @@ def _register_hook(self):
"""
_logger.info("Patching BaseModel for Smart Search")

patched_models = defaultdict(set)

def patch(model, name, method):
if model not in patched_models[name]:
ModelClass = type(model)
method.origin = getattr(ModelClass, name)
setattr(ModelClass, name, method)

for model in self.sudo().search(self.ids or []):
Model = self.env.get(model.model)
if Model is not None and not Model._abstract:
Model._patch_method("_name_search", patch_name_search())

patch(Model, "_name_search", patch_name_search())
return super()._register_hook()
2 changes: 1 addition & 1 deletion base_name_search_improved/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ the top right search box, is not affected.
Additional search fields can be configured at Settings \> Technical \>
Database \> Models, using the "Name Search Fields" field.

![](https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image1.png)
![image1](https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image1.png)
4 changes: 2 additions & 2 deletions base_name_search_improved/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ relaxed search also looks up for records containing all the words, so
"John M. Brown" would be a match. It also tolerates words in a different
order, so searching for "brown john" also works.

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

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

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

How it works:

Expand Down
Loading

0 comments on commit 81aad35

Please sign in to comment.