Make request available to menu plugin hooks, closes #1371

This commit is contained in:
Simon Willison 2021-06-09 21:45:24 -07:00
commit d23a267138
8 changed files with 44 additions and 23 deletions

View file

@ -316,9 +316,12 @@ def forbidden(datasette, request, message):
@hookimpl
def menu_links(datasette, actor):
def menu_links(datasette, actor, request):
if actor:
return [{"href": datasette.urls.instance(), "label": "Hello"}]
label = "Hello"
if request.args.get("_hello"):
label += ", " + request.args["_hello"]
return [{"href": datasette.urls.instance(), "label": label}]
@hookimpl
@ -334,11 +337,14 @@ def table_actions(datasette, database, table, actor):
@hookimpl
def database_actions(datasette, database, actor):
def database_actions(datasette, database, actor, request):
if actor:
label = f"Database: {database}"
if request.args.get("_hello"):
label += " - " + request.args["_hello"]
return [
{
"href": datasette.urls.instance(),
"label": f"Database: {database}",
"label": label,
}
]

View file

@ -158,9 +158,12 @@ def menu_links(datasette, actor):
@hookimpl
def table_actions(datasette, database, table, actor):
def table_actions(datasette, database, table, actor, request):
async def inner():
if actor:
return [{"href": datasette.urls.instance(), "label": "From async"}]
label = "From async"
if request.args.get("_hello"):
label += " " + request.args["_hello"]
return [{"href": datasette.urls.instance(), "label": label}]
return inner

View file

@ -781,9 +781,9 @@ def test_hook_menu_links(app_client):
response = app_client.get("/")
assert get_menu_links(response.text) == []
response_2 = app_client.get("/?_bot=1")
response_2 = app_client.get("/?_bot=1&_hello=BOB")
assert get_menu_links(response_2.text) == [
{"label": "Hello", "href": "/"},
{"label": "Hello, BOB", "href": "/"},
{"label": "Hello 2", "href": "/"},
]
@ -800,12 +800,12 @@ def test_hook_table_actions(app_client, table_or_view):
response = app_client.get(f"/fixtures/{table_or_view}")
assert get_table_actions_links(response.text) == []
response_2 = app_client.get(f"/fixtures/{table_or_view}?_bot=1")
response_2 = app_client.get(f"/fixtures/{table_or_view}?_bot=1&_hello=BOB")
assert sorted(
get_table_actions_links(response_2.text), key=lambda l: l["label"]
) == [
{"label": "Database: fixtures", "href": "/"},
{"label": "From async", "href": "/"},
{"label": "From async BOB", "href": "/"},
{"label": f"Table: {table_or_view}", "href": "/"},
]
@ -821,7 +821,7 @@ def test_hook_database_actions(app_client):
response = app_client.get("/fixtures")
assert get_table_actions_links(response.text) == []
response_2 = app_client.get("/fixtures?_bot=1")
response_2 = app_client.get("/fixtures?_bot=1&_hello=BOB")
assert get_table_actions_links(response_2.text) == [
{"label": "Database: fixtures", "href": "/"},
{"label": "Database: fixtures - BOB", "href": "/"},
]