changeset c25c1b385cb9 in tryton:default
details: https://hg.tryton.org/tryton?cmd=changeset&node=c25c1b385cb9
description:
        Use '\' as escape char for wildcard

        issue10626
        review349031002
diffstat:

 tryton/common/domain_parser.py            |  27 +++++++++++++++++++++------
 tryton/tests/test_common_domain_parser.py |   2 +-
 2 files changed, 22 insertions(+), 7 deletions(-)

diffs (68 lines):

diff -r fd10b051b6b8 -r c25c1b385cb9 tryton/common/domain_parser.py
--- a/tryton/common/domain_parser.py    Wed Aug 11 18:02:25 2021 +0200
+++ b/tryton/common/domain_parser.py    Thu Aug 12 18:39:50 2021 +0200
@@ -109,6 +109,23 @@
         return '%' + value + '%'
 
 
+def is_full_text(value, escape='\\'):
+    escaped = value.strip('%')
+    escaped = escaped.replace(escape + '%', '').replace(escape + '_', '')
+    if '%' in escaped or '_' in escaped:
+        return False
+    return value.startswith('%') and value.endswith('%')
+
+
+def is_like(value, escape='\\'):
+    escaped = value.replace(escape + '%', '').replace(escape + '_', '')
+    return '%' in escaped or '_' in escaped
+
+
+def unescape(value, escape='\\'):
+    return value.replace(escape + '%', '%').replace(escape + '_', '_')
+
+
 def quote(value):
     "Quote string if needed"
     if not isinstance(value, str):
@@ -594,8 +611,7 @@
             if name.endswith('.rec_name'):
                 name = name[:-9]
             if name not in self.fields:
-                escaped = value.replace('%%', '__')
-                if escaped.startswith('%') and escaped.endswith('%'):
+                if is_full_text(value):
                     value = value[1:-1]
                 return quote(value)
             field = self.fields[name]
@@ -606,15 +622,14 @@
                 target = None
 
             if 'ilike' in operator:
-                escaped = value.replace('%%', '__')
-                if escaped.startswith('%') and escaped.endswith('%'):
+                if is_full_text(value):
                     value = value[1:-1]
-                elif '%' not in escaped:
+                elif not is_like(value):
                     if operator == 'ilike':
                         operator = '='
                     else:
                         operator = '!'
-                    value = value.replace('%%', '%')
+                    value = unescape(value)
             def_operator = default_operator(field)
             if def_operator == operator.strip():
                 operator = ''
diff -r fd10b051b6b8 -r c25c1b385cb9 tryton/tests/test_common_domain_parser.py
--- a/tryton/tests/test_common_domain_parser.py Wed Aug 11 18:02:25 2021 +0200
+++ b/tryton/tests/test_common_domain_parser.py Thu Aug 12 18:39:50 2021 +0200
@@ -674,7 +674,7 @@
         self.assertEqual(dom.string([('name', 'ilike', 'Doe')]), 'Name: =Doe')
         self.assertEqual(dom.string([('name', 'ilike', 'Doe%')]), 'Name: Doe%')
         self.assertEqual(
-            dom.string([('name', 'ilike', 'Doe%%')]), 'Name: =Doe%')
+            dom.string([('name', 'ilike', 'Doe\\%')]), 'Name: =Doe%')
         self.assertEqual(
             dom.string([('name', 'not ilike', '%Doe%')]), 'Name: !Doe')
         self.assertEqual(

Reply via email to