Skip to content

Commit

Permalink
Refactor template setup into Datasette constructor
Browse files Browse the repository at this point in the history
Closes #707
  • Loading branch information
simonw committed Mar 27, 2020
1 parent 6aa516d commit 2aaad72
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
65 changes: 33 additions & 32 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,39 @@ def __init__(
# Plugin already registered
pass

# Configure Jinja
default_templates = str(app_root / "datasette" / "templates")
template_paths = []
if self.template_dir:
template_paths.append(self.template_dir)
plugin_template_paths = [
plugin["templates_path"]
for plugin in get_plugins()
if plugin["templates_path"]
]
template_paths.extend(plugin_template_paths)
template_paths.append(default_templates)
template_loader = ChoiceLoader(
[
FileSystemLoader(template_paths),
# Support {% extends "default:table.html" %}:
PrefixLoader(
{"default": FileSystemLoader(default_templates)}, delimiter=":"
),
]
)
self.jinja_env = Environment(
loader=template_loader, autoescape=True, enable_async=True
)
self.jinja_env.filters["escape_css_string"] = escape_css_string
self.jinja_env.filters["quote_plus"] = lambda u: urllib.parse.quote_plus(u)
self.jinja_env.filters["escape_sqlite"] = escape_sqlite
self.jinja_env.filters["to_css_class"] = to_css_class
# pylint: disable=no-member
pm.hook.prepare_jinja2_environment(env=self.jinja_env)

self.register_renderers()

def add_database(self, name, db):
self.databases[name] = db

Expand Down Expand Up @@ -611,38 +644,6 @@ def _asset_urls(self, key, template, context):

def app(self):
"Returns an ASGI app function that serves the whole of Datasette"
default_templates = str(app_root / "datasette" / "templates")
template_paths = []
if self.template_dir:
template_paths.append(self.template_dir)
plugin_template_paths = [
plugin["templates_path"]
for plugin in get_plugins()
if plugin["templates_path"]
]
template_paths.extend(plugin_template_paths)
template_paths.append(default_templates)
template_loader = ChoiceLoader(
[
FileSystemLoader(template_paths),
# Support {% extends "default:table.html" %}:
PrefixLoader(
{"default": FileSystemLoader(default_templates)}, delimiter=":"
),
]
)
self.jinja_env = Environment(
loader=template_loader, autoescape=True, enable_async=True
)
self.jinja_env.filters["escape_css_string"] = escape_css_string
self.jinja_env.filters["quote_plus"] = lambda u: urllib.parse.quote_plus(u)
self.jinja_env.filters["escape_sqlite"] = escape_sqlite
self.jinja_env.filters["to_css_class"] = to_css_class
# pylint: disable=no-member
pm.hook.prepare_jinja2_environment(env=self.jinja_env)

self.register_renderers()

routes = []

def add_route(view, regex):
Expand Down
2 changes: 1 addition & 1 deletion docs/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ This method lets you read plugin configuration values that were set in ``metadat
``template`` - string
The template file to be rendered, e.g. ``my_plugin.html``. Datasette will search for this file first in the ``--template-dir=`` location, if it was specified - then in the plugin's bundled templates and finally in Datasette's set of default templates.

``conttext`` - None or a Python dictionary
``context`` - None or a Python dictionary
The context variables to pass to the template.

``request`` - request object or None
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ 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"] if request else None
}, default=lambda b: b.decode("utf8"))
}
"""
Expand Down Expand Up @@ -468,7 +468,7 @@ async def inner():
return {
"extra_template_vars_from_awaitable": json.dumps({
"template": template,
"scope_path": request.scope["path"],
"scope_path": request.scope["path"] if request else None,
"awaitable": True,
}, default=lambda b: b.decode("utf8")),
"query_database": query_database,
Expand Down

0 comments on commit 2aaad72

Please sign in to comment.