mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
query_actions plugin hook
* New query_actions plugin hook, closes #2283
This commit is contained in:
parent
f99c2f5f8c
commit
6ec0081f5d
7 changed files with 151 additions and 0 deletions
|
|
@ -46,6 +46,7 @@ EXPECTED_PLUGINS = [
|
|||
"permission_allowed",
|
||||
"prepare_connection",
|
||||
"prepare_jinja2_environment",
|
||||
"query_actions",
|
||||
"register_facet_classes",
|
||||
"register_magic_parameters",
|
||||
"register_permissions",
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ from datasette.utils.asgi import asgi_send_json, Response
|
|||
import base64
|
||||
import pint
|
||||
import json
|
||||
import urllib
|
||||
|
||||
ureg = pint.UnitRegistry()
|
||||
|
||||
|
|
@ -390,6 +391,23 @@ def table_actions(datasette, database, table, actor):
|
|||
]
|
||||
|
||||
|
||||
@hookimpl
|
||||
def query_actions(datasette, database, query_name, sql):
|
||||
args = {
|
||||
"sql": sql,
|
||||
}
|
||||
if query_name:
|
||||
args["query_name"] = query_name
|
||||
return [
|
||||
{
|
||||
"href": datasette.urls.database(database)
|
||||
+ "/-/explain?"
|
||||
+ urllib.parse.urlencode(args),
|
||||
"label": "Explain this query",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@hookimpl
|
||||
def database_actions(datasette, database, actor, request):
|
||||
if actor:
|
||||
|
|
|
|||
|
|
@ -945,6 +945,31 @@ async def test_hook_table_actions(ds_client, table_or_view):
|
|||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"path,expected_url",
|
||||
(
|
||||
("/fixtures?sql=select+1", "/fixtures/-/explain?sql=select+1"),
|
||||
(
|
||||
"/fixtures/pragma_cache_size",
|
||||
"/fixtures/-/explain?sql=PRAGMA+cache_size%3B&query_name=pragma_cache_size",
|
||||
),
|
||||
),
|
||||
)
|
||||
async def test_hook_query_actions(ds_client, path, expected_url):
|
||||
def get_table_actions_links(html):
|
||||
soup = Soup(html, "html.parser")
|
||||
details = soup.find("details", {"class": "actions-menu-links"})
|
||||
if details is None:
|
||||
return []
|
||||
return [{"label": a.text, "href": a["href"]} for a in details.select("a")]
|
||||
|
||||
response = await ds_client.get(path)
|
||||
assert response.status_code == 200
|
||||
links = get_table_actions_links(response.text)
|
||||
assert links == [{"label": "Explain this query", "href": expected_url}]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hook_database_actions(ds_client):
|
||||
def get_table_actions_links(html):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue