Skip to content

Commit

Permalink
Better implementation for #1238 using new route_path scope key
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 5, 2021
1 parent 69bf85e commit 1b27643
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
17 changes: 10 additions & 7 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,7 @@ async def route_path(self, scope, receive, send, path):
base_url = self.ds.setting("base_url")
if base_url != "/" and path.startswith(base_url):
path = "/" + path[len(base_url) :]
scope = dict(scope, route_path=path)
request = Request(scope, receive)
# Populate request_messages if ds_messages cookie is present
try:
Expand Down Expand Up @@ -1143,17 +1144,16 @@ async def handle_404(self, request, send, exception=None):
await asgi_send_redirect(send, path.decode("latin1"))
else:
# Is there a pages/* template matching this path?
template_path = (
os.path.join("pages", *request.scope["path"].split("/")) + ".html"
)
route_path = request.scope.get("route_path", request.scope["path"])
template_path = os.path.join("pages", *route_path.split("/")) + ".html"
try:
template = self.ds.jinja_env.select_template([template_path])
except TemplateNotFound:
template = None
if template is None:
# Try for a pages/blah/{name}.html template match
for regex, wildcard_template in self.page_routes:
match = regex.match(request.scope["path"])
match = regex.match(route_path)
if match is not None:
context.update(match.groupdict())
template = wildcard_template
Expand Down Expand Up @@ -1356,8 +1356,8 @@ def __init__(self, ds):
self.ds = ds
self.app = ds.app()

def _fix(self, path):
if not isinstance(path, PrefixedUrlString):
def _fix(self, path, avoid_path_rewrites=False):
if not isinstance(path, PrefixedUrlString) and not avoid_path_rewrites:
path = self.ds.urls.path(path)
if path.startswith("/"):
path = f"http://localhost{path}"
Expand Down Expand Up @@ -1392,5 +1392,8 @@ async def delete(self, path, **kwargs):
return await client.delete(self._fix(path), **kwargs)

async def request(self, method, path, **kwargs):
avoid_path_rewrites = kwargs.pop("avoid_path_rewrites", None)
async with httpx.AsyncClient(app=self.app) as client:
return await client.request(method, self._fix(path), **kwargs)
return await client.request(
method, self._fix(path, avoid_path_rewrites), **kwargs
)
1 change: 1 addition & 0 deletions datasette/utils/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ async def _request(
method,
path,
allow_redirects=allow_redirects,
avoid_path_rewrites=True,
cookies=cookies,
headers=headers,
content=post_body,
Expand Down
1 change: 1 addition & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,7 @@ def test_base_url_config(app_client_base_url_prefix, path):
and href
not in {
"https://datasette.io/",
"https://github.com/simonw/datasette",
"https://github.com/simonw/datasette/blob/main/LICENSE",
"https://github.com/simonw/datasette/blob/main/tests/fixtures.py",
"/login-as-root", # Only used for the latest.datasette.io demo
Expand Down

0 comments on commit 1b27643

Please sign in to comment.