New plugin hook: extra_serve_options()

This commit is contained in:
Simon Willison 2019-07-25 17:09:13 +03:00
commit 894c424b90
6 changed files with 78 additions and 2 deletions

View file

@ -108,6 +108,7 @@ def make_app_client(
inspect_data=None,
static_mounts=None,
template_dir=None,
plugin_extra_options=None,
):
with tempfile.TemporaryDirectory() as tmpdir:
filepath = os.path.join(tmpdir, filename)
@ -151,6 +152,7 @@ def make_app_client(
inspect_data=inspect_data,
static_mounts=static_mounts,
template_dir=template_dir,
plugin_extra_options=plugin_extra_options,
)
ds.sqlite_functions.append(("sleep", 1, lambda n: time.sleep(float(n))))
client = TestClient(ds.app())
@ -386,7 +388,8 @@ def extra_template_vars(template, database, table, view_name, request, datasette
return {
"extra_template_vars": json.dumps({
"template": template,
"scope_path": request.scope["path"]
"scope_path": request.scope["path"],
"plugin_extra_options": datasette.plugin_extra_options,
}, default=lambda b: b.decode("utf8"))
}
"""

View file

@ -203,6 +203,7 @@ def test_plugins_extra_template_vars(restore_working_directory):
assert {
"template": "show_json.html",
"scope_path": "/-/metadata",
"plugin_extra_options": {},
} == extra_template_vars
extra_template_vars_from_awaitable = json.loads(
Soup(response.body, "html.parser")
@ -214,3 +215,16 @@ def test_plugins_extra_template_vars(restore_working_directory):
"awaitable": True,
"scope_path": "/-/metadata",
} == extra_template_vars_from_awaitable
def test_plugin_extra_options_available_on_datasette(restore_working_directory):
for client in make_app_client(
template_dir=str(pathlib.Path(__file__).parent / "test_templates"),
plugin_extra_options={"foo": "bar"},
):
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 {"foo": "bar"} == extra_template_vars["plugin_extra_options"]