From a3877cadb3a50dc96a91af0d3362d860a44b9d2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 25 Sep 2023 16:42:04 +0200 Subject: [PATCH] Add likify tools --- trytond/CHANGELOG | 1 + trytond/doc/ref/tools/misc.rst | 4 ++++ trytond/trytond/tests/test_tools.py | 15 ++++++++++++++- trytond/trytond/tools/__init__.py | 3 ++- trytond/trytond/tools/misc.py | 10 ++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/trytond/CHANGELOG b/trytond/CHANGELOG index 34e10a54eb..6639287c07 100644 --- a/trytond/CHANGELOG +++ b/trytond/CHANGELOG @@ -1,3 +1,4 @@ +* Add likify tools * Remove user from contextual key of Cache * Remove user from evaluation context of rule domain * Allow customizing context per model for rule diff --git a/trytond/doc/ref/tools/misc.rst b/trytond/doc/ref/tools/misc.rst index da9305d753..eaec07c0a1 100644 --- a/trytond/doc/ref/tools/misc.rst +++ b/trytond/doc/ref/tools/misc.rst @@ -36,6 +36,10 @@ Miscellaneous This is the case when the value starts and ends with a ``%`` or does not contain any wild cards. +.. function:: likify(string[, escape]) + + Convert the string for full text if it does not contain any wild cards. + .. function:: sql_pairing(x, y) Return an SQL expression that pairs SQL integers x and y. diff --git a/trytond/trytond/tests/test_tools.py b/trytond/trytond/tests/test_tools.py index e2920f8531..8aee2a7bd0 100644 --- a/trytond/trytond/tests/test_tools.py +++ b/trytond/trytond/tests/test_tools.py @@ -13,7 +13,7 @@ import sql.operators from trytond.tools import ( decimal_, escape_wildcard, file_open, firstline, grouped_slice, - is_full_text, is_instance_method, lstrip_wildcard, reduce_domain, + is_full_text, is_instance_method, likify, lstrip_wildcard, reduce_domain, reduce_ids, remove_forbidden_chars, rstrip_wildcard, slugify, sortable_values, strip_wildcard, timezone, unescape_wildcard) from trytond.tools.domain_inversion import ( @@ -252,6 +252,19 @@ class ToolsTestCase(unittest.TestCase): with self.subTest(value=value): self.assertEqual(is_full_text(value), result) + def test_likify(self): + "Test likify" + for value, result in [ + ('', '%'), + ('foo', '%foo%'), + ('foo%', 'foo%'), + ('f_o', 'f_o'), + ('%foo%', '%foo%'), + ('foo\\_bar', '%foo\\_bar%'), + ]: + with self.subTest(value=value): + self.assertEqual(likify(value), result) + def test_slugify(self): "Test slugify" self.assertEqual(slugify('unicode ♥ is ☢'), 'unicode-is') diff --git a/trytond/trytond/tools/__init__.py b/trytond/trytond/tools/__init__.py index 38156ef0a3..9de5bd1203 100644 --- a/trytond/trytond/tools/__init__.py +++ b/trytond/trytond/tools/__init__.py @@ -15,7 +15,7 @@ except ImportError: from .decimal_ import decistmt from .misc import ( escape_wildcard, file_open, find_dir, find_path, firstline, - get_smtp_server, grouped_slice, is_full_text, is_instance_method, + get_smtp_server, grouped_slice, is_full_text, is_instance_method, likify, lstrip_wildcard, reduce_domain, reduce_ids, remove_forbidden_chars, resolve, rstrip_wildcard, slugify, sortable_values, sql_pairing, strip_wildcard, unescape_wildcard) @@ -50,6 +50,7 @@ __all__ = [ grouped_slice, is_full_text, is_instance_method, + likify, lstrip_wildcard, reduce_domain, reduce_ids, diff --git a/trytond/trytond/tools/misc.py b/trytond/trytond/tools/misc.py index 1bb1d87e87..3ef04b42c7 100644 --- a/trytond/trytond/tools/misc.py +++ b/trytond/trytond/tools/misc.py @@ -253,6 +253,16 @@ def is_full_text(value, escape='\\'): return value.startswith('%') == value.endswith('%') +def likify(string, escape='\\'): + if not string: + return '%' + escaped = string.replace(escape + '%', '').replace(escape + '_', '') + if '%' in escaped or '_' in escaped: + return string + else: + return '%' + string + '%' + + _slugify_strip_re = re.compile(r'[^\w\s-]') _slugify_hyphenate_re = re.compile(r'[-\s]+')