mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Pass view_name=page to custom pages, refs #648
This commit is contained in:
parent
6add534c65
commit
b5509a0b0a
3 changed files with 26 additions and 5 deletions
|
|
@ -763,7 +763,9 @@ class DatasetteRouter(AsgiRouter):
|
|||
template = None
|
||||
if template:
|
||||
await asgi_send_html(
|
||||
send, await self.ds.render_template(template), status=200
|
||||
send,
|
||||
await self.ds.render_template(template, view_name="page"),
|
||||
status=200,
|
||||
)
|
||||
else:
|
||||
await self.handle_500(
|
||||
|
|
|
|||
|
|
@ -109,6 +109,7 @@ def make_app_client(
|
|||
inspect_data=None,
|
||||
static_mounts=None,
|
||||
template_dir=None,
|
||||
extra_plugins=None,
|
||||
):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
filepath = os.path.join(tmpdir, filename)
|
||||
|
|
@ -133,6 +134,8 @@ def make_app_client(
|
|||
os.mkdir(plugins_dir)
|
||||
open(os.path.join(plugins_dir, "my_plugin.py"), "w").write(PLUGIN1)
|
||||
open(os.path.join(plugins_dir, "my_plugin_2.py"), "w").write(PLUGIN2)
|
||||
for filename, content in (extra_plugins or {}).items():
|
||||
open(os.path.join(plugins_dir, filename), "w").write(content)
|
||||
config = config or {}
|
||||
config.update(
|
||||
{
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import json
|
|||
import pathlib
|
||||
import pytest
|
||||
import re
|
||||
import textwrap
|
||||
import urllib.parse
|
||||
|
||||
|
||||
|
|
@ -1243,16 +1244,31 @@ def test_base_url_config(base_url, path):
|
|||
}
|
||||
|
||||
|
||||
def test_custom_template_page(tmpdir):
|
||||
def test_custom_pages(tmpdir):
|
||||
template_dir = tmpdir.mkdir("page-templates")
|
||||
extra_plugins = {
|
||||
"view_name.py": textwrap.dedent(
|
||||
"""
|
||||
from datasette import hookimpl
|
||||
|
||||
@hookimpl
|
||||
def extra_template_vars(view_name):
|
||||
return {
|
||||
"view_name": view_name,
|
||||
}
|
||||
"""
|
||||
)
|
||||
}
|
||||
pages_dir = template_dir.mkdir("pages")
|
||||
(pages_dir / "about.html").write_text("ABOUT!", "utf-8")
|
||||
(pages_dir / "about.html").write_text("ABOUT! view_name:{{ view_name }}", "utf-8")
|
||||
nested_dir = pages_dir.mkdir("nested")
|
||||
(nested_dir / "nest.html").write_text("Nest!", "utf-8")
|
||||
for client in make_app_client(template_dir=str(template_dir)):
|
||||
for client in make_app_client(
|
||||
template_dir=str(template_dir), extra_plugins=extra_plugins
|
||||
):
|
||||
response = client.get("/about")
|
||||
assert 200 == response.status
|
||||
assert "ABOUT!" == response.text
|
||||
assert "ABOUT! view_name:page" == response.text
|
||||
response = client.get("/nested/nest")
|
||||
assert 200 == response.status
|
||||
assert "Nest!" == response.text
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue