changeset 1060:f0f7b3324749

pages: '*' can now be used to prefix the last arg name in getPathArgs, in which case the data will be a list of remaining path arguments.
author Goffi <goffi@goffi.org>
date Sun, 11 Mar 2018 19:26:18 +0100
parents d127a85b2fee
children bacb8f229742
files src/server/pages.py
diffstat 1 files changed, 46 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/server/pages.py	Tue Jan 30 08:15:05 2018 +0100
+++ b/src/server/pages.py	Sun Mar 11 19:26:18 2018 +0100
@@ -533,6 +533,28 @@
         request.prepath.append(pathElement)
         return urllib.unquote(pathElement).decode('utf-8')
 
+    def _filterPathValue(self, value, handler, name, request):
+        """Modify a path value according to handler (see [getPathArgs])"""
+        if handler in (u'@', u'@jid') and value == u'@':
+            value = None
+
+        if handler in (u'', u'@'):
+            if value is None:
+                return u''
+        elif handler in (u'jid', u'@jid'):
+            if value:
+                try:
+                    return jid.JID(value)
+                except RuntimeError:
+                    log.warning(_(u'invalid jid argument: {value}').format(value=value))
+                    self.pageError(request, C.HTTP_BAD_REQUEST)
+            else:
+                return u''
+        else:
+            return handler(self, value, name, request)
+
+        return value
+
     def getPathArgs(self, request, names, min_args=0, **kwargs):
         """get several path arguments at once
 
@@ -551,39 +573,36 @@
         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
+            if name[0] == u'*':
+                value = data[name[1:]] = []
+                while True:
+                    try:
+                        value.append(self.nextPath(request))
+                    except IndexError:
+                        break
+                    else:
+                        idx+=1
+            else:
+                try:
+                    value = data[name] = self.nextPath(request)
+                except IndexError:
+                    data[name] = None
+                    break
+
+        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)
 
         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 name[0] == '*':
+                data[name] = [self._filterPathValue(v, handler, name, request) for v in data[name]]
+            else:
+                data[name] = self._filterPathValue(data[name], handler, name, request)
 
-            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 ##