New plugin hook: extra_body_script

This commit is contained in:
Simon Willison 2018-08-28 01:56:44 -07:00
commit 5cf0c6c91c
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
6 changed files with 88 additions and 0 deletions

View file

@ -222,6 +222,23 @@ def extra_js_urls():
'url': 'https://example.com/jquery.js',
'sri': 'SRIHASH',
}, 'https://example.com/plugin1.js']
@hookimpl
def extra_body_script(template, database, table, datasette):
import json
return 'var extra_body_script = {};'.format(
json.dumps({
"template": template,
"database": database,
"table": table,
"config": datasette.plugin_config(
"name-of-plugin",
database=database,
table=table,
)
})
)
'''
PLUGIN2 = '''

View file

@ -2,6 +2,8 @@ from bs4 import BeautifulSoup as Soup
from .fixtures import ( # noqa
app_client,
)
import json
import re
import pytest
import urllib
@ -102,3 +104,42 @@ def test_plugin_config(app_client):
)
assert {"depth": "root"} == app_client.ds.plugin_config("name-of-plugin")
assert None is app_client.ds.plugin_config("unknown-plugin")
@pytest.mark.parametrize(
"path,expected_extra_body_script",
[
(
"/",
{
"template": "index.html",
"database": None,
"table": None,
"config": {"depth": "root"},
},
),
(
"/fixtures/",
{
"template": "database.html",
"database": "fixtures",
"table": None,
"config": {"depth": "database"},
},
),
(
"/fixtures/sortable",
{
"template": "table.html",
"database": "fixtures",
"table": "sortable",
"config": {"depth": "table"},
},
),
],
)
def test_extra_body_script(app_client, path, expected_extra_body_script):
r = re.compile(r"<script>var extra_body_script = (.*?);</script>")
json_data = r.search(app_client.get(path).body.decode("utf8")).group(1)
actual_data = json.loads(json_data)
assert expected_extra_body_script == actual_data