Skip to content

Commit

Permalink
top_query() and top_canned_query()
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 31, 2024
1 parent 9872565 commit a097d6d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
12 changes: 6 additions & 6 deletions datasette/hookspecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ def top_row(datasette, request, database, table, row):
"""HTML to include at the top of the row page"""


# @hookspec
# def top_query(datasette, request, database, sql):
# """HTML to include at the top of the query results page"""
@hookspec
def top_query(datasette, request, database, sql):
"""HTML to include at the top of the query results page"""


# @hookspec
# def top_canned_query(datasette, request, database, query_name):
# """HTML to include at the top of the canned query page"""
@hookspec
def top_canned_query(datasette, request, database, query_name):
"""HTML to include at the top of the canned query page"""
2 changes: 2 additions & 0 deletions datasette/templates/query.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

<h1 style="padding-left: 10px; border-left: 10px solid #{{ database_color }}">{{ metadata.title or database }}{% if canned_query and not metadata.title %}: {{ canned_query }}{% endif %}{% if private %} 🔒{% endif %}</h1>

{% if canned_query %}{{ top_canned_query() }}{% else %}{{ top_query() }}{% endif %}

{% block description_source_license %}{% include "_description_source_license.html" %}{% endblock %}

<form class="sql" action="{{ urls.database(database) }}{% if canned_query %}/{{ canned_query }}{% endif %}" method="{% if canned_query_write %}post{% else %}get{% endif %}">
Expand Down
16 changes: 16 additions & 0 deletions datasette/views/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ class QueryContext:
"help": "List of templates that were considered for rendering this page"
}
)
top_query: callable = field(
metadata={"help": "Callable to render the top_query slot"}
)
top_canned_query: callable = field(
metadata={"help": "Callable to render the top_canned_query slot"}
)


async def get_tables(datasette, request, db):
Expand Down Expand Up @@ -730,6 +736,16 @@ async def fetch_data_for_csv(request, _next=None):
f"{'*' if template_name == template.name else ''}{template_name}"
for template_name in templates
],
top_query=make_slot_function(
"top_query", datasette, request, database=database, sql=sql
),
top_canned_query=make_slot_function(
"top_canned_query",
datasette,
request,
database=database,
query_name=canned_query["name"] if canned_query else None,
),
),
request=request,
view_name="database",
Expand Down
30 changes: 2 additions & 28 deletions docs/plugin_hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1642,32 +1642,6 @@ This hook is responsible for returning a dictionary corresponding to Datasette :
Example: `datasette-remote-metadata plugin <https://datasette.io/plugins/datasette-remote-metadata>`__


@hookimpl
def top_homepage(datasette, request):
"""HTML to include at the top of the homepage"""


@hookimpl
def top_database(datasette, request, database):
"""HTML to include at the top of the database page"""


@hookimpl
def top_table(datasette, request, database, table):
"""HTML to include at the top of the table page"""


@hookimpl
def top_row(datasette, request, database, table, row):
"""HTML to include at the top of the row page"""


@hookimpl
def top_query(datasette, request, database, query):
"""HTML to include at the top of the query page"""


.. _plugin_hook_top_homepage:

top_homepage(datasette, request)
Expand Down Expand Up @@ -1700,7 +1674,7 @@ Returns HTML to be displayed at the top of the database page.
.. _plugin_hook_top_table:

top_table(datasette, request, database, table)
---------------------------------------------
----------------------------------------------

``datasette`` - :ref:`internals_datasette`
You can use this to access plugin configuration options via ``datasette.plugin_config(your_plugin_name)``.
Expand All @@ -1719,7 +1693,7 @@ Returns HTML to be displayed at the top of the table page.
.. _plugin_hook_top_row:

top_row(datasette, request, database, table, row)
------------------------------------------------
-------------------------------------------------

``datasette`` - :ref:`internals_datasette`
You can use this to access plugin configuration options via ``datasette.plugin_config(your_plugin_name)``.
Expand Down
30 changes: 30 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,14 @@ def top_row(self, request, database, table, row):
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():
Expand Down Expand Up @@ -1405,3 +1413,25 @@ async def test_hook_top_row(ds_client):
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")

0 comments on commit a097d6d

Please sign in to comment.