changeset 1031:4ba7df23b976

pages: new method getURLByNames to retrieve URL from list of page names/path arguments
author Goffi <goffi@goffi.org>
date Mon, 22 Jan 2018 22:17:20 +0100
parents 66a050b32df8
children 863cc6f97068
files src/server/pages.py
diffstat 1 files changed, 22 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/server/pages.py	Mon Jan 22 22:16:07 2018 +0100
+++ b/src/server/pages.py	Mon Jan 22 22:17:20 2018 +0100
@@ -489,6 +489,28 @@
         path, child = self.getSubPageByName(self, page_name)
         return os.path.join(u'/', current_url, path, *[quote(a) for a in args])
 
+    def getURLByNames(self, named_path):
+        """retrieve URL from pages names and arguments
+
+        @param named_path(list[tuple[unicode, list[unicode]]]): path to the page as a list
+            of tuples of 2 items:
+                - first item is page name
+                - second item is list of path arguments of this page
+        @return (unicode): URL to the requested page with given path arguments
+        @raise exceptions.NotFound: one of the page was not found
+        """
+        current_page = None
+        path = []
+        for page_name, page_args in named_path:
+            if current_page is None:
+                current_page = self.getPageByName(page_name)
+                path.append(current_page.getURL(*page_args))
+            else:
+                sub_path, current_page = self.getSubPageByName(current_page, page_name)
+                path.append(sub_path)
+                if page_args:
+                    path.extend([quote(a) for a in page_args])
+        return u'/'.join(path)
 
     def getChildWithDefault(self, path, request):
         # we handle children ourselves