changeset 863:01458d3b646b

routing: Properly order routes by pass when matching them to the request.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 08 Jun 2017 23:30:42 -0700
parents fddaf43424e2
children 5c5b85d4f17d
files piecrust/serving/util.py
diffstat 1 files changed, 10 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/serving/util.py	Thu Jun 08 23:09:34 2017 -0700
+++ b/piecrust/serving/util.py	Thu Jun 08 23:30:42 2017 -0700
@@ -30,14 +30,19 @@
         self.not_found_errors = []
 
 
-def find_routes(routes, uri, sub_num=1):
+def find_routes(routes, uri, uri_no_sub, sub_num=1):
     """ Returns routes matching the given URL.
     """
     res = []
     for route in routes:
         route_params = route.matchUri(uri)
         if route_params is not None:
-            res.append((route, route_params, sub_num))
+            res.append((route, route_params, 1))
+
+        if sub_num > 1:
+            route_params = route.matchUri(uri_no_sub)
+            if route_params is not None:
+                res.append((route, route_params, sub_num))
     return res
 
 
@@ -48,14 +53,10 @@
         req_path = req_path.rstrip('/')
 
     # Try to find what matches the requested URL.
-    routes = find_routes(app.routes, req_path)
-
     # It could also be a sub-page (i.e. the URL ends with a page number), so
     # we try to also match the base URL (without the number).
-    req_path_no_num, page_num = split_sub_uri(app, req_path)
-    if page_num > 1:
-        routes += find_routes(app.routes, req_path_no_num, page_num)
-
+    req_path_no_sub, sub_num = split_sub_uri(app, req_path)
+    routes = find_routes(app.routes, req_path, req_path_no_sub, sub_num)
     if len(routes) == 0:
         raise RouteNotFoundError("Can't find route for: %s" % req_path)
 
@@ -63,7 +64,7 @@
     for route, route_params, route_sub_num in routes:
         cur_req_path = req_path
         if route_sub_num > 1:
-            cur_req_path = req_path_no_num
+            cur_req_path = req_path_no_sub
 
         page = _get_requested_page_for_route(app, route, route_params)
         if page is not None: