comparison src/server/pages.py @ 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 f2170536ba23
children d127a85b2fee
comparison
equal deleted inserted replaced
1055:c2037b44f84e 1056:47c354ca66a3
535 """ 535 """
536 pathElement = request.postpath.pop(0) 536 pathElement = request.postpath.pop(0)
537 request.prepath.append(pathElement) 537 request.prepath.append(pathElement)
538 return urllib.unquote(pathElement).decode('utf-8') 538 return urllib.unquote(pathElement).decode('utf-8')
539 539
540 def getPathArgs(self, request, names, min_args=0, **kwargs):
541 """get several path arguments at once
542
543 Arguments will be put in request data.
544 Missing arguments will have None value
545 @param names(list[unicode]): list of arguments to get
546 @param min_args(int): if less than min_args are found, PageError is used with C.HTTP_BAD_REQUEST
547 @param **kwargs: special value or optional callback to use for arguments
548 names of the arguments must correspond to those in names
549 special values may be:
550 - '': use empty string instead of None when no value is specified
551 - '@': if value of argument is empty or '@', empty string will be used
552 - 'jid': value must be converted to jid.JID if it exists, else empty string is used
553 - '@jid': if value of arguments is empty or '@', empty string will be used, else it will be converted to jid
554 """
555 data = self.getRData(request)
556
557 for idx, name in enumerate(names):
558 try:
559 value = data[name] = self.nextPath(request)
560 except IndexError:
561 if idx < min_args:
562 log.warning(_(u"Missing arguments in URL (got {idx}, expected at least {min_args}").format(
563 idx = idx, min_args = min_args))
564 self.pageError(request, C.HTTP_BAD_REQUEST)
565 data[name] = None
566 break
567
568 for name in names[idx+1:]:
569 data[name] = None
570
571 for name, handler in kwargs.iteritems():
572 value = data[name]
573
574 if handler in (u'@', u'@jid') and value == u'@':
575 value = None
576
577 if handler in (u'', u'@'):
578 if value is None:
579 data[name] = u''
580 elif handler in (u'jid', u'@jid'):
581 if value:
582 try:
583 data[name] = jid.JID(value)
584 except RuntimeError:
585 log.warning(_(u'invalid jid argument: {value}').format(value=value))
586 self.pageError(request, C.HTTP_BAD_REQUEST)
587 else:
588 data[name] = u''
589 else:
590 data[name] = handler(data[self, request, name])
591
540 ## Cache handling ## 592 ## Cache handling ##
541 593
542 def _setCacheHeaders(self, request, cache): 594 def _setCacheHeaders(self, request, cache):
543 """Set ETag and Last-Modified HTTP headers, used for caching""" 595 """Set ETag and Last-Modified HTTP headers, used for caching"""
544 request.setHeader('ETag', cache.hash) 596 request.setHeader('ETag', cache.hash)