render_cell(..., request) argument, closes #2007

This commit is contained in:
Simon Willison 2023-01-27 19:34:14 -08:00
commit 0b4a286914
7 changed files with 30 additions and 18 deletions

View file

@ -98,23 +98,24 @@ def extra_body_script(
@hookimpl
def render_cell(row, value, column, table, database, datasette):
def render_cell(row, value, column, table, database, datasette, request):
async def inner():
# Render some debug output in cell with value RENDER_CELL_DEMO
if value == "RENDER_CELL_DEMO":
return json.dumps(
{
"row": dict(row),
"column": column,
"table": table,
"database": database,
"config": datasette.plugin_config(
"name-of-plugin",
database=database,
table=table,
),
}
)
data = {
"row": dict(row),
"column": column,
"table": table,
"database": database,
"config": datasette.plugin_config(
"name-of-plugin",
database=database,
table=table,
),
}
if request.args.get("_render_cell_extra"):
data["render_cell_extra"] = 1
return json.dumps(data)
elif value == "RENDER_CELL_ASYNC":
return (
await datasette.get_database(database).execute(

View file

@ -187,7 +187,9 @@ async def test_hook_render_cell_link_from_json(ds_client):
@pytest.mark.asyncio
async def test_hook_render_cell_demo(ds_client):
response = await ds_client.get("/fixtures/simple_primary_key?id=4")
response = await ds_client.get(
"/fixtures/simple_primary_key?id=4&_render_cell_extra=1"
)
soup = Soup(response.text, "html.parser")
td = soup.find("td", {"class": "col-content"})
assert json.loads(td.string) == {
@ -196,6 +198,7 @@ async def test_hook_render_cell_demo(ds_client):
"table": "simple_primary_key",
"database": "fixtures",
"config": {"depth": "table", "special": "this-is-simple_primary_key"},
"render_cell_extra": 1,
}