diff libervia/server/pages.py @ 1187:dab7a2b151ea

server (pages): added raise_on_missing arguments in getPostedData: raise_on_missing argument can be set to False to use None for missing args instead of raising an exception.
author Goffi <goffi@goffi.org>
date Sun, 26 May 2019 22:18:04 +0200
parents 352865f4a268
children 263fed3ce354
line wrap: on
line diff
--- a/libervia/server/pages.py	Sun May 26 22:16:07 2019 +0200
+++ b/libervia/server/pages.py	Sun May 26 22:18:04 2019 +0200
@@ -1314,8 +1314,8 @@
         d.addCallback(self._on_data_post_redirect, request)
         return d
 
-    def getPostedData(self, request, keys, multiple=False):
-        """get data from a POST request or from URL's query part and decode it
+    def getPostedData(self, request, keys, multiple=False, raise_on_missing=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 keys(unicode, iterable[unicode]): name of the value(s) to get
@@ -1323,6 +1323,8 @@
             iterable to get more than one
         @param multiple(bool): 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
+            else use None for missing 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
@@ -1344,7 +1346,10 @@
                 try:
                     ret.append(next(gen))
                 except StopIteration:
-                    raise KeyError(key)
+                    if raise_on_missing:
+                        raise KeyError(key)
+                    else:
+                        ret.append(None)
 
         return ret[0] if get_first else ret