comparison piecrust/serving/util.py @ 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 84ce51430346
comparison
equal deleted inserted replaced
862:fddaf43424e2 863:01458d3b646b
28 self.sub_num = 1 28 self.sub_num = 1
29 self.req_path = None 29 self.req_path = None
30 self.not_found_errors = [] 30 self.not_found_errors = []
31 31
32 32
33 def find_routes(routes, uri, sub_num=1): 33 def find_routes(routes, uri, uri_no_sub, sub_num=1):
34 """ Returns routes matching the given URL. 34 """ Returns routes matching the given URL.
35 """ 35 """
36 res = [] 36 res = []
37 for route in routes: 37 for route in routes:
38 route_params = route.matchUri(uri) 38 route_params = route.matchUri(uri)
39 if route_params is not None: 39 if route_params is not None:
40 res.append((route, route_params, sub_num)) 40 res.append((route, route_params, 1))
41
42 if sub_num > 1:
43 route_params = route.matchUri(uri_no_sub)
44 if route_params is not None:
45 res.append((route, route_params, sub_num))
41 return res 46 return res
42 47
43 48
44 def get_requested_page(app, req_path): 49 def get_requested_page(app, req_path):
45 # Remove the trailing slash to simplify how we parse URLs. 50 # Remove the trailing slash to simplify how we parse URLs.
46 root_url = app.config.get('site/root') 51 root_url = app.config.get('site/root')
47 if req_path != root_url: 52 if req_path != root_url:
48 req_path = req_path.rstrip('/') 53 req_path = req_path.rstrip('/')
49 54
50 # Try to find what matches the requested URL. 55 # Try to find what matches the requested URL.
51 routes = find_routes(app.routes, req_path)
52
53 # It could also be a sub-page (i.e. the URL ends with a page number), so 56 # It could also be a sub-page (i.e. the URL ends with a page number), so
54 # we try to also match the base URL (without the number). 57 # we try to also match the base URL (without the number).
55 req_path_no_num, page_num = split_sub_uri(app, req_path) 58 req_path_no_sub, sub_num = split_sub_uri(app, req_path)
56 if page_num > 1: 59 routes = find_routes(app.routes, req_path, req_path_no_sub, sub_num)
57 routes += find_routes(app.routes, req_path_no_num, page_num)
58
59 if len(routes) == 0: 60 if len(routes) == 0:
60 raise RouteNotFoundError("Can't find route for: %s" % req_path) 61 raise RouteNotFoundError("Can't find route for: %s" % req_path)
61 62
62 req_page = RequestedPage() 63 req_page = RequestedPage()
63 for route, route_params, route_sub_num in routes: 64 for route, route_params, route_sub_num in routes:
64 cur_req_path = req_path 65 cur_req_path = req_path
65 if route_sub_num > 1: 66 if route_sub_num > 1:
66 cur_req_path = req_path_no_num 67 cur_req_path = req_path_no_sub
67 68
68 page = _get_requested_page_for_route(app, route, route_params) 69 page = _get_requested_page_for_route(app, route, route_params)
69 if page is not None: 70 if page is not None:
70 req_page.page = page 71 req_page.page = page
71 req_page.sub_num = route_sub_num 72 req_page.sub_num = route_sub_num