Navigation menu plus menu_links() hook

Closes #1064, refs #690.
This commit is contained in:
Simon Willison 2020-10-29 20:45:15 -07:00 committed by GitHub
commit 18a64fbb29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 193 additions and 13 deletions

View file

@ -43,6 +43,7 @@ EXPECTED_PLUGINS = [
"extra_js_urls",
"extra_template_vars",
"forbidden",
"menu_links",
"permission_allowed",
"prepare_connection",
"prepare_jinja2_environment",
@ -64,6 +65,7 @@ EXPECTED_PLUGINS = [
"canned_queries",
"extra_js_urls",
"extra_template_vars",
"menu_links",
"permission_allowed",
"render_cell",
"startup",

View file

@ -290,3 +290,9 @@ def forbidden(datasette, request, message):
datasette._last_forbidden_message = message
if request.path == "/data2":
return Response.redirect("/login?message=" + message)
@hookimpl
def menu_links(datasette, actor):
if actor:
return [{"href": datasette.urls.instance(), "label": "Hello"}]

View file

@ -146,3 +146,12 @@ def canned_queries(datasette, database):
}
return inner
@hookimpl(trylast=True)
def menu_links(datasette, actor):
async def inner():
if actor:
return [{"href": datasette.urls.instance(), "label": "Hello 2"}]
return inner

View file

@ -101,7 +101,7 @@ def test_logout_button_in_navigation(app_client, path):
)
anon_response = app_client.get(path)
for fragment in (
"<strong>test</strong> &middot;",
"<strong>test</strong>",
'<form action="/-/logout" method="post">',
):
assert fragment in response.text
@ -112,5 +112,4 @@ def test_logout_button_in_navigation(app_client, path):
def test_no_logout_button_in_navigation_if_no_ds_actor_cookie(app_client, path):
response = app_client.get(path + "?_bot=1")
assert "<strong>bot</strong>" in response.text
assert "<strong>bot</strong> &middot;" not in response.text
assert '<form action="/-/logout" method="post">' not in response.text

View file

@ -765,3 +765,20 @@ def test_hook_forbidden(restore_working_directory):
assert 302 == response2.status
assert "/login?message=view-database" == response2.headers["Location"]
assert "view-database" == client.ds._last_forbidden_message
def test_hook_menu_links(app_client):
def get_menu_links(html):
soup = Soup(html, "html.parser")
return [
{"label": a.text, "href": a["href"]} for a in soup.find("nav").select("a")
]
response = app_client.get("/")
assert get_menu_links(response.text) == []
response_2 = app_client.get("/?_bot=1")
assert get_menu_links(response_2.text) == [
{"label": "Hello", "href": "/"},
{"label": "Hello 2", "href": "/"},
]