diff roundup/rest.py @ 5874:6630baff5f68

Implement exact string search in REST API Now with ':=' instead of '=' an exact string match is requested. In addition we now support '~=' for a substring search. The old semantics of '=' is kept. The new syntax works for all types of properties but only makes a difference for String properties. Note that this is not yet documented, pending discussion if we want to keep this syntax.
author Ralf Schlatterbeck <rsc@runtux.com>
date Tue, 27 Aug 2019 18:37:18 +0200
parents 14f5c3179711
children 2b78e21d7047
line wrap: on
line diff
--- a/roundup/rest.py	Tue Aug 27 17:44:41 2019 +0200
+++ b/roundup/rest.py	Tue Aug 27 18:37:18 2019 +0200
@@ -644,6 +644,7 @@
 
         # Handle filtering and pagination
         filter_props = {}
+        exact_props  = {}
         page = {
             'size': None,
             'index': 1   # setting just size starts at page 1
@@ -696,6 +697,12 @@
                 # like @apiver
                 pass
             else: # serve the filter purpose
+                exact = False
+                if key.endswith (':') :
+                    exact = True
+                    key = key [:-1]
+                elif key.endswith ('~') :
+                    key = key [:-1]
                 p = key.split('.', 1)[0]
                 try: 
                     prop = class_obj.getprops()[p]
@@ -731,17 +738,24 @@
                             vals.append(linkcls.lookup(p))
                     filter_props[key] = vals
                 else:
-                    if key in filter_props:
-                        if isinstance(filter_props[key], list):
-                            filter_props[key].append(value)
+                    if not isinstance (prop, hyperdb.String):
+                        exact = False
+                    props = filter_props
+                    if exact:
+                        props = exact_props
+                    if key in props:
+                        if isinstance(props[key], list):
+                            props[key].append(value)
                         else:
-                            filter_props[key]=[filter_props[key],value]
+                            props[key] = [props[key],value]
                     else:
-                        filter_props[key] = value
+                        props[key] = value
         l = [filter_props]
         kw = {}
         if sort:
             l.append(sort)
+        if exact_props:
+            kw ['exact_match_spec'] = exact_props
         if page ['size'] is not None and page ['size'] > 0:
             kw ['limit'] = page ['size']
             if page ['index'] is not None and page ['index'] > 1:

Roundup Issue Tracker: http://roundup-tracker.org/