changeset 1393:d9a328ddef9c

pages: implements `strip` argument in `getPostedData`
author Goffi <goffi@goffi.org>
date Sat, 27 Feb 2021 21:03:55 +0100
parents e11a71a08a48
children 72f9639594b2
files libervia/server/pages.py
diffstat 1 files changed, 16 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/server/pages.py	Sat Feb 27 21:03:08 2021 +0100
+++ b/libervia/server/pages.py	Sat Feb 27 21:03:55 2021 +0100
@@ -1521,17 +1521,25 @@
             if ret != "continue":
                 self._on_data_post_redirect(ret, request)
 
-    def getPostedData(self, request, keys, multiple=False, raise_on_missing=True):
+    def getPostedData(
+            self,
+            request: server.Request,
+            keys,
+            multiple: bool = False,
+            raise_on_missing: bool = True,
+            strip: bool = True
+    ):
         """Get data from a POST request or from URL's query part and decode it
 
-        @param request(server.Request): request linked to the session
+        @param request: request linked to the session
         @param keys(unicode, iterable[unicode]): name of the value(s) to get
             unicode to get one value
             iterable to get more than one
-        @param multiple(bool): True if multiple values are possible/expected
+        @param multiple: True if multiple values are possible/expected
             if False, the first value is returned
-        @param raise_on_missing(bool): raise KeyError on missing key if True
+        @param raise_on_missing: raise KeyError on missing key if True
             else use None for missing values
+        @param strip: if True, apply "strip()" on values
         @return (iterator[unicode], list[iterator[unicode], unicode, list[unicode]):
             values received for this(these) key(s)
         @raise KeyError: one specific key has been requested, and it is missing
@@ -1548,15 +1556,17 @@
             gen = (urllib.parse.unquote(v.decode("utf-8"))
                                         for v in request.args.get(key, []))
             if multiple:
-                ret.append(gen)
+                ret.append(gen.strip() if strip else gen)
             else:
                 try:
-                    ret.append(next(gen))
+                    v = next(gen)
                 except StopIteration:
                     if raise_on_missing:
                         raise KeyError(key)
                     else:
                         ret.append(None)
+                else:
+                    ret.append(v.strip() if strip else v)
 
         if len(keys) == 1:
             return ret[0]