From 091441a4449beae559a8c0d007376dc85d3aa624 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 19 Oct 2020 22:21:19 -0700 Subject: [PATCH] Fixed remaining places that needed datasette.urls, closes #1025 --- datasette/app.py | 2 +- datasette/cli.py | 6 ++++-- datasette/templates/logout.html | 2 +- datasette/templates/messages_debug.html | 2 +- datasette/views/base.py | 2 +- datasette/views/special.py | 8 ++++---- docs/config.rst | 2 +- tests/test_html.py | 5 ++--- 8 files changed, 15 insertions(+), 14 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index 321e4a92..4277ef73 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -158,7 +158,7 @@ CONFIG_OPTIONS = ( False, "Allow display of template debug information with ?_context=1", ), - ConfigOption("base_url", "/", "Datasette URLs should use this base"), + ConfigOption("base_url", "/", "Datasette URLs should use this base path"), ) DEFAULT_CONFIG = {option.name: option.default for option in CONFIG_OPTIONS} diff --git a/datasette/cli.py b/datasette/cli.py index ab24cb12..f0752b7c 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -457,10 +457,12 @@ def serve( # Start the server if root: - url = "http://{}:{}/-/auth-token?token={}".format(host, port, ds._root_token) + url = "http://{}:{}{}?token={}".format( + host, port, ds.urls.path("-/auth-token"), ds._root_token + ) print(url) else: - url = "http://{}:{}/".format(host, port) + url = "http://{}:{}{}".format(host, port, ds.urls.instance()) if open_browser: webbrowser.open(url) uvicorn.run( diff --git a/datasette/templates/logout.html b/datasette/templates/logout.html index 3c8eb17a..98738679 100644 --- a/datasette/templates/logout.html +++ b/datasette/templates/logout.html @@ -15,7 +15,7 @@

You are logged in as {{ display_actor(actor) }}

-
+
diff --git a/datasette/templates/messages_debug.html b/datasette/templates/messages_debug.html index e83d2a2f..e0ab9a40 100644 --- a/datasette/templates/messages_debug.html +++ b/datasette/templates/messages_debug.html @@ -8,7 +8,7 @@

Set a message:

- +
diff --git a/datasette/views/base.py b/datasette/views/base.py index 92ec03ca..06968e03 100644 --- a/datasette/views/base.py +++ b/datasette/views/base.py @@ -218,7 +218,7 @@ class DataView(BaseView): elif kwargs.get("table"): kwargs["table"] = urllib.parse.unquote_plus(kwargs["table"]) - should_redirect = "/{}-{}".format(name, expected) + should_redirect = self.ds.urls.path("{}-{}".format(name, expected)) if kwargs.get("table"): should_redirect += "/" + urllib.parse.quote_plus(kwargs["table"]) if kwargs.get("pk_path"): diff --git a/datasette/views/special.py b/datasette/views/special.py index 25abb193..28af1e99 100644 --- a/datasette/views/special.py +++ b/datasette/views/special.py @@ -64,7 +64,7 @@ class AuthTokenView(BaseView): raise Forbidden("Root token has already been used") if secrets.compare_digest(token, self.ds._root_token): self.ds._root_token = None - response = Response.redirect("/") + response = Response.redirect(self.ds.urls.instance()) response.set_cookie( "ds_actor", self.ds.sign({"a": {"id": "root"}}, "actor") ) @@ -81,7 +81,7 @@ class LogoutView(BaseView): async def get(self, request): if not request.actor: - return Response.redirect("/") + return Response.redirect(self.ds.urls.instance()) return await self.render( ["logout.html"], request, @@ -89,7 +89,7 @@ class LogoutView(BaseView): ) async def post(self, request): - response = Response.redirect("/") + response = Response.redirect(self.ds.urls.instance()) response.set_cookie("ds_actor", "", expires=0, max_age=0) self.ds.add_message(request, "You are now logged out", self.ds.WARNING) return response @@ -172,4 +172,4 @@ class MessagesDebugView(BaseView): datasette.add_message(request, message, datasette.ERROR) else: datasette.add_message(request, message, getattr(datasette, message_type)) - return Response.redirect("/") + return Response.redirect(self.ds.urls.instance()) diff --git a/docs/config.rst b/docs/config.rst index 6dafd69d..ff39f02a 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -272,7 +272,7 @@ Some examples: base_url ~~~~~~~~ -If you are running Datasette behind a proxy, it may be useful to change the root URL used for the Datasette instance. +If you are running Datasette behind a proxy, it may be useful to change the root path used for the Datasette instance. For example, if you are sending traffic from ``https://www.example.com/tools/datasette/`` through to a proxied Datasette instance you may wish Datasette to use ``/tools/datasette/`` as its root URL. diff --git a/tests/test_html.py b/tests/test_html.py index fb86d9d9..6e18e516 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -1360,8 +1360,6 @@ def test_metadata_sort_desc(app_client): assert list(reversed(expected)) == rows -@pytest.mark.xfail -@pytest.mark.parametrize("base_url", ["/prefix/", "https://example.com/"]) @pytest.mark.parametrize( "path", [ @@ -1373,7 +1371,8 @@ def test_metadata_sort_desc(app_client): "/fixtures/facetable", ], ) -def test_base_url_config(base_url, path): +def test_base_url_config(path): + base_url = "/prefix/" with make_app_client(config={"base_url": base_url}) as client: response = client.get(base_url + path.lstrip("/")) soup = Soup(response.body, "html.parser")