New plugin hook: canned_queries(), refs #852

This commit is contained in:
Simon Willison 2020-06-18 16:22:33 -07:00
commit 6c26345836
12 changed files with 203 additions and 50 deletions

View file

@ -40,6 +40,7 @@ EXPECTED_PLUGINS = [
"hooks": [
"actor_from_request",
"asgi_wrapper",
"canned_queries",
"extra_body_script",
"extra_css_urls",
"extra_js_urls",
@ -61,6 +62,7 @@ EXPECTED_PLUGINS = [
"hooks": [
"actor_from_request",
"asgi_wrapper",
"canned_queries",
"extra_js_urls",
"extra_template_vars",
"permission_allowed",

View file

@ -193,3 +193,12 @@ def register_routes():
@hookimpl
def startup(datasette):
datasette._startup_hook_fired = True
@hookimpl
def canned_queries(datasette, database, actor):
return {
"from_hook": "select 1, '{}' as actor_id".format(
actor["id"] if actor else "null"
)
}

View file

@ -129,3 +129,17 @@ def startup(datasette):
datasette._startup_hook_calculation = result.first()[0]
return inner
@hookimpl
def canned_queries(datasette, database):
async def inner():
return {
"from_async_hook": "select {}".format(
(
await datasette.get_database(database).execute("select 1 + 1")
).first()[0]
)
}
return inner

View file

@ -111,7 +111,13 @@ def test_canned_query_permissions_on_database_page(canned_write_client):
query_names = [
q["name"] for q in canned_write_client.get("/data.json").json["queries"]
]
assert ["add_name", "add_name_specify_id", "update_name"] == query_names
assert [
"add_name",
"add_name_specify_id",
"update_name",
"from_async_hook",
"from_hook",
] == query_names
# With auth shows four
response = canned_write_client.get(
@ -124,6 +130,8 @@ def test_canned_query_permissions_on_database_page(canned_write_client):
{"name": "add_name_specify_id", "private": False},
{"name": "delete_name", "private": True},
{"name": "update_name", "private": False},
{"name": "from_async_hook", "private": False},
{"name": "from_hook", "private": False},
] == [
{"name": q["name"], "private": q["private"]} for q in response.json["queries"]
]

View file

@ -97,6 +97,8 @@ def test_database_page(app_client):
),
("/fixtures/pragma_cache_size", "pragma_cache_size"),
("/fixtures/neighborhood_search#fragment-goes-here", "Search neighborhoods"),
("/fixtures/from_async_hook", "from_async_hook"),
("/fixtures/from_hook", "from_hook"),
] == [(a["href"], a.text) for a in queries_ul.find_all("a")]

View file

@ -591,3 +591,34 @@ async def test_startup(app_client):
await app_client.ds.invoke_startup()
assert app_client.ds._startup_hook_fired
assert 2 == app_client.ds._startup_hook_calculation
def test_canned_queries(app_client):
queries = app_client.get("/fixtures.json").json["queries"]
queries_by_name = {q["name"]: q for q in queries}
assert {
"sql": "select 2",
"name": "from_async_hook",
"private": False,
} == queries_by_name["from_async_hook"]
assert {
"sql": "select 1, 'null' as actor_id",
"name": "from_hook",
"private": False,
} == queries_by_name["from_hook"]
def test_canned_queries_non_async(app_client):
response = app_client.get("/fixtures/from_hook.json?_shape=array")
assert [{"1": 1, "actor_id": "null"}] == response.json
def test_canned_queries_async(app_client):
response = app_client.get("/fixtures/from_async_hook.json?_shape=array")
assert [{"2": 2}] == response.json
def test_canned_queries_actor(app_client):
assert [{"1": 1, "actor_id": "bot"}] == app_client.get(
"/fixtures/from_hook.json?_bot=1&_shape=array"
).json