mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
homepage_actions() plugin hook, closes #2298
This commit is contained in:
parent
7b32d5f7d8
commit
daf5ca02ca
8 changed files with 118 additions and 2 deletions
|
|
@ -42,6 +42,7 @@ EXPECTED_PLUGINS = [
|
|||
"extra_js_urls",
|
||||
"extra_template_vars",
|
||||
"forbidden",
|
||||
"homepage_actions",
|
||||
"menu_links",
|
||||
"permission_allowed",
|
||||
"prepare_connection",
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue