Add likify tools

This commit is contained in:
Cédric Krier
2023-09-25 16:42:04 +02:00
parent 66e460b97d
commit a3877cadb3
5 changed files with 31 additions and 2 deletions
+1
View File
@@ -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
+4
View File
@@ -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.
+14 -1
View File
@@ -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')
+2 -1
View File
@@ -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,
+10
View File
@@ -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]+')