changeset 1030:66a050b32df8

pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
author Goffi <goffi@goffi.org>
date Mon, 22 Jan 2018 22:16:07 +0100
parents 78b7b5ec7ca1
children 4ba7df23b976
files src/server/pages.py
diffstat 1 files changed, 21 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/server/pages.py	Mon Jan 22 08:53:06 2018 +0100
+++ b/src/server/pages.py	Mon Jan 22 22:16:07 2018 +0100
@@ -447,6 +447,25 @@
             current_url = current_url + u'?' + encoded
         return current_url
 
+    def getSubPageByName(self, page, subpage_name):
+        """retrieve a subpage and its path using its name
+
+        @param request(server.Request): current HTTP request
+        @param page_name(unicode): name of the page to retrieve
+            it must be a direct children of current page
+        @return (tuple[str, LiberviaPage]): page subpath and instance
+        @raise exceptions.NotFound: no page has been found
+        """
+        for path, child in page.children.iteritems():
+            try:
+                child_name = child.name
+            except AttributeError:
+                # LiberviaPages have a name, but maybe this is an other Resource
+                continue
+            if child_name == subpage_name:
+                return path, child
+        raise exceptions.NotFound(_(u'requested sub page has not been found'))
+
     def getSubPageURL(self, request, page_name, *args):
         """retrieve a page in direct children and build its URL according to request
 
@@ -467,16 +486,9 @@
         @return unicode: absolute URL to the sub page
         """
         current_url = self.getCurrentURL(request)
+        path, child = self.getSubPageByName(self, page_name)
+        return os.path.join(u'/', current_url, path, *[quote(a) for a in args])
 
-        for path, child in self.children.iteritems():
-            try:
-                child_name = child.name
-            except AttributeError:
-                # LiberviaPage have a name, but maybe this is an other Resource
-                continue
-            if child_name == page_name:
-                return os.path.join(u'/', current_url, path, *args)
-        raise exceptions.NotFound(_(u'requested sub page has not been found'))
 
     def getChildWithDefault(self, path, request):
         # we handle children ourselves