comparison 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
comparison
equal deleted inserted replaced
1186:352865f4a268 1187:dab7a2b151ea
1312 self.pageError(request, C.HTTP_FORBIDDEN) 1312 self.pageError(request, C.HTTP_FORBIDDEN)
1313 d = defer.maybeDeferred(self.on_data_post, self, request) 1313 d = defer.maybeDeferred(self.on_data_post, self, request)
1314 d.addCallback(self._on_data_post_redirect, request) 1314 d.addCallback(self._on_data_post_redirect, request)
1315 return d 1315 return d
1316 1316
1317 def getPostedData(self, request, keys, multiple=False): 1317 def getPostedData(self, request, keys, multiple=False, raise_on_missing=True):
1318 """get data from a POST request or from URL's query part and decode it 1318 """Get data from a POST request or from URL's query part and decode it
1319 1319
1320 @param request(server.Request): request linked to the session 1320 @param request(server.Request): request linked to the session
1321 @param keys(unicode, iterable[unicode]): name of the value(s) to get 1321 @param keys(unicode, iterable[unicode]): name of the value(s) to get
1322 unicode to get one value 1322 unicode to get one value
1323 iterable to get more than one 1323 iterable to get more than one
1324 @param multiple(bool): True if multiple values are possible/expected 1324 @param multiple(bool): True if multiple values are possible/expected
1325 if False, the first value is returned 1325 if False, the first value is returned
1326 @param raise_on_missing(bool): raise KeyError on missing key if True
1327 else use None for missing values
1326 @return (iterator[unicode], list[iterator[unicode], unicode, list[unicode]): 1328 @return (iterator[unicode], list[iterator[unicode], unicode, list[unicode]):
1327 values received for this(these) key(s) 1329 values received for this(these) key(s)
1328 @raise KeyError: one specific key has been requested, and it is missing 1330 @raise KeyError: one specific key has been requested, and it is missing
1329 """ 1331 """
1330 #  FIXME: request.args is already unquoting the value, it seems we are doing 1332 #  FIXME: request.args is already unquoting the value, it seems we are doing
1342 ret.append(gen) 1344 ret.append(gen)
1343 else: 1345 else:
1344 try: 1346 try:
1345 ret.append(next(gen)) 1347 ret.append(next(gen))
1346 except StopIteration: 1348 except StopIteration:
1347 raise KeyError(key) 1349 if raise_on_missing:
1350 raise KeyError(key)
1351 else:
1352 ret.append(None)
1348 1353
1349 return ret[0] if get_first else ret 1354 return ret[0] if get_first else ret
1350 1355
1351 def getAllPostedData(self, request, except_=(), multiple=True): 1356 def getAllPostedData(self, request, except_=(), multiple=True):
1352 """get all posted data 1357 """get all posted data