From c5e8cd84d3ef55ed86771ac0bde0ca91d6b0e07a Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 8 Dec 2019 16:14:05 -0800 Subject: [PATCH 1/2] Render templates/pages/x.html on 404 to /x - refs #648 --- datasette/app.py | 14 +++++++++++++- datasette/utils/asgi.py | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/datasette/app.py b/datasette/app.py index 5c1c3e83..3a2aefee 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -13,6 +13,7 @@ from pathlib import Path import click from markupsafe import Markup from jinja2 import ChoiceLoader, Environment, FileSystemLoader, PrefixLoader +from jinja2.exceptions import TemplateNotFound import uvicorn from .views.base import DatasetteError, ureg, AsgiRouter @@ -658,7 +659,18 @@ class DatasetteRouter(AsgiRouter): path += b"?" + scope["query_string"] await asgi_send_redirect(send, path.decode("latin1")) else: - await super().handle_404(scope, receive, send) + # Is there a pages/* template matching this path? + template_path = os.path.join("pages", *scope["path"].split("/")) + ".html" + try: + template = self.ds.jinja_env.select_template([template_path]) + except TemplateNotFound: + template = None + if template: + await asgi_send_html( + send, await template.render_async(), status=200 + ) + else: + await super().handle_404(scope, receive, send) async def handle_500(self, scope, receive, send, exception): title = None diff --git a/datasette/utils/asgi.py b/datasette/utils/asgi.py index bafcfb4a..634a596f 100644 --- a/datasette/utils/asgi.py +++ b/datasette/utils/asgi.py @@ -99,6 +99,8 @@ class AsgiRouter: new_scope = dict(scope, url_route={"kwargs": match.groupdict()}) try: return await view(new_scope, receive, send) + except NotFound: + return await self.handle_404(scope, receive, send) except Exception as exception: return await self.handle_500(scope, receive, send, exception) return await self.handle_404(scope, receive, send) From 1d9efeef740b778ce5ec6093e416350c7521195d Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 8 Dec 2019 16:22:09 -0800 Subject: [PATCH 2/2] Apply black --- datasette/app.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index 3a2aefee..75c96db4 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -489,7 +489,10 @@ class Datasette: if hasattr(asyncio, "all_tasks"): tasks = asyncio.all_tasks() d.update( - {"num_tasks": len(tasks), "tasks": [_cleaner_task_str(t) for t in tasks]} + { + "num_tasks": len(tasks), + "tasks": [_cleaner_task_str(t) for t in tasks], + } ) return d @@ -666,9 +669,7 @@ class DatasetteRouter(AsgiRouter): except TemplateNotFound: template = None if template: - await asgi_send_html( - send, await template.render_async(), status=200 - ) + await asgi_send_html(send, await template.render_async(), status=200) else: await super().handle_404(scope, receive, send)