row_actions() plugin hook, closes #2299

This commit is contained in:
Simon Willison 2024-03-12 16:13:31 -07:00
commit b8711988b9
7 changed files with 134 additions and 7 deletions

View file

@ -53,6 +53,7 @@ EXPECTED_PLUGINS = [
"register_permissions",
"register_routes",
"render_cell",
"row_actions",
"skip_csrf",
"startup",
"table_actions",

View file

@ -423,6 +423,18 @@ def query_actions(datasette, database, query_name, sql):
]
@hookimpl
def row_actions(datasette, database, table, actor, row):
if actor:
return [
{
"href": datasette.urls.instance(),
"label": f"Row details for {actor['id']}",
"description": json.dumps(dict(row), default=repr),
},
]
@hookimpl
def database_actions(datasette, database, actor, request):
if actor:

View file

@ -1000,6 +1000,24 @@ async def test_hook_query_actions(ds_client, path, expected_url):
]
@pytest.mark.asyncio
async def test_hook_row_actions(ds_client):
response = await ds_client.get("/fixtures/facet_cities/1")
assert get_actions_links(response.text) == []
response_2 = await ds_client.get(
"/fixtures/facet_cities/1",
cookies={"ds_actor": ds_client.actor_cookie({"id": "sam"})},
)
assert get_actions_links(response_2.text) == [
{
"label": "Row details for sam",
"href": "/",
"description": '{"id": 1, "name": "San Francisco"}',
}
]
@pytest.mark.asyncio
async def test_hook_database_actions(ds_client):
response = await ds_client.get("/fixtures")