Mercurial > piecrust2
comparison piecrust/serving/util.py @ 1145:e94737572542
serve: Fix an issue where false positive matches were rendered as the requested page.
Now we try to render the page, but also try to detect for the most common "empty" pages.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 05 Jun 2018 22:08:51 -0700 |
parents | c8518f5cedbb |
children |
comparison
equal
deleted
inserted
replaced
1144:9f3e702a8a69 | 1145:e94737572542 |
---|---|
37 uri_no_sub = None | 37 uri_no_sub = None |
38 if decomposed_uri is not None: | 38 if decomposed_uri is not None: |
39 uri_no_sub, sub_num = decomposed_uri | 39 uri_no_sub, sub_num = decomposed_uri |
40 | 40 |
41 res = [] | 41 res = [] |
42 | |
42 for route in routes: | 43 for route in routes: |
43 route_params = route.matchUri(uri) | 44 route_params = route.matchUri(uri) |
44 if route_params is not None: | 45 if route_params is not None: |
45 res.append((route, route_params, 1)) | 46 res.append((route, route_params, 1)) |
46 | 47 |
47 if sub_num > 1: | 48 if sub_num > 1: |
49 for route in routes: | |
48 route_params = route.matchUri(uri_no_sub) | 50 route_params = route.matchUri(uri_no_sub) |
49 if route_params is not None: | 51 if route_params is not None: |
50 res.append((route, route_params, sub_num)) | 52 res.append((route, route_params, sub_num)) |
53 | |
51 return res | 54 return res |
52 | 55 |
53 | 56 |
54 def get_requested_page(app, req_path): | 57 def get_requested_pages(app, req_path): |
55 # Remove the trailing slash to simplify how we parse URLs. | 58 # Remove the trailing slash to simplify how we parse URLs. |
56 root_url = app.config.get('site/root') | 59 root_url = app.config.get('site/root') |
57 if req_path != root_url: | 60 if req_path != root_url: |
58 req_path = req_path.rstrip('/') | 61 req_path = req_path.rstrip('/') |
59 | 62 |
63 req_path_no_sub, sub_num = split_sub_uri(app, req_path) | 66 req_path_no_sub, sub_num = split_sub_uri(app, req_path) |
64 routes = find_routes(app.routes, req_path, (req_path_no_sub, sub_num)) | 67 routes = find_routes(app.routes, req_path, (req_path_no_sub, sub_num)) |
65 if len(routes) == 0: | 68 if len(routes) == 0: |
66 raise RouteNotFoundError("Can't find route for: %s" % req_path) | 69 raise RouteNotFoundError("Can't find route for: %s" % req_path) |
67 | 70 |
68 req_page = RequestedPage() | 71 req_pages = [] |
72 not_founds = [] | |
73 | |
69 for route, route_params, route_sub_num in routes: | 74 for route, route_params, route_sub_num in routes: |
70 cur_req_path = req_path | 75 cur_req_path = req_path |
71 if route_sub_num > 1: | 76 if route_sub_num > 1: |
72 cur_req_path = req_path_no_sub | 77 cur_req_path = req_path_no_sub |
73 | 78 |
74 page = _get_requested_page_for_route(app, route, route_params) | 79 page = _get_requested_page_for_route(app, route, route_params) |
75 if page is not None: | 80 if page is not None: |
81 req_page = RequestedPage() | |
76 req_page.page = page | 82 req_page.page = page |
77 req_page.sub_num = route_sub_num | 83 req_page.sub_num = route_sub_num |
78 req_page.req_path = cur_req_path | 84 req_page.req_path = cur_req_path |
79 break | 85 req_pages.append(req_page) |
86 else: | |
87 not_founds.append(PageNotFoundError( | |
88 "No path found for '%s' in source '%s'." % | |
89 (cur_req_path, route.source_name))) | |
80 | 90 |
81 req_page.not_found_errors.append(PageNotFoundError( | 91 return req_pages, not_founds |
82 "No path found for '%s' in source '%s'." % | |
83 (cur_req_path, route.source_name))) | |
84 | 92 |
85 return req_page | 93 |
94 def get_requested_page(app, req_path): | |
95 req_pages, not_founds = get_requested_pages(app, req_path) | |
96 if req_pages: | |
97 return req_pages[0] | |
98 | |
99 nfrp = RequestedPage() | |
100 nfrp.not_found_errors = not_founds | |
101 return nfrp | |
86 | 102 |
87 | 103 |
88 def _get_requested_page_for_route(app, route, route_params): | 104 def _get_requested_page_for_route(app, route, route_params): |
89 source = app.getSource(route.source_name) | 105 source = app.getSource(route.source_name) |
90 item = source.findContentFromRoute(route_params) | 106 item = source.findContentFromRoute(route_params) |