# HG changeset patch # User Goffi # Date 1614456235 -3600 # Node ID d9a328ddef9c8b6499115e536c1b7422ae63ee0e # Parent e11a71a08a488034b55811ff753ce3ee6d1d463a pages: implements `strip` argument in `getPostedData` diff -r e11a71a08a48 -r d9a328ddef9c libervia/server/pages.py --- 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]