changeset 1056:47c354ca66a3

pages: new getPathArgs helper method to retrieve several path arguments at once
author Goffi <goffi@goffi.org>
date Tue, 30 Jan 2018 08:01:27 +0100
parents c2037b44f84e
children 50ba8947a6e8
files src/server/pages.py
diffstat 1 files changed, 52 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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):