Template slot family of plugin hooks - top_homepage() and others

New plugin hooks:

top_homepage
top_database
top_table
top_row
top_query
top_canned_query

New datasette.utils.make_slot_function()

Closes #1191
This commit is contained in:
Simon Willison 2024-01-30 19:54:03 -08:00 committed by GitHub
commit c3caf36af7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 324 additions and 7 deletions

View file

@ -41,7 +41,9 @@ def plugin_hooks_content():
"plugin", [name for name in dir(app.pm.hook) if not name.startswith("_")]
)
def test_plugin_hooks_are_documented(plugin, plugin_hooks_content):
headings = get_headings(plugin_hooks_content, "-")
headings = set()
headings.update(get_headings(plugin_hooks_content, "-"))
headings.update(get_headings(plugin_hooks_content, "~"))
assert plugin in headings
hook_caller = getattr(app.pm.hook, plugin)
arg_names = [a for a in hook_caller.spec.argnames if a != "__multicall__"]

View file

@ -1334,3 +1334,104 @@ async def test_hook_jinja2_environment_from_request(tmpdir):
assert "Hello museums!" in response2.text
finally:
pm.unregister(name="EnvironmentPlugin")
class SlotPlugin:
__name__ = "SlotPlugin"
@hookimpl
def top_homepage(self, request):
return "Xtop_homepage:" + request.args["z"]
@hookimpl
def top_database(self, request, database):
async def inner():
return "Xtop_database:{}:{}".format(database, request.args["z"])
return inner
@hookimpl
def top_table(self, request, database, table):
return "Xtop_table:{}:{}:{}".format(database, table, request.args["z"])
@hookimpl
def top_row(self, request, database, table, row):
return "Xtop_row:{}:{}:{}:{}".format(
database, table, row["name"], request.args["z"]
)
@hookimpl
def top_query(self, request, database, sql):
return "Xtop_query:{}:{}:{}".format(database, sql, request.args["z"])
@hookimpl
def top_canned_query(self, request, database, query_name):
return "Xtop_query:{}:{}:{}".format(database, query_name, request.args["z"])
@pytest.mark.asyncio
async def test_hook_top_homepage():
try:
pm.register(SlotPlugin(), name="SlotPlugin")
datasette = Datasette(memory=True)
response = await datasette.client.get("/?z=foo")
assert response.status_code == 200
assert "Xtop_homepage:foo" in response.text
finally:
pm.unregister(name="SlotPlugin")
@pytest.mark.asyncio
async def test_hook_top_database():
try:
pm.register(SlotPlugin(), name="SlotPlugin")
datasette = Datasette(memory=True)
response = await datasette.client.get("/_memory?z=bar")
assert response.status_code == 200
assert "Xtop_database:_memory:bar" in response.text
finally:
pm.unregister(name="SlotPlugin")
@pytest.mark.asyncio
async def test_hook_top_table(ds_client):
try:
pm.register(SlotPlugin(), name="SlotPlugin")
response = await ds_client.get("/fixtures/facetable?z=baz")
assert response.status_code == 200
assert "Xtop_table:fixtures:facetable:baz" in response.text
finally:
pm.unregister(name="SlotPlugin")
@pytest.mark.asyncio
async def test_hook_top_row(ds_client):
try:
pm.register(SlotPlugin(), name="SlotPlugin")
response = await ds_client.get("/fixtures/facet_cities/1?z=bax")
assert response.status_code == 200
assert "Xtop_row:fixtures:facet_cities:San Francisco:bax" in response.text
finally:
pm.unregister(name="SlotPlugin")
@pytest.mark.asyncio
async def test_hook_top_query(ds_client):
try:
pm.register(SlotPlugin(), name="SlotPlugin")
response = await ds_client.get("/fixtures?sql=select+1&z=x")
assert response.status_code == 200
assert "Xtop_query:fixtures:select 1:x" in response.text
finally:
pm.unregister(name="SlotPlugin")
@pytest.mark.asyncio
async def test_hook_top_canned_query(ds_client):
try:
pm.register(SlotPlugin(), name="SlotPlugin")
response = await ds_client.get("/fixtures/from_hook?z=xyz")
assert response.status_code == 200
assert "Xtop_query:fixtures:from_hook:xyz" in response.text
finally:
pm.unregister(name="SlotPlugin")