# HG changeset patch # User Goffi # Date 1517295687 -3600 # Node ID 47c354ca66a37932ec91f7cee5d94437fed14a7a # Parent c2037b44f84e7ec1f1ac80f2055a6a650105df0b pages: new getPathArgs helper method to retrieve several path arguments at once diff -r c2037b44f84e -r 47c354ca66a3 src/server/pages.py --- a/src/server/pages.py Tue Jan 30 08:01:21 2018 +0100 +++ b/src/server/pages.py Tue Jan 30 08:01:27 2018 +0100 @@ -537,6 +537,58 @@ request.prepath.append(pathElement) return urllib.unquote(pathElement).decode('utf-8') + def getPathArgs(self, request, names, min_args=0, **kwargs): + """get several path arguments at once + + Arguments will be put in request data. + Missing arguments will have None value + @param names(list[unicode]): list of arguments to get + @param min_args(int): if less than min_args are found, PageError is used with C.HTTP_BAD_REQUEST + @param **kwargs: special value or optional callback to use for arguments + names of the arguments must correspond to those in names + special values may be: + - '': use empty string instead of None when no value is specified + - '@': if value of argument is empty or '@', empty string will be used + - 'jid': value must be converted to jid.JID if it exists, else empty string is used + - '@jid': if value of arguments is empty or '@', empty string will be used, else it will be converted to jid + """ + data = self.getRData(request) + + for idx, name in enumerate(names): + try: + value = data[name] = self.nextPath(request) + except IndexError: + if idx < min_args: + log.warning(_(u"Missing arguments in URL (got {idx}, expected at least {min_args}").format( + idx = idx, min_args = min_args)) + self.pageError(request, C.HTTP_BAD_REQUEST) + data[name] = None + break + + for name in names[idx+1:]: + data[name] = None + + for name, handler in kwargs.iteritems(): + value = data[name] + + if handler in (u'@', u'@jid') and value == u'@': + value = None + + if handler in (u'', u'@'): + if value is None: + data[name] = u'' + elif handler in (u'jid', u'@jid'): + if value: + try: + data[name] = jid.JID(value) + except RuntimeError: + log.warning(_(u'invalid jid argument: {value}').format(value=value)) + self.pageError(request, C.HTTP_BAD_REQUEST) + else: + data[name] = u'' + else: + data[name] = handler(data[self, request, name]) + ## Cache handling ## def _setCacheHeaders(self, request, cache):