diff --git a/base_name_search_improved/README.rst b/base_name_search_improved/README.rst index a2a1c3cc03b..e15806fadb7 100644 --- a/base_name_search_improved/README.rst +++ b/base_name_search_improved/README.rst @@ -69,12 +69,15 @@ hopefully presenting them in order of relevance. Configuration ============= -The fuzzy search is automatically enabled on all Models. -Note that this only affects typing in related fields. -The regular ``search()``, used in the top right search box, is not affected. +In "Settings > Smart searches" enable the smart search in the necessary models and configure the search fields. +1. Smart search button: enable smart search in search views +2. Smart name search button: enables smart search in related fields. + +.. figure:: https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image3.png + :alt: Name Search Tree + :width: 600 px + -Additional search fields can be configured at Settings > Technical > Database > Models, -using the "Name Search Fields" field. .. figure:: https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image1.png :alt: Name Search Fields diff --git a/base_name_search_improved/__manifest__.py b/base_name_search_improved/__manifest__.py index 447a01b21f3..f110da53c7d 100644 --- a/base_name_search_improved/__manifest__.py +++ b/base_name_search_improved/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Improved Name Search", "summary": "Friendlier search when typing in relation fields", - "version": "14.0.1.1.0", + "version": "16.0.1.0.0", "category": "Uncategorized", "website": "https://github.com/OCA/server-tools", "author": "Daniel Reis, Odoo Community Association (OCA), ADHOC SA", diff --git a/base_name_search_improved/hooks.py b/base_name_search_improved/hooks.py index b8df8de3dae..ff9ed79b47b 100644 --- a/base_name_search_improved/hooks.py +++ b/base_name_search_improved/hooks.py @@ -7,7 +7,6 @@ def uninstall_hook(cr, registry): _logger.info("Reverting Patches...") - models.BaseModel._revert_method("fields_view_get") env = api.Environment(cr, SUPERUSER_ID, {}) env["ir.model.fields"].with_context(_force_unlink=True).search( [("name", "=", "smart_search")] diff --git a/base_name_search_improved/images/image3.png b/base_name_search_improved/images/image3.png new file mode 100644 index 00000000000..62ab354ba03 Binary files /dev/null and b/base_name_search_improved/images/image3.png differ diff --git a/base_name_search_improved/models/ir_model.py b/base_name_search_improved/models/ir_model.py index c45eff4d2f2..d6fde67cf75 100644 --- a/base_name_search_improved/models/ir_model.py +++ b/base_name_search_improved/models/ir_model.py @@ -81,6 +81,8 @@ def _name_search( name_get_uid=name_get_uid, ) 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 @@ -123,34 +125,6 @@ def _name_search( return _name_search -def patch_fields_view_get(): - @api.model - def fields_view_get( - self, view_id=None, view_type="form", toolbar=False, submenu=False - ): - res = fields_view_get.origin( - self, - view_id=view_id, - view_type=view_type, - toolbar=toolbar, - submenu=submenu, - ) - if view_type == "search" and _get_add_smart_search(self): - eview = etree.fromstring(res["arch"]) - placeholders = eview.xpath("//search/field") - if placeholders: - placeholder = placeholders[0] - else: - placeholder = eview.xpath("//search")[0] - placeholder.addnext(etree.Element("field", {"name": "smart_search"})) - eview.remove(placeholder) - res["arch"] = etree.tostring(eview) - res["fields"].update(self.fields_get(["smart_search"])) - return res - - return fields_view_get - - class Base(models.AbstractModel): _inherit = "base" @@ -189,6 +163,19 @@ def _search_smart_search(self, operator, value): return domain return [] + @api.model + def _get_view(self, view_id=None, view_type="form", **options): + arch, view = super()._get_view(view_id=view_id, view_type=view_type, **options) + if view_type == "search" and _get_add_smart_search(self): + placeholders = arch.xpath("//search/field") + if placeholders: + placeholder = placeholders[0] + else: + placeholder = arch.xpath("//search")[0] + placeholder.addnext(etree.Element("field", {"name": "smart_search"})) + arch.remove(placeholder) + return arch, view + class IrModel(models.Model): _inherit = "ir.model" @@ -236,21 +223,26 @@ def check_name_search_domain(self): name_search_domain = False try: name_search_domain = literal_eval(rec.name_search_domain) - except Exception as error: + except ( + ValueError, + TypeError, + SyntaxError, + MemoryError, + RecursionError, + ) as e: raise ValidationError( - _("Couldn't eval Name Search Domain (%s)") % error - ) + _("Couldn't eval Name Search Domain (%s)") % e + ) from e if not isinstance(name_search_domain, list): raise ValidationError(_("Name Search Domain must be a list of tuples")) def _register_hook(self): _logger.info("Patching BaseModel for Smart Search") - models.BaseModel._patch_method("fields_view_get", patch_fields_view_get()) for model in self.sudo().search(self.ids or []): Model = self.env.get(model.model) if Model is not None: Model._patch_method("_name_search", patch_name_search()) - return super(IrModel, self)._register_hook() + return super()._register_hook() diff --git a/base_name_search_improved/static/description/index.html b/base_name_search_improved/static/description/index.html index 5eda3c5d974..98dc2e4cb62 100644 --- a/base_name_search_improved/static/description/index.html +++ b/base_name_search_improved/static/description/index.html @@ -417,11 +417,16 @@

Improved Name Search

Configuration

-

The fuzzy search is automatically enabled on all Models. -Note that this only affects typing in related fields. -The regular search(), used in 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.

+

In "Settings > Smart searches" enable the smart search in the necessary models and configure the search fields. +

+ +
+ Name Search tree00 +
+
Name Search Fields