homepage_actions() plugin hook, closes #2298

This commit is contained in:
Simon Willison 2024-03-12 13:44:07 -07:00
commit daf5ca02ca
8 changed files with 118 additions and 2 deletions

View file

@ -42,6 +42,7 @@ EXPECTED_PLUGINS = [
"extra_js_urls",
"extra_template_vars",
"forbidden",
"homepage_actions",
"menu_links",
"permission_allowed",
"prepare_connection",

View file

@ -425,6 +425,18 @@ def database_actions(datasette, database, actor, request):
]
@hookimpl
def homepage_actions(datasette, actor, request):
if actor:
label = f"Custom homepage for: {actor['id']}"
return [
{
"href": datasette.urls.path("/-/custom-homepage"),
"label": label,
}
]
@hookimpl
def skip_csrf(scope):
return scope["path"] == "/skip-csrf"

View file

@ -995,6 +995,25 @@ async def test_hook_database_actions(ds_client):
]
@pytest.mark.asyncio
async def test_hook_homepage_actions(ds_client):
response = await ds_client.get("/")
# No button for anonymous users
assert "<span>Homepage actions</span>" not in response.text
# Signed in user gets an action
response2 = await ds_client.get(
"/", cookies={"ds_actor": ds_client.actor_cookie({"id": "troy"})}
)
assert "<span>Homepage actions</span>" in response2.text
assert get_actions_links(response2.text) == [
{
"label": "Custom homepage for: troy",
"href": "/-/custom-homepage",
"description": None,
},
]
def test_hook_skip_csrf(app_client):
cookie = app_client.actor_cookie({"id": "test"})
csrf_response = app_client.post(