More datasette.urls usage, refs #1025

This commit is contained in:
Simon Willison 2020-10-19 21:24:47 -07:00
commit 0d1763fb2f
6 changed files with 26 additions and 10 deletions

View file

@ -1158,7 +1158,7 @@ class DatasetteRouter:
await template.render_async( await template.render_async(
dict( dict(
info, info,
base_url=self.ds.config("base_url"), urls=self.ds.urls,
app_css_hash=self.ds.app_css_hash(), app_css_hash=self.ds.app_css_hash(),
) )
), ),
@ -1270,19 +1270,26 @@ class Urls:
def __init__(self, ds): def __init__(self, ds):
self.ds = ds self.ds = ds
def path(self, path):
if path.startswith("/"):
path = path[1:]
return self.ds.config("base_url") + path
def instance(self): def instance(self):
return self.ds.config("base_url") return self.path("")
def static(self, path): def static(self, path):
return "{}-/static/{}".format(self.instance(), path) return self.path("-/static/{}".format(path))
def logout(self):
return self.path("-/logout")
def database(self, database): def database(self, database):
db = self.ds.databases[database] db = self.ds.databases[database]
base_url = self.ds.config("base_url")
if self.ds.config("hash_urls") and db.hash: if self.ds.config("hash_urls") and db.hash:
return "{}{}-{}".format(base_url, database, db.hash[:HASH_LENGTH]) return self.path("{}-{}".format(database, db.hash[:HASH_LENGTH]))
else: else:
return "{}{}".format(base_url, database) return self.path(database)
def table(self, database, table): def table(self, database, table):
return "{}/{}".format(self.database(database), urllib.parse.quote_plus(table)) return "{}/{}".format(self.database(database), urllib.parse.quote_plus(table))

View file

@ -35,7 +35,7 @@ p.message-warning {
<p>Use this tool to try out different actor and allow combinations. See <a href="https://docs.datasette.io/en/stable/authentication.html#defining-permissions-with-allow-blocks">Defining permissions with "allow" blocks</a> for documentation.</p> <p>Use this tool to try out different actor and allow combinations. See <a href="https://docs.datasette.io/en/stable/authentication.html#defining-permissions-with-allow-blocks">Defining permissions with "allow" blocks</a> for documentation.</p>
<form action="/-/allow-debug" method="get"> <form action="{{ urls.path('-/allow-debug') }}" method="get">
<div class="two-col"> <div class="two-col">
<p><label>Allow block</label></p> <p><label>Allow block</label></p>
<textarea name="allow">{{ allow_input }}</textarea> <textarea name="allow">{{ allow_input }}</textarea>

View file

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<title>{% block title %}{% endblock %}</title> <title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{{ base_url }}-/static/app.css?{{ app_css_hash }}"> <link rel="stylesheet" href="{{ urls.static('app.css') }}?{{ app_css_hash }}">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{% for url in extra_css_urls %} {% for url in extra_css_urls %}
<link rel="stylesheet" href="{{ url.url }}"{% if url.sri %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}> <link rel="stylesheet" href="{{ url.url }}"{% if url.sri %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}>
@ -18,7 +18,7 @@
{% if actor %} {% if actor %}
<div class="logout"> <div class="logout">
<strong>{{ display_actor(actor) }}</strong>{% if show_logout %} &middot; <strong>{{ display_actor(actor) }}</strong>{% if show_logout %} &middot;
<form action="/-/logout" method="post"> <form action="{{ urls.logout() }}" method="post">
<input type="hidden" name="csrftoken" value="{{ csrftoken() }}"> <input type="hidden" name="csrftoken" value="{{ csrftoken() }}">
<button class="button-as-link">Log out</button> <button class="button-as-link">Log out</button>
</form>{% endif %} </form>{% endif %}

View file

@ -4,7 +4,7 @@
{% block nav %} {% block nav %}
<p class="crumbs"> <p class="crumbs">
<a href="/">home</a> <a href="{{ urls.instance() }}">home</a>
</p> </p>
{{ super() }} {{ super() }}
{% endblock %} {% endblock %}

View file

@ -102,6 +102,7 @@ class DatabaseView(DataView):
return ( return (
{ {
"database": database, "database": database,
"path": self.ds.urls.database(database),
"size": db.size, "size": db.size,
"tables": tables, "tables": tables,
"hidden_count": len([t for t in tables if t["hidden"]]), "hidden_count": len([t for t in tables if t["hidden"]]),

View file

@ -375,6 +375,14 @@ The ``datasette.urls`` object contains methods for building URLs to pages within
``datasette.urls.instance()`` ``datasette.urls.instance()``
Returns the URL to the Datasette instance root page. This is usually ``"/"`` Returns the URL to the Datasette instance root page. This is usually ``"/"``
``datasette.urls.path(path)``
Takes a path and returns the full path, taking ``base_url`` into account.
For example, ``datasette.urls.path("-/logout")`` will return the path to the logout page, which will be ``"/-/logout"`` by default or ``/prefix-path/-/logout`` if ``base_url`` is set to ``/prefix-path/``
``datasette.urls.logout()``
Returns the URL to the logout page, usually ``"/-/logout"``.
``datasette.urls.database(database_name)`` ``datasette.urls.database(database_name)``
Returns the URL to a database page, for example ``"/fixtures"`` Returns the URL to a database page, for example ``"/fixtures"``