From a8a5f813722f72703a7aae41135ccc40635cc02f Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 28 Jun 2020 17:50:47 -0700 Subject: [PATCH] Made show_messages available to plugins, closes #864 --- datasette/app.py | 1 + datasette/views/base.py | 1 - tests/plugins/my_plugin.py | 8 ++++++-- tests/test_plugins.py | 10 ++++++++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index af3dcc8b..90abc373 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -739,6 +739,7 @@ class Datasette: "zip": zip, "body_scripts": body_scripts, "format_bytes": format_bytes, + "show_messages": lambda: self._show_messages(request), "extra_css_urls": self._asset_urls("extra_css_urls", template, context), "extra_js_urls": self._asset_urls("extra_js_urls", template, context), "base_url": self.config("base_url"), diff --git a/datasette/views/base.py b/datasette/views/base.py index 208c3c96..6346a3f5 100644 --- a/datasette/views/base.py +++ b/datasette/views/base.py @@ -92,7 +92,6 @@ class BaseView: **{ "database_url": self.database_url, "database_color": self.database_color, - "show_messages": lambda: self.ds._show_messages(request), "select_templates": [ "{}{}".format( "*" if template_name == template.name else "", template_name diff --git a/tests/plugins/my_plugin.py b/tests/plugins/my_plugin.py index bf6340ce..8701c6db 100644 --- a/tests/plugins/my_plugin.py +++ b/tests/plugins/my_plugin.py @@ -192,10 +192,13 @@ def register_routes(): def add_message(datasette, request): datasette.add_message(request, "Hello from messages") - print("Adding message") - print(request._messages) return Response.html("Added message") + async def render_message(datasette, request): + return Response.html( + await datasette.render_template("render_message.html", request=request) + ) + return [ (r"/one/$", one), (r"/two/(?P.*)$", two), @@ -204,6 +207,7 @@ def register_routes(): (r"/csrftoken-form/$", csrftoken_form), (r"/not-async/$", not_async), (r"/add-message/$", add_message), + (r"/render-message/$", render_message), ] diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 9468fde9..9a2ee2a3 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -610,6 +610,16 @@ def test_register_routes_add_message(app_client): assert [["Hello from messages", 1]] == decoded +def test_register_routes_render_message(restore_working_directory, tmpdir_factory): + templates = tmpdir_factory.mktemp("templates") + (templates / "render_message.html").write_text('{% extends "base.html" %}', "utf-8") + with make_app_client(template_dir=templates) as client: + response1 = client.get("/add-message/") + response2 = client.get("/render-message/", cookies=response1.cookies) + assert 200 == response2.status + assert "Hello from messages" in response2.text + + @pytest.mark.asyncio async def test_startup(app_client): await app_client.ds.invoke_startup()