Skip to content

Commit

Permalink
Made show_messages available to plugins, closes #864
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 29, 2020
1 parent 7ac4936 commit a8a5f81
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ async def render_template(
"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"),
Expand Down
1 change: 0 additions & 1 deletion datasette/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ async def render(self, templates, request, context=None):
**{
"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
Expand Down
8 changes: 6 additions & 2 deletions tests/plugins/my_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,13 @@ def not_async():

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<name>.*)$", two),
Expand All @@ -204,6 +207,7 @@ def add_message(datasette, request):
(r"/csrftoken-form/$", csrftoken_form),
(r"/not-async/$", not_async),
(r"/add-message/$", add_message),
(r"/render-message/$", render_message),
]


Expand Down
10 changes: 10 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a8a5f81

Please sign in to comment.