table_actions() plugin hook plus menu, closes #1066

Refs #690
This commit is contained in:
Simon Willison 2020-10-29 22:16:41 -07:00
commit 2f7731e9e5
10 changed files with 166 additions and 14 deletions

View file

@ -52,6 +52,7 @@ EXPECTED_PLUGINS = [
"register_routes",
"render_cell",
"startup",
"table_actions",
],
},
{
@ -69,6 +70,7 @@ EXPECTED_PLUGINS = [
"permission_allowed",
"render_cell",
"startup",
"table_actions",
],
},
{

View file

@ -296,3 +296,15 @@ def forbidden(datasette, request, message):
def menu_links(datasette, actor):
if actor:
return [{"href": datasette.urls.instance(), "label": "Hello"}]
@hookimpl
def table_actions(datasette, database, table, actor):
if actor:
return [
{
"href": datasette.urls.instance(),
"label": "Database: {}".format(database),
},
{"href": datasette.urls.instance(), "label": "Table: {}".format(table)},
]

View file

@ -155,3 +155,12 @@ def menu_links(datasette, actor):
return [{"href": datasette.urls.instance(), "label": "Hello 2"}]
return inner
@hookimpl
def table_actions(datasette, database, table, actor):
async def inner():
if actor:
return [{"href": datasette.urls.instance(), "label": "From async"}]
return inner

View file

@ -782,3 +782,22 @@ def test_hook_menu_links(app_client):
{"label": "Hello", "href": "/"},
{"label": "Hello 2", "href": "/"},
]
def test_hook_table_actions(app_client):
def get_table_actions_links(html):
soup = Soup(html, "html.parser")
details = soup.find("details", {"class": "table-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/facetable")
assert get_table_actions_links(response.text) == []
response_2 = app_client.get("/fixtures/facetable?_bot=1")
assert get_table_actions_links(response_2.text) == [
{"label": "From async", "href": "/"},
{"label": "Database: fixtures", "href": "/"},
{"label": "Table: facetable", "href": "/"},
]