diff 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
line wrap: on
line diff
--- a/src/server/pages.py	Fri Jan 12 22:00:28 2018 +0100
+++ b/src/server/pages.py	Fri Jan 12 22:04:30 2018 +0100
@@ -368,6 +368,43 @@
 
         return os.path.join(self.url, *url_args)
 
+    def getCurrentURL(self, request):
+        """retrieve URL use to access this page
+
+        @return(unicode): current URL
+        """
+        # we get url in the following way (splitting request.path instead of using
+        # request.prepath) because request.prepath may have been modified by
+        # redirection (if redirection args have been specified), while path reflect
+        # the real request
+
+        # we ignore empty path elements (i.e. double '/' or '/' at the end)
+        path_elts = [p for p in request.path.split('/') if p]
+
+        if request.postpath:
+            if not request.postpath[-1]:
+                # we remove trailing slash
+                request.postpath = request.postpath[:-1]
+            if request.postpath:
+                # getSubPageURL must return subpage from the point where
+                # the it is called, so we have to remove remanining
+                # path elements
+                path_elts = path_elts[:-len(request.postpath)]
+
+        return u'/' + '/'.join(path_elts).decode('utf-8')
+
+    def getParamURL(self, request, **kwargs):
+        """use URL of current request but modify the parameters in query part
+
+        **kwargs(dict[str, unicode]): argument to use as query parameters
+        @return (unicode): constructed URL
+        """
+        current_url = self.getCurrentURL(request)
+        if kwargs:
+            encoded = urllib.urlencode({k:v.encode('utf-8') for k,v in kwargs.iteritems()}).decode('utf-8')
+            current_url = current_url + u'?' + encoded
+        return current_url
+
     def getSubPageURL(self, request, page_name, *args):
         """retrieve a page in direct children and build its URL according to request
 
@@ -387,25 +424,7 @@
         @param *args(list[unicode]): arguments to add as path elements
         @return unicode: absolute URL to the sub page
         """
-        # we get url in the following way (splitting request.path instead of using
-        # request.prepath) because request.prepath may have been modified by
-        # redirection (if redirection args have been specified), while path reflect
-        # the real request
-
-        # we ignore empty path elements (i.e. double '/' or '/' at the end)
-        path_elts = [p for p in request.path.split('/') if p]
-
-        if request.postpath:
-            if not request.postpath[-1]:
-                # we remove trailing slash
-                request.postpath = request.postpath[:-1]
-            if request.postpath:
-                # getSubPageURL must return subpage from the point where
-                # the it is called, so we have to remove remanining
-                # path elements
-                path_elts = path_elts[:-len(request.postpath)]
-
-        current_url = '/' + '/'.join(path_elts).decode('utf-8')
+        current_url = self.getCurrentURL(request)
 
         for path, child in self.children.iteritems():
             try:
@@ -451,7 +470,7 @@
         @param request(server.Request): current HTTP request
         @param cache_type(int): on of C.CACHE_* const.
         @param **kwargs: args according to cache_type:
-            C.CACHE_PROFILE:
+            C.CACHE_PUBSUB:
                 service: pubsub service
                 node: pubsub node
                 short: short name of feature (needed if node is empty)
@@ -792,7 +811,7 @@
         return d
 
     def getPostedData(self, request, keys, multiple=False):
-        """get data from a POST request and decode it
+        """get data from a POST request or from URL's query part and decode it
 
         @param request(server.Request): request linked to the session
         @param keys(unicode, iterable[unicode]): name of the value(s) to get