database_actions() plugin hook, closes #1077

This commit is contained in:
Simon Willison 2020-11-02 10:27:25 -08:00
commit 7b19492070
10 changed files with 97 additions and 8 deletions

View file

@ -38,6 +38,7 @@ EXPECTED_PLUGINS = [
"actor_from_request",
"asgi_wrapper",
"canned_queries",
"database_actions",
"extra_body_script",
"extra_css_urls",
"extra_js_urls",

View file

@ -333,3 +333,14 @@ def table_actions(datasette, database, table, actor):
},
{"href": datasette.urls.instance(), "label": "Table: {}".format(table)},
]
@hookimpl
def database_actions(datasette, database, actor):
if actor:
return [
{
"href": datasette.urls.instance(),
"label": "Database: {}".format(database),
}
]

View file

@ -60,6 +60,7 @@ def test_homepage_sort_by_relationships(app_client):
def test_database_page(app_client):
response = app_client.get("/fixtures.json")
assert response.status == 200
data = response.json
assert "fixtures" == data["database"]
assert [

View file

@ -788,7 +788,7 @@ def test_hook_menu_links(app_client):
def test_hook_table_actions(app_client, table_or_view):
def get_table_actions_links(html):
soup = Soup(html, "html.parser")
details = soup.find("details", {"class": "table-menu-links"})
details = soup.find("details", {"class": "actions-menu-links"})
if details is None:
return []
return [{"label": a.text, "href": a["href"]} for a in details.select("a")]
@ -802,3 +802,20 @@ def test_hook_table_actions(app_client, table_or_view):
{"label": "Database: fixtures", "href": "/"},
{"label": "Table: {}".format(table_or_view), "href": "/"},
]
def test_hook_database_actions(app_client):
def get_table_actions_links(html):
soup = Soup(html, "html.parser")
details = soup.find("details", {"class": "actions-menu-links"})
if details is None:
return []
return [{"label": a.text, "href": a["href"]} for a in details.select("a")]
response = app_client.get("/fixtures")
assert get_table_actions_links(response.text) == []
response_2 = app_client.get("/fixtures?_bot=1")
assert get_table_actions_links(response_2.text) == [
{"label": "Database: fixtures", "href": "/"},
]