comparison src/server/pages.py @ 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
comparison
equal deleted inserted replaced
1059:d127a85b2fee 1060:f0f7b3324749
531 """ 531 """
532 pathElement = request.postpath.pop(0) 532 pathElement = request.postpath.pop(0)
533 request.prepath.append(pathElement) 533 request.prepath.append(pathElement)
534 return urllib.unquote(pathElement).decode('utf-8') 534 return urllib.unquote(pathElement).decode('utf-8')
535 535
536 def _filterPathValue(self, value, handler, name, request):
537 """Modify a path value according to handler (see [getPathArgs])"""
538 if handler in (u'@', u'@jid') and value == u'@':
539 value = None
540
541 if handler in (u'', u'@'):
542 if value is None:
543 return u''
544 elif handler in (u'jid', u'@jid'):
545 if value:
546 try:
547 return jid.JID(value)
548 except RuntimeError:
549 log.warning(_(u'invalid jid argument: {value}').format(value=value))
550 self.pageError(request, C.HTTP_BAD_REQUEST)
551 else:
552 return u''
553 else:
554 return handler(self, value, name, request)
555
556 return value
557
536 def getPathArgs(self, request, names, min_args=0, **kwargs): 558 def getPathArgs(self, request, names, min_args=0, **kwargs):
537 """get several path arguments at once 559 """get several path arguments at once
538 560
539 Arguments will be put in request data. 561 Arguments will be put in request data.
540 Missing arguments will have None value 562 Missing arguments will have None value
549 - '@jid': if value of arguments is empty or '@', empty string will be used, else it will be converted to jid 571 - '@jid': if value of arguments is empty or '@', empty string will be used, else it will be converted to jid
550 """ 572 """
551 data = self.getRData(request) 573 data = self.getRData(request)
552 574
553 for idx, name in enumerate(names): 575 for idx, name in enumerate(names):
554 try: 576 if name[0] == u'*':
555 value = data[name] = self.nextPath(request) 577 value = data[name[1:]] = []
556 except IndexError: 578 while True:
557 if idx < min_args: 579 try:
558 log.warning(_(u"Missing arguments in URL (got {idx}, expected at least {min_args}").format( 580 value.append(self.nextPath(request))
559 idx = idx, min_args = min_args)) 581 except IndexError:
560 self.pageError(request, C.HTTP_BAD_REQUEST) 582 break
561 data[name] = None 583 else:
562 break 584 idx+=1
585 else:
586 try:
587 value = data[name] = self.nextPath(request)
588 except IndexError:
589 data[name] = None
590 break
591
592 if idx < min_args:
593 log.warning(_(u"Missing arguments in URL (got {idx}, expected at least {min_args})").format(
594 idx = idx, min_args = min_args))
595 self.pageError(request, C.HTTP_BAD_REQUEST)
563 596
564 for name in names[idx+1:]: 597 for name in names[idx+1:]:
565 data[name] = None 598 data[name] = None
566 599
567 for name, handler in kwargs.iteritems(): 600 for name, handler in kwargs.iteritems():
568 value = data[name] 601 if name[0] == '*':
569 602 data[name] = [self._filterPathValue(v, handler, name, request) for v in data[name]]
570 if handler in (u'@', u'@jid') and value == u'@': 603 else:
571 value = None 604 data[name] = self._filterPathValue(data[name], handler, name, request)
572 605
573 if handler in (u'', u'@'):
574 if value is None:
575 data[name] = u''
576 elif handler in (u'jid', u'@jid'):
577 if value:
578 try:
579 data[name] = jid.JID(value)
580 except RuntimeError:
581 log.warning(_(u'invalid jid argument: {value}').format(value=value))
582 self.pageError(request, C.HTTP_BAD_REQUEST)
583 else:
584 data[name] = u''
585 else:
586 data[name] = handler(data[self, request, name])
587 606
588 ## Cache handling ## 607 ## Cache handling ##
589 608
590 def _setCacheHeaders(self, request, cache): 609 def _setCacheHeaders(self, request, cache):
591 """Set ETag and Last-Modified HTTP headers, used for caching""" 610 """Set ETag and Last-Modified HTTP headers, used for caching"""