mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Async support for prepare_jinja2_environment, closes #1809
This commit is contained in:
parent
2ebcffe222
commit
ddc999ad12
10 changed files with 76 additions and 9 deletions
|
|
@ -71,6 +71,7 @@ EXPECTED_PLUGINS = [
|
|||
"handle_exception",
|
||||
"menu_links",
|
||||
"permission_allowed",
|
||||
"prepare_jinja2_environment",
|
||||
"register_routes",
|
||||
"render_cell",
|
||||
"startup",
|
||||
|
|
|
|||
|
|
@ -143,8 +143,14 @@ def extra_template_vars(
|
|||
|
||||
@hookimpl
|
||||
def prepare_jinja2_environment(env, datasette):
|
||||
env.filters["format_numeric"] = lambda s: f"{float(s):,.0f}"
|
||||
env.filters["to_hello"] = lambda s: datasette._HELLO
|
||||
async def select_times_three(s):
|
||||
db = datasette.get_database()
|
||||
return (await db.execute("select 3 * ?", [int(s)])).first()[0]
|
||||
|
||||
async def inner():
|
||||
env.filters["select_times_three"] = select_times_three
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
@hookimpl
|
||||
|
|
|
|||
|
|
@ -126,6 +126,12 @@ def permission_allowed(datasette, actor, action):
|
|||
return inner
|
||||
|
||||
|
||||
@hookimpl
|
||||
def prepare_jinja2_environment(env, datasette):
|
||||
env.filters["format_numeric"] = lambda s: f"{float(s):,.0f}"
|
||||
env.filters["to_hello"] = lambda s: datasette._HELLO
|
||||
|
||||
|
||||
@hookimpl
|
||||
def startup(datasette):
|
||||
async def inner():
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
from .fixtures import app_client
|
||||
import httpx
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def datasette(app_client):
|
||||
@pytest_asyncio.fixture
|
||||
async def datasette(app_client):
|
||||
await app_client.ds.invoke_startup()
|
||||
return app_client.ds
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -546,11 +546,13 @@ def test_hook_register_output_renderer_can_render(app_client):
|
|||
@pytest.mark.asyncio
|
||||
async def test_hook_prepare_jinja2_environment(app_client):
|
||||
app_client.ds._HELLO = "HI"
|
||||
await app_client.ds.invoke_startup()
|
||||
template = app_client.ds.jinja_env.from_string(
|
||||
"Hello there, {{ a|format_numeric }}, {{ a|to_hello }}", {"a": 3412341}
|
||||
"Hello there, {{ a|format_numeric }}, {{ a|to_hello }}, {{ b|select_times_three }}",
|
||||
{"a": 3412341, "b": 5},
|
||||
)
|
||||
rendered = await app_client.ds.render_template(template)
|
||||
assert "Hello there, 3,412,341, HI" == rendered
|
||||
assert "Hello there, 3,412,341, HI, 15" == rendered
|
||||
|
||||
|
||||
def test_hook_publish_subcommand():
|
||||
|
|
|
|||
|
|
@ -59,6 +59,7 @@ def test_routes(routes, path, expected_class, expected_matches):
|
|||
@pytest_asyncio.fixture
|
||||
async def ds_with_route():
|
||||
ds = Datasette()
|
||||
await ds.invoke_startup()
|
||||
ds.remove_database("_memory")
|
||||
db = Database(ds, is_memory=True, memory_name="route-name-db")
|
||||
ds.add_database(db, name="original-name", route="custom-route-name")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue