Mercurial > libervia-web
comparison libervia/server/pages.py @ 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 | 822bd0139769 |
comparison
equal
deleted
inserted
replaced
1392:e11a71a08a48 | 1393:d9a328ddef9c |
---|---|
1519 raise failure.Failure(exceptions.CancelError("Post/Redirect/Get is used")) | 1519 raise failure.Failure(exceptions.CancelError("Post/Redirect/Get is used")) |
1520 else: | 1520 else: |
1521 if ret != "continue": | 1521 if ret != "continue": |
1522 self._on_data_post_redirect(ret, request) | 1522 self._on_data_post_redirect(ret, request) |
1523 | 1523 |
1524 def getPostedData(self, request, keys, multiple=False, raise_on_missing=True): | 1524 def getPostedData( |
1525 self, | |
1526 request: server.Request, | |
1527 keys, | |
1528 multiple: bool = False, | |
1529 raise_on_missing: bool = True, | |
1530 strip: bool = True | |
1531 ): | |
1525 """Get data from a POST request or from URL's query part and decode it | 1532 """Get data from a POST request or from URL's query part and decode it |
1526 | 1533 |
1527 @param request(server.Request): request linked to the session | 1534 @param request: request linked to the session |
1528 @param keys(unicode, iterable[unicode]): name of the value(s) to get | 1535 @param keys(unicode, iterable[unicode]): name of the value(s) to get |
1529 unicode to get one value | 1536 unicode to get one value |
1530 iterable to get more than one | 1537 iterable to get more than one |
1531 @param multiple(bool): True if multiple values are possible/expected | 1538 @param multiple: True if multiple values are possible/expected |
1532 if False, the first value is returned | 1539 if False, the first value is returned |
1533 @param raise_on_missing(bool): raise KeyError on missing key if True | 1540 @param raise_on_missing: raise KeyError on missing key if True |
1534 else use None for missing values | 1541 else use None for missing values |
1542 @param strip: if True, apply "strip()" on values | |
1535 @return (iterator[unicode], list[iterator[unicode], unicode, list[unicode]): | 1543 @return (iterator[unicode], list[iterator[unicode], unicode, list[unicode]): |
1536 values received for this(these) key(s) | 1544 values received for this(these) key(s) |
1537 @raise KeyError: one specific key has been requested, and it is missing | 1545 @raise KeyError: one specific key has been requested, and it is missing |
1538 """ | 1546 """ |
1539 # FIXME: request.args is already unquoting the value, it seems we are doing | 1547 # FIXME: request.args is already unquoting the value, it seems we are doing |
1546 ret = [] | 1554 ret = [] |
1547 for key in keys: | 1555 for key in keys: |
1548 gen = (urllib.parse.unquote(v.decode("utf-8")) | 1556 gen = (urllib.parse.unquote(v.decode("utf-8")) |
1549 for v in request.args.get(key, [])) | 1557 for v in request.args.get(key, [])) |
1550 if multiple: | 1558 if multiple: |
1551 ret.append(gen) | 1559 ret.append(gen.strip() if strip else gen) |
1552 else: | 1560 else: |
1553 try: | 1561 try: |
1554 ret.append(next(gen)) | 1562 v = next(gen) |
1555 except StopIteration: | 1563 except StopIteration: |
1556 if raise_on_missing: | 1564 if raise_on_missing: |
1557 raise KeyError(key) | 1565 raise KeyError(key) |
1558 else: | 1566 else: |
1559 ret.append(None) | 1567 ret.append(None) |
1568 else: | |
1569 ret.append(v.strip() if strip else v) | |
1560 | 1570 |
1561 if len(keys) == 1: | 1571 if len(keys) == 1: |
1562 return ret[0] | 1572 return ret[0] |
1563 else: | 1573 else: |
1564 return ret | 1574 return ret |