mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Fixed remaining places that needed datasette.urls, closes #1025
This commit is contained in:
parent
0d1763fb2f
commit
091441a444
8 changed files with 15 additions and 14 deletions
|
|
@ -158,7 +158,7 @@ CONFIG_OPTIONS = (
|
||||||
False,
|
False,
|
||||||
"Allow display of template debug information with ?_context=1",
|
"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}
|
DEFAULT_CONFIG = {option.name: option.default for option in CONFIG_OPTIONS}
|
||||||
|
|
|
||||||
|
|
@ -457,10 +457,12 @@ def serve(
|
||||||
|
|
||||||
# Start the server
|
# Start the server
|
||||||
if root:
|
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)
|
print(url)
|
||||||
else:
|
else:
|
||||||
url = "http://{}:{}/".format(host, port)
|
url = "http://{}:{}{}".format(host, port, ds.urls.instance())
|
||||||
if open_browser:
|
if open_browser:
|
||||||
webbrowser.open(url)
|
webbrowser.open(url)
|
||||||
uvicorn.run(
|
uvicorn.run(
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
<p>You are logged in as <strong>{{ display_actor(actor) }}</strong></p>
|
<p>You are logged in as <strong>{{ display_actor(actor) }}</strong></p>
|
||||||
|
|
||||||
<form action="/-/logout" method="post">
|
<form action="{{ urls.logout() }}" method="post">
|
||||||
<div>
|
<div>
|
||||||
<input type="hidden" name="csrftoken" value="{{ csrftoken() }}">
|
<input type="hidden" name="csrftoken" value="{{ csrftoken() }}">
|
||||||
<input type="submit" value="Log out">
|
<input type="submit" value="Log out">
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
<p>Set a message:</p>
|
<p>Set a message:</p>
|
||||||
|
|
||||||
<form action="/-/messages" method="post">
|
<form action="{{ urls.path('-/messages') }}" method="post">
|
||||||
<div>
|
<div>
|
||||||
<input type="text" name="message" style="width: 40%">
|
<input type="text" name="message" style="width: 40%">
|
||||||
<div class="select-wrapper">
|
<div class="select-wrapper">
|
||||||
|
|
|
||||||
|
|
@ -218,7 +218,7 @@ class DataView(BaseView):
|
||||||
elif kwargs.get("table"):
|
elif kwargs.get("table"):
|
||||||
kwargs["table"] = urllib.parse.unquote_plus(kwargs["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"):
|
if kwargs.get("table"):
|
||||||
should_redirect += "/" + urllib.parse.quote_plus(kwargs["table"])
|
should_redirect += "/" + urllib.parse.quote_plus(kwargs["table"])
|
||||||
if kwargs.get("pk_path"):
|
if kwargs.get("pk_path"):
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ class AuthTokenView(BaseView):
|
||||||
raise Forbidden("Root token has already been used")
|
raise Forbidden("Root token has already been used")
|
||||||
if secrets.compare_digest(token, self.ds._root_token):
|
if secrets.compare_digest(token, self.ds._root_token):
|
||||||
self.ds._root_token = None
|
self.ds._root_token = None
|
||||||
response = Response.redirect("/")
|
response = Response.redirect(self.ds.urls.instance())
|
||||||
response.set_cookie(
|
response.set_cookie(
|
||||||
"ds_actor", self.ds.sign({"a": {"id": "root"}}, "actor")
|
"ds_actor", self.ds.sign({"a": {"id": "root"}}, "actor")
|
||||||
)
|
)
|
||||||
|
|
@ -81,7 +81,7 @@ class LogoutView(BaseView):
|
||||||
|
|
||||||
async def get(self, request):
|
async def get(self, request):
|
||||||
if not request.actor:
|
if not request.actor:
|
||||||
return Response.redirect("/")
|
return Response.redirect(self.ds.urls.instance())
|
||||||
return await self.render(
|
return await self.render(
|
||||||
["logout.html"],
|
["logout.html"],
|
||||||
request,
|
request,
|
||||||
|
|
@ -89,7 +89,7 @@ class LogoutView(BaseView):
|
||||||
)
|
)
|
||||||
|
|
||||||
async def post(self, request):
|
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)
|
response.set_cookie("ds_actor", "", expires=0, max_age=0)
|
||||||
self.ds.add_message(request, "You are now logged out", self.ds.WARNING)
|
self.ds.add_message(request, "You are now logged out", self.ds.WARNING)
|
||||||
return response
|
return response
|
||||||
|
|
@ -172,4 +172,4 @@ class MessagesDebugView(BaseView):
|
||||||
datasette.add_message(request, message, datasette.ERROR)
|
datasette.add_message(request, message, datasette.ERROR)
|
||||||
else:
|
else:
|
||||||
datasette.add_message(request, message, getattr(datasette, message_type))
|
datasette.add_message(request, message, getattr(datasette, message_type))
|
||||||
return Response.redirect("/")
|
return Response.redirect(self.ds.urls.instance())
|
||||||
|
|
|
||||||
|
|
@ -272,7 +272,7 @@ Some examples:
|
||||||
base_url
|
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.
|
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.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1360,8 +1360,6 @@ def test_metadata_sort_desc(app_client):
|
||||||
assert list(reversed(expected)) == rows
|
assert list(reversed(expected)) == rows
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.xfail
|
|
||||||
@pytest.mark.parametrize("base_url", ["/prefix/", "https://example.com/"])
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"path",
|
"path",
|
||||||
[
|
[
|
||||||
|
|
@ -1373,7 +1371,8 @@ def test_metadata_sort_desc(app_client):
|
||||||
"/fixtures/facetable",
|
"/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:
|
with make_app_client(config={"base_url": base_url}) as client:
|
||||||
response = client.get(base_url + path.lstrip("/"))
|
response = client.get(base_url + path.lstrip("/"))
|
||||||
soup = Soup(response.body, "html.parser")
|
soup = Soup(response.body, "html.parser")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue