mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
parent
a18e0964ec
commit
e06e083240
8 changed files with 171 additions and 19 deletions
|
|
@ -376,6 +376,16 @@ def render_cell(value, column, table, database, datasette):
|
|||
table=table,
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
@hookimpl
|
||||
def extra_template_vars(template, database, table, view_name, request, datasette):
|
||||
return {
|
||||
"extra_template_vars": json.dumps({
|
||||
"template": template,
|
||||
"scope_path": request.scope["path"]
|
||||
}, default=lambda b: b.decode("utf8"))
|
||||
}
|
||||
"""
|
||||
|
||||
PLUGIN2 = """
|
||||
|
|
@ -424,6 +434,19 @@ def render_cell(value, database):
|
|||
)
|
||||
|
||||
|
||||
@hookimpl
|
||||
def extra_template_vars(template, database, table, view_name, request, datasette):
|
||||
async def inner():
|
||||
return {
|
||||
"extra_template_vars_from_awaitable": json.dumps({
|
||||
"template": template,
|
||||
"scope_path": request.scope["path"],
|
||||
"awaitable": True,
|
||||
}, default=lambda b: b.decode("utf8"))
|
||||
}
|
||||
return inner
|
||||
|
||||
|
||||
@hookimpl
|
||||
def asgi_wrapper(datasette):
|
||||
def wrap_with_databases_header(app):
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ from .fixtures import app_client, make_app_client, TEMP_PLUGIN_SECRET_FILE # no
|
|||
import base64
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import re
|
||||
import pytest
|
||||
import urllib
|
||||
|
|
@ -188,3 +189,28 @@ def test_plugins_extra_body_script(app_client, path, expected_extra_body_script)
|
|||
def test_plugins_asgi_wrapper(app_client):
|
||||
response = app_client.get("/fixtures")
|
||||
assert "fixtures" == response.headers["x-databases"]
|
||||
|
||||
|
||||
def test_plugins_extra_template_vars():
|
||||
for client in make_app_client(
|
||||
template_dir=str(pathlib.Path(__file__).parent / "test_templates")
|
||||
):
|
||||
response = client.get("/-/metadata")
|
||||
assert response.status == 200
|
||||
extra_template_vars = json.loads(
|
||||
Soup(response.body, "html.parser").select("pre.extra_template_vars")[0].text
|
||||
)
|
||||
assert {
|
||||
"template": "show_json.html",
|
||||
"scope_path": "/-/metadata",
|
||||
} == extra_template_vars
|
||||
extra_template_vars_from_awaitable = json.loads(
|
||||
Soup(response.body, "html.parser")
|
||||
.select("pre.extra_template_vars_from_awaitable")[0]
|
||||
.text
|
||||
)
|
||||
assert {
|
||||
"template": "show_json.html",
|
||||
"awaitable": True,
|
||||
"scope_path": "/-/metadata",
|
||||
} == extra_template_vars_from_awaitable
|
||||
|
|
|
|||
8
tests/test_templates/show_json.html
Normal file
8
tests/test_templates/show_json.html
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
{{ super() }}
|
||||
Test data for extra_template_vars:
|
||||
<pre class="extra_template_vars">{{ extra_template_vars|safe }}</pre>
|
||||
<pre class="extra_template_vars_from_awaitable">{{ extra_template_vars_from_awaitable|safe }}</pre>
|
||||
{% endblock %}
|
||||
Loading…
Add table
Add a link
Reference in a new issue