Skip to content

Commit

Permalink
[MIG] base_search_fuzzy: Migrated to v16
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikul-OSI authored and dreispt committed Feb 4, 2023
1 parent ca64967 commit 3b5f41c
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion base_search_fuzzy/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Fuzzy Search",
"summary": "Fuzzy search with the PostgreSQL trigram extension",
"category": "Uncategorized",
"version": "15.0.1.0.0",
"version": "16.0.1.0.0",
"website": "https://github.com/OCA/server-tools",
"author": "bloopark systems GmbH & Co. KG, "
"ForgeFlow, "
Expand Down
1 change: 0 additions & 1 deletion base_search_fuzzy/demo/TrgmIndex_demo.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="trgm_index_gin_partner_name" model="trgm.index">
<field name="field_id" ref="base.field_res_partner__name" />
Expand Down
4 changes: 3 additions & 1 deletion base_search_fuzzy/demo/res_partner_demo.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="res_partner_John_Smith" model="res.partner">
<field name="name">John Smith</field>
</record>

<record id="res_partner_John_Smizz" model="res.partner">
<field name="name">John Smizz</field>
</record>

<record id="res_partner_Linus_Torvalds" model="res.partner">
<field name="name">Linus Torvalds</field>
</record>

<record id="view_res_partner_filter" model="ir.ui.view">
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_res_partner_filter" />
Expand Down
4 changes: 2 additions & 2 deletions base_search_fuzzy/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ def _wrapper(self, leaf, model, alias):
raise ValueError(
"Invalid field {!r} in domain term {!r}".format(left, leaf)
)

# Generate correct WHERE clause part
query = '("{}"."{}" %% {})'.format(
query = '("{}"."{}" %% %s)'.format(
alias,
left,
model._fields[left].column_format,
)
params = [right]
return query, params
Expand Down
2 changes: 1 addition & 1 deletion base_search_fuzzy/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


from odoo.osv import query
from odoo.tools import query

Oridinal_Query_obj = query.Query

Expand Down
11 changes: 6 additions & 5 deletions base_search_fuzzy/models/trgm_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ def index_exists(self, model_name, field_name):
trgm_index = self.search([("field_id", "=", field.id)], limit=1)
return bool(trgm_index)

@api.model
def create(self, vals):
rec = super().create(vals)
rec.index_name = rec.create_index()
return rec
@api.model_create_multi
def create(self, vals_list):
recs = super().create(vals_list)
for rec in recs:
rec.index_name = rec.create_index()
return recs

def unlink(self):
for rec in self:
Expand Down
3 changes: 3 additions & 0 deletions base_search_fuzzy/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
* `Tecnativa <https://www.tecnativa.com>`_:
* Vicent Cubells
* Ernesto Tejeda

* [email protected] 2020 NextERP SRL.
* Daniel Reis <[email protected]>
* Nikul Chaudhary <[email protected]>
10 changes: 5 additions & 5 deletions base_search_fuzzy/tests/test_query_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_fuzzy_where_generation_translatable(self):

# the % parameter has to be escaped (%%) for the string replation
self.assertIn(
"""COALESCE("res_partner_category__name"."value", "res_partner_category"."name") %% %s""", # noqa
"""("res_partner_category"."name"->>'en_US' %% %s)""",
where_clause,
)

Expand All @@ -56,7 +56,7 @@ def test_fuzzy_where_generation_translatable(self):
)

self.assertIn(
b"""SELECT FROM "res_partner_category" LEFT JOIN "ir_translation" AS "res_partner_category__name" ON ("res_partner_category"."id" = "res_partner_category__name"."res_id" AND "res_partner_category__name"."type" = \'model\' AND "res_partner_category__name"."name" = \'res.partner.category,name\' AND "res_partner_category__name"."lang" = \'de_DE\' AND "res_partner_category__name"."value" != \'\') WHERE COALESCE("res_partner_category__name"."value", "res_partner_category"."name") % \'Goschaeftlic\'""", # noqa
b'SELECT FROM "res_partner_category" WHERE ("res_partner_category"."name"->>\'en_US\' % \'Goschaeftlic\')', # noqa
complete_where,
)

Expand All @@ -71,9 +71,9 @@ def test_fuzzy_search(self):
{"field_id": field_partner_name.id, "index_type": "gin"}
)

partner1 = self.ResPartner.create({"name": "John Smith"})
partner2 = self.ResPartner.create({"name": "John Smizz"})
partner3 = self.ResPartner.create({"name": "Linus Torvalds"})
partner1, partner2, partner3 = self.ResPartner.create(
[{"name": "John Smith"}, {"name": "John Smizz"}, {"name": "Linus Torvalds"}]
)

res = self.ResPartner.search([("name", "%", "Jon Smith")])
self.assertIn(partner1.id, res.ids)
Expand Down
1 change: 1 addition & 0 deletions setup/base_search_fuzzy/odoo/addons/base_search_fuzzy
6 changes: 6 additions & 0 deletions setup/base_search_fuzzy/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 3b5f41c

Please sign in to comment.