comparison src/server/pages.py @ 1010:4de970de87d7

pages: added getCurrentURL and getParamURL: getCurrentURL retrieve URL used to access current page. getParamURL modify current URL by using given query parameters in query.
author Goffi <goffi@goffi.org>
date Fri, 12 Jan 2018 22:04:30 +0100
parents b57f86bc1177
children 841a595bee5a
comparison
equal deleted inserted replaced
1009:b57f86bc1177 1010:4de970de87d7
366 remaining_url = '/'.join(remaining) 366 remaining_url = '/'.join(remaining)
367 return os.path.join('/', url_base, remaining_url) 367 return os.path.join('/', url_base, remaining_url)
368 368
369 return os.path.join(self.url, *url_args) 369 return os.path.join(self.url, *url_args)
370 370
371 def getCurrentURL(self, request):
372 """retrieve URL use to access this page
373
374 @return(unicode): current URL
375 """
376 # we get url in the following way (splitting request.path instead of using
377 # request.prepath) because request.prepath may have been modified by
378 # redirection (if redirection args have been specified), while path reflect
379 # the real request
380
381 # we ignore empty path elements (i.e. double '/' or '/' at the end)
382 path_elts = [p for p in request.path.split('/') if p]
383
384 if request.postpath:
385 if not request.postpath[-1]:
386 # we remove trailing slash
387 request.postpath = request.postpath[:-1]
388 if request.postpath:
389 # getSubPageURL must return subpage from the point where
390 # the it is called, so we have to remove remanining
391 # path elements
392 path_elts = path_elts[:-len(request.postpath)]
393
394 return u'/' + '/'.join(path_elts).decode('utf-8')
395
396 def getParamURL(self, request, **kwargs):
397 """use URL of current request but modify the parameters in query part
398
399 **kwargs(dict[str, unicode]): argument to use as query parameters
400 @return (unicode): constructed URL
401 """
402 current_url = self.getCurrentURL(request)
403 if kwargs:
404 encoded = urllib.urlencode({k:v.encode('utf-8') for k,v in kwargs.iteritems()}).decode('utf-8')
405 current_url = current_url + u'?' + encoded
406 return current_url
407
371 def getSubPageURL(self, request, page_name, *args): 408 def getSubPageURL(self, request, page_name, *args):
372 """retrieve a page in direct children and build its URL according to request 409 """retrieve a page in direct children and build its URL according to request
373 410
374 request's current path is used as base (at current parsing point, 411 request's current path is used as base (at current parsing point,
375 i.e. it's more prepath than path). 412 i.e. it's more prepath than path).
385 @param page_name(unicode): name of the page to retrieve 422 @param page_name(unicode): name of the page to retrieve
386 it must be a direct children of current page 423 it must be a direct children of current page
387 @param *args(list[unicode]): arguments to add as path elements 424 @param *args(list[unicode]): arguments to add as path elements
388 @return unicode: absolute URL to the sub page 425 @return unicode: absolute URL to the sub page
389 """ 426 """
390 # we get url in the following way (splitting request.path instead of using 427 current_url = self.getCurrentURL(request)
391 # request.prepath) because request.prepath may have been modified by
392 # redirection (if redirection args have been specified), while path reflect
393 # the real request
394
395 # we ignore empty path elements (i.e. double '/' or '/' at the end)
396 path_elts = [p for p in request.path.split('/') if p]
397
398 if request.postpath:
399 if not request.postpath[-1]:
400 # we remove trailing slash
401 request.postpath = request.postpath[:-1]
402 if request.postpath:
403 # getSubPageURL must return subpage from the point where
404 # the it is called, so we have to remove remanining
405 # path elements
406 path_elts = path_elts[:-len(request.postpath)]
407
408 current_url = '/' + '/'.join(path_elts).decode('utf-8')
409 428
410 for path, child in self.children.iteritems(): 429 for path, child in self.children.iteritems():
411 try: 430 try:
412 child_name = child.name 431 child_name = child.name
413 except AttributeError: 432 except AttributeError:
449 this method may perform extra operation to handle cache (e.g. subscribing to a 468 this method may perform extra operation to handle cache (e.g. subscribing to a
450 pubsub node) 469 pubsub node)
451 @param request(server.Request): current HTTP request 470 @param request(server.Request): current HTTP request
452 @param cache_type(int): on of C.CACHE_* const. 471 @param cache_type(int): on of C.CACHE_* const.
453 @param **kwargs: args according to cache_type: 472 @param **kwargs: args according to cache_type:
454 C.CACHE_PROFILE: 473 C.CACHE_PUBSUB:
455 service: pubsub service 474 service: pubsub service
456 node: pubsub node 475 node: pubsub node
457 short: short name of feature (needed if node is empty) 476 short: short name of feature (needed if node is empty)
458 477
459 """ 478 """
790 d = defer.maybeDeferred(self.on_data_post, self, request) 809 d = defer.maybeDeferred(self.on_data_post, self, request)
791 d.addCallback(self._on_data_post_redirect, request) 810 d.addCallback(self._on_data_post_redirect, request)
792 return d 811 return d
793 812
794 def getPostedData(self, request, keys, multiple=False): 813 def getPostedData(self, request, keys, multiple=False):
795 """get data from a POST request and decode it 814 """get data from a POST request or from URL's query part and decode it
796 815
797 @param request(server.Request): request linked to the session 816 @param request(server.Request): request linked to the session
798 @param keys(unicode, iterable[unicode]): name of the value(s) to get 817 @param keys(unicode, iterable[unicode]): name of the value(s) to get
799 unicode to get one value 818 unicode to get one value
800 iterable to get more than one 819 iterable to get more than one