Skip to content

Commit

Permalink
[MIG] base_name_search_improved: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
filoquin committed Mar 15, 2023
1 parent f15efb0 commit 681c71b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 45 deletions.
13 changes: 8 additions & 5 deletions base_name_search_improved/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
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": "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",
Expand Down
1 change: 0 additions & 1 deletion base_name_search_improved/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Binary file added base_name_search_improved/images/image3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 25 additions & 33 deletions base_name_search_improved/models/ir_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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()
15 changes: 10 additions & 5 deletions base_name_search_improved/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,16 @@ <h1 class="title">Improved Name Search</h1>
</div>
<div class="section" id="configuration">
<h1><a class="toc-backref" href="#id1">Configuration</a></h1>
<p>The fuzzy search is automatically enabled on all Models.
Note that this only affects typing in related fields.
The regular <tt class="docutils literal">search()</tt>, used in the top right search box, is not affected.</p>
<p>Additional search fields can be configured at Settings &gt; Technical &gt; Database &gt; Models,
using the “Name Search Fields” field.</p>
<p>In "Settings > Smart searches" enable the smart search in the necessary models and configure the search fields.
</p>
<ul class="simple">
<li> Smart search button: enable smart search in search views</li>
<li>Smart name search button: enables smart search in related fields.</li>
</ul>
<div class="figure">
<img alt="Name Search tree" src="https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image1.png" style="width: 600px;" />00
</div>

<div class="figure">
<img alt="Name Search Fields" src="https://raw.githubusercontent.com/OCA/server-tools/11.0/base_name_search_improved/images/image1.png" style="width: 600px;" />
</div>
Expand Down

0 comments on commit 681c71b

Please sign in to comment.