kopia lustrzana https://github.com/simonw/datasette
render_cell(..., request) argument, closes #2007
rodzic
0728c2e41b
commit
226dde0d66
|
@ -60,7 +60,7 @@ def publish_subcommand(publish):
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
@hookspec
|
||||||
def render_cell(row, value, column, table, database, datasette):
|
def render_cell(row, value, column, table, database, datasette, request):
|
||||||
"""Customize rendering of HTML table cell values"""
|
"""Customize rendering of HTML table cell values"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -396,6 +396,7 @@ class QueryView(DataView):
|
||||||
table=None,
|
table=None,
|
||||||
database=database,
|
database=database,
|
||||||
datasette=self.ds,
|
datasette=self.ds,
|
||||||
|
request=request,
|
||||||
):
|
):
|
||||||
candidate = await await_me_maybe(candidate)
|
candidate = await await_me_maybe(candidate)
|
||||||
if candidate is not None:
|
if candidate is not None:
|
||||||
|
|
|
@ -51,6 +51,7 @@ class RowView(DataView):
|
||||||
rows,
|
rows,
|
||||||
link_column=False,
|
link_column=False,
|
||||||
truncate_cells=0,
|
truncate_cells=0,
|
||||||
|
request=request,
|
||||||
)
|
)
|
||||||
for column in display_columns:
|
for column in display_columns:
|
||||||
column["sortable"] = False
|
column["sortable"] = False
|
||||||
|
|
|
@ -783,6 +783,7 @@ class TableView(DataView):
|
||||||
sortable_columns=await self.sortable_columns_for_table(
|
sortable_columns=await self.sortable_columns_for_table(
|
||||||
database_name, table_name, use_rowid=True
|
database_name, table_name, use_rowid=True
|
||||||
),
|
),
|
||||||
|
request=request,
|
||||||
)
|
)
|
||||||
metadata = (
|
metadata = (
|
||||||
(self.ds.metadata("databases") or {})
|
(self.ds.metadata("databases") or {})
|
||||||
|
@ -911,6 +912,7 @@ async def display_columns_and_rows(
|
||||||
link_column=False,
|
link_column=False,
|
||||||
truncate_cells=0,
|
truncate_cells=0,
|
||||||
sortable_columns=None,
|
sortable_columns=None,
|
||||||
|
request=None,
|
||||||
):
|
):
|
||||||
"""Returns columns, rows for specified table - including fancy foreign key treatment"""
|
"""Returns columns, rows for specified table - including fancy foreign key treatment"""
|
||||||
sortable_columns = sortable_columns or set()
|
sortable_columns = sortable_columns or set()
|
||||||
|
@ -992,6 +994,7 @@ async def display_columns_and_rows(
|
||||||
table=table_name,
|
table=table_name,
|
||||||
database=database_name,
|
database=database_name,
|
||||||
datasette=datasette,
|
datasette=datasette,
|
||||||
|
request=request,
|
||||||
):
|
):
|
||||||
candidate = await await_me_maybe(candidate)
|
candidate = await await_me_maybe(candidate)
|
||||||
if candidate is not None:
|
if candidate is not None:
|
||||||
|
|
|
@ -380,8 +380,8 @@ Examples: `datasette-publish-fly <https://datasette.io/plugins/datasette-publish
|
||||||
|
|
||||||
.. _plugin_hook_render_cell:
|
.. _plugin_hook_render_cell:
|
||||||
|
|
||||||
render_cell(row, value, column, table, database, datasette)
|
render_cell(row, value, column, table, database, datasette, request)
|
||||||
-----------------------------------------------------------
|
--------------------------------------------------------------------
|
||||||
|
|
||||||
Lets you customize the display of values within table cells in the HTML table view.
|
Lets you customize the display of values within table cells in the HTML table view.
|
||||||
|
|
||||||
|
@ -403,6 +403,9 @@ Lets you customize the display of values within table cells in the HTML table vi
|
||||||
``datasette`` - :ref:`internals_datasette`
|
``datasette`` - :ref:`internals_datasette`
|
||||||
You can use this to access plugin configuration options via ``datasette.plugin_config(your_plugin_name)``, or to execute SQL queries.
|
You can use this to access plugin configuration options via ``datasette.plugin_config(your_plugin_name)``, or to execute SQL queries.
|
||||||
|
|
||||||
|
``request`` - :ref:`internals_request`
|
||||||
|
The current request object
|
||||||
|
|
||||||
If your hook returns ``None``, it will be ignored. Use this to indicate that your hook is not able to custom render this particular value.
|
If your hook returns ``None``, it will be ignored. Use this to indicate that your hook is not able to custom render this particular value.
|
||||||
|
|
||||||
If the hook returns a string, that string will be rendered in the table cell.
|
If the hook returns a string, that string will be rendered in the table cell.
|
||||||
|
|
|
@ -98,23 +98,24 @@ def extra_body_script(
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
def render_cell(row, value, column, table, database, datasette):
|
def render_cell(row, value, column, table, database, datasette, request):
|
||||||
async def inner():
|
async def inner():
|
||||||
# Render some debug output in cell with value RENDER_CELL_DEMO
|
# Render some debug output in cell with value RENDER_CELL_DEMO
|
||||||
if value == "RENDER_CELL_DEMO":
|
if value == "RENDER_CELL_DEMO":
|
||||||
return json.dumps(
|
data = {
|
||||||
{
|
"row": dict(row),
|
||||||
"row": dict(row),
|
"column": column,
|
||||||
"column": column,
|
"table": table,
|
||||||
"table": table,
|
"database": database,
|
||||||
"database": database,
|
"config": datasette.plugin_config(
|
||||||
"config": datasette.plugin_config(
|
"name-of-plugin",
|
||||||
"name-of-plugin",
|
database=database,
|
||||||
database=database,
|
table=table,
|
||||||
table=table,
|
),
|
||||||
),
|
}
|
||||||
}
|
if request.args.get("_render_cell_extra"):
|
||||||
)
|
data["render_cell_extra"] = 1
|
||||||
|
return json.dumps(data)
|
||||||
elif value == "RENDER_CELL_ASYNC":
|
elif value == "RENDER_CELL_ASYNC":
|
||||||
return (
|
return (
|
||||||
await datasette.get_database(database).execute(
|
await datasette.get_database(database).execute(
|
||||||
|
|
|
@ -187,7 +187,9 @@ async def test_hook_render_cell_link_from_json(ds_client):
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_hook_render_cell_demo(ds_client):
|
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")
|
soup = Soup(response.text, "html.parser")
|
||||||
td = soup.find("td", {"class": "col-content"})
|
td = soup.find("td", {"class": "col-content"})
|
||||||
assert json.loads(td.string) == {
|
assert json.loads(td.string) == {
|
||||||
|
@ -196,6 +198,7 @@ async def test_hook_render_cell_demo(ds_client):
|
||||||
"table": "simple_primary_key",
|
"table": "simple_primary_key",
|
||||||
"database": "fixtures",
|
"database": "fixtures",
|
||||||
"config": {"depth": "table", "special": "this-is-simple_primary_key"},
|
"config": {"depth": "table", "special": "this-is-simple_primary_key"},
|
||||||
|
"render_cell_extra": 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue