From 2d95afc1f7af031a8de1617d0f5de47f6e500659 Mon Sep 17 00:00:00 2001 From: Areeb Siddiqi Date: Fri, 28 May 2021 15:07:09 -0400 Subject: [PATCH] base_search_fuzzy: Migration to 14.0 --- base_search_fuzzy/__init__.py | 1 + base_search_fuzzy/models/__init__.py | 2 + base_search_fuzzy/models/ir_model.py | 13 +++-- base_search_fuzzy/models/query.py | 49 +++++++++++++++++++ base_search_fuzzy/models/trgm_index.py | 9 ++-- base_search_fuzzy/readme/CONTRIBUTORS.rst | 2 +- .../tests/test_query_generation.py | 5 +- 7 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 base_search_fuzzy/models/query.py diff --git a/base_search_fuzzy/__init__.py b/base_search_fuzzy/__init__.py index 9fd9692bc..14b61a7d5 100644 --- a/base_search_fuzzy/__init__.py +++ b/base_search_fuzzy/__init__.py @@ -1,4 +1,5 @@ # Copyright 2016 ForgeFlow S.L. # Copyright 2016 Serpent Consulting Services Pvt. Ltd. +# Copyright 2020 NextERP SRL. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from . import models diff --git a/base_search_fuzzy/models/__init__.py b/base_search_fuzzy/models/__init__.py index 7a1159b1d..d4b09c313 100644 --- a/base_search_fuzzy/models/__init__.py +++ b/base_search_fuzzy/models/__init__.py @@ -1,5 +1,7 @@ # Copyright 2016 ForgeFlow S.L. # Copyright 2016 Serpent Consulting Services Pvt. Ltd. +# Copyright 2020 NextERP SRL. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from . import ir_model from . import trgm_index +from . import query diff --git a/base_search_fuzzy/models/ir_model.py b/base_search_fuzzy/models/ir_model.py index eb1ecbe1e..d17776de7 100644 --- a/base_search_fuzzy/models/ir_model.py +++ b/base_search_fuzzy/models/ir_model.py @@ -1,6 +1,7 @@ # Copyright 2016 ForgeFlow S.L. # Copyright 2016 Serpent Consulting Services Pvt. Ltd. # Copyright 2017 LasLabs Inc. +# Copyright 2020 NextERP SRL. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). import logging @@ -11,12 +12,9 @@ _logger = logging.getLogger(__name__) def patch_leaf_trgm(method): - def decorate_leaf_to_sql(self, eleaf): - model = eleaf.model - leaf = eleaf.leaf + def decorate_leaf_to_sql(self, leaf, model, alias): left, operator, right = leaf - table_alias = '"%s"' % (eleaf.generate_alias()) - + table_alias = '"%s"' % alias if operator == "%": sql_operator = "%%" @@ -45,8 +43,9 @@ def patch_leaf_trgm(method): return query, params elif operator == "inselect": right = (right[0].replace(" % ", " %% "), right[1]) - eleaf.leaf = (left, operator, right) - return method(self, eleaf) + leaf = (left, operator, right) + + return method(self, leaf, model, alias) decorate_leaf_to_sql.__decorated__ = True diff --git a/base_search_fuzzy/models/query.py b/base_search_fuzzy/models/query.py new file mode 100644 index 000000000..543de878b --- /dev/null +++ b/base_search_fuzzy/models/query.py @@ -0,0 +1,49 @@ +# Copyright 2016 ForgeFlow S.L. +# Copyright 2016 Serpent Consulting Services Pvt. Ltd. +# Copyright 2017 LasLabs Inc. +# Copyright 2020 NextERP SRL. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +from odoo.osv import query + +Oridinal_Query_obj = query.Query + + +def percent_search_fuzzy(self, where_claus): + if " % " in " ".join(where_claus): + new_where_clause = [x.replace(" % ", " %% ") for x in where_claus] + return tuple(new_where_clause) + return where_claus + + +Oridinal_Query_obj.percent_search_fuzzy = percent_search_fuzzy + + +# @property +def where_clause_new(self): + ok_where = self.percent_search_fuzzy(self._where_clausess) + return tuple(ok_where) + + +# original return(self._where_clausess) +Oridinal_Query_obj.where_clause = where_clause_new + + +def get_sql_new(self): + """ Returns (query_from, query_where, query_params). """ + tables = [query._from_table(table, alias) for alias, table in self._tables.items()] + joins = [] + params = [] + for alias, (kind, table, condition, condition_params) in self._joins.items(): + joins.append(f"{kind} {query._from_table(table, alias)} ON ({condition})") + params.extend(condition_params) + + from_clause = " ".join([", ".join(tables)] + joins) + ok_where = self.percent_search_fuzzy(self._where_clauses) + where_clause = " AND ".join(ok_where) + # original where_clause = " AND ".join(self._where_clauses) + return from_clause, where_clause, params + self._where_params + + +Oridinal_Query_obj.get_sql = get_sql_new diff --git a/base_search_fuzzy/models/trgm_index.py b/base_search_fuzzy/models/trgm_index.py index f05e0125f..2d7f54df6 100644 --- a/base_search_fuzzy/models/trgm_index.py +++ b/base_search_fuzzy/models/trgm_index.py @@ -22,6 +22,7 @@ class TrgmIndex(models.Model): field_id = fields.Many2one( comodel_name="ir.model.fields", string="Field", + ondelete="set default", required=True, help='You can either select a field of type "text" or "char".', ) @@ -41,10 +42,10 @@ class TrgmIndex(models.Model): string="Index Type", default="gin", required=True, - help='Cite from PostgreSQL documentation: "As a rule of thumb, a ' - "GIN index is faster to search than a GiST index, but slower to " - "build or update; so GIN is better suited for static data and " - 'GiST for often-updated data."', + ondelete={"gin": "set default", "gist": "set default"}, + help="Cite from PostgreSQL documentation: GIN indexes are " + "the preferred text search index type." + "See: https://www.postgresql.org/docs/current/textsearch-indexes.html", ) def _trgm_extension_exists(self): diff --git a/base_search_fuzzy/readme/CONTRIBUTORS.rst b/base_search_fuzzy/readme/CONTRIBUTORS.rst index e947cbbec..37599d228 100644 --- a/base_search_fuzzy/readme/CONTRIBUTORS.rst +++ b/base_search_fuzzy/readme/CONTRIBUTORS.rst @@ -4,6 +4,6 @@ * Dave Lasley * `Tecnativa `_: - * Vicent Cubells * Ernesto Tejeda +* teodoralexandru@nexterp.ro 2020 NextERP SRL. diff --git a/base_search_fuzzy/tests/test_query_generation.py b/base_search_fuzzy/tests/test_query_generation.py index d9f4434ca..fa88e975d 100644 --- a/base_search_fuzzy/tests/test_query_generation.py +++ b/base_search_fuzzy/tests/test_query_generation.py @@ -49,7 +49,8 @@ class QueryGenerationCase(TransactionCase): # the % parameter has to be escaped (%%) for the string replation self.assertIn( - """SELECT id FROM temp_irt_current WHERE name %% %s""", where_clause + """COALESCE("res_partner_category__name"."value", "res_partner_category"."name") %% %s""", # noqa + where_clause, ) complete_where = self.env.cr.mogrify( @@ -58,7 +59,7 @@ class QueryGenerationCase(TransactionCase): ) self.assertIn( - b"""SELECT id FROM temp_irt_current WHERE name % 'Goschaeftlic'""", + 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 complete_where, )