mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Use dash encoding for table names, refs #1439
This commit is contained in:
parent
4976494160
commit
b760264ae6
5 changed files with 18 additions and 18 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
from .utils import path_with_format, HASH_LENGTH, PrefixedUrlString
|
from .utils import dash_encode, path_with_format, HASH_LENGTH, PrefixedUrlString
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -38,13 +38,13 @@ class Urls:
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def table(self, database, table, format=None):
|
def table(self, database, table, format=None):
|
||||||
path = f"{self.database(database)}/{urllib.parse.quote_plus(table)}"
|
path = f"{self.database(database)}/{dash_encode(table)}"
|
||||||
if format is not None:
|
if format is not None:
|
||||||
path = path_with_format(path=path, format=format)
|
path = path_with_format(path=path, format=format)
|
||||||
return PrefixedUrlString(path)
|
return PrefixedUrlString(path)
|
||||||
|
|
||||||
def query(self, database, query, format=None):
|
def query(self, database, query, format=None):
|
||||||
path = f"{self.database(database)}/{urllib.parse.quote_plus(query)}"
|
path = f"{self.database(database)}/{dash_encode(query)}"
|
||||||
if format is not None:
|
if format is not None:
|
||||||
path = path_with_format(path=path, format=format)
|
path = path_with_format(path=path, format=format)
|
||||||
return PrefixedUrlString(path)
|
return PrefixedUrlString(path)
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ from datasette.utils import (
|
||||||
InvalidSql,
|
InvalidSql,
|
||||||
LimitedWriter,
|
LimitedWriter,
|
||||||
call_with_supported_arguments,
|
call_with_supported_arguments,
|
||||||
|
dash_decode,
|
||||||
|
dash_encode,
|
||||||
path_from_row_pks,
|
path_from_row_pks,
|
||||||
path_with_added_args,
|
path_with_added_args,
|
||||||
path_with_removed_args,
|
path_with_removed_args,
|
||||||
|
|
@ -233,9 +235,7 @@ class DataView(BaseView):
|
||||||
return await db.table_exists(t)
|
return await db.table_exists(t)
|
||||||
|
|
||||||
table, _format = await resolve_table_and_format(
|
table, _format = await resolve_table_and_format(
|
||||||
table_and_format=urllib.parse.unquote_plus(
|
table_and_format=dash_decode(kwargs["table_and_format"]),
|
||||||
kwargs["table_and_format"]
|
|
||||||
),
|
|
||||||
table_exists=async_table_exists,
|
table_exists=async_table_exists,
|
||||||
allowed_formats=self.ds.renderers.keys(),
|
allowed_formats=self.ds.renderers.keys(),
|
||||||
)
|
)
|
||||||
|
|
@ -243,11 +243,11 @@ class DataView(BaseView):
|
||||||
if _format:
|
if _format:
|
||||||
kwargs["as_format"] = f".{_format}"
|
kwargs["as_format"] = f".{_format}"
|
||||||
elif kwargs.get("table"):
|
elif kwargs.get("table"):
|
||||||
kwargs["table"] = urllib.parse.unquote_plus(kwargs["table"])
|
kwargs["table"] = dash_decode(kwargs["table"])
|
||||||
|
|
||||||
should_redirect = self.ds.urls.path(f"{name}-{expected}")
|
should_redirect = self.ds.urls.path(f"{name}-{expected}")
|
||||||
if kwargs.get("table"):
|
if kwargs.get("table"):
|
||||||
should_redirect += "/" + urllib.parse.quote_plus(kwargs["table"])
|
should_redirect += "/" + dash_encode(kwargs["table"])
|
||||||
if kwargs.get("pk_path"):
|
if kwargs.get("pk_path"):
|
||||||
should_redirect += "/" + kwargs["pk_path"]
|
should_redirect += "/" + kwargs["pk_path"]
|
||||||
if kwargs.get("as_format"):
|
if kwargs.get("as_format"):
|
||||||
|
|
@ -467,7 +467,7 @@ class DataView(BaseView):
|
||||||
return await db.table_exists(t)
|
return await db.table_exists(t)
|
||||||
|
|
||||||
table, _ext_format = await resolve_table_and_format(
|
table, _ext_format = await resolve_table_and_format(
|
||||||
table_and_format=urllib.parse.unquote_plus(args["table_and_format"]),
|
table_and_format=dash_decode(args["table_and_format"]),
|
||||||
table_exists=async_table_exists,
|
table_exists=async_table_exists,
|
||||||
allowed_formats=self.ds.renderers.keys(),
|
allowed_formats=self.ds.renderers.keys(),
|
||||||
)
|
)
|
||||||
|
|
@ -475,7 +475,7 @@ class DataView(BaseView):
|
||||||
args["table"] = table
|
args["table"] = table
|
||||||
del args["table_and_format"]
|
del args["table_and_format"]
|
||||||
elif "table" in args:
|
elif "table" in args:
|
||||||
args["table"] = urllib.parse.unquote_plus(args["table"])
|
args["table"] = dash_decode(args["table"])
|
||||||
return _format, args
|
return _format, args
|
||||||
|
|
||||||
async def view_get(self, request, database, hash, correct_hash_provided, **kwargs):
|
async def view_get(self, request, database, hash, correct_hash_provided, **kwargs):
|
||||||
|
|
|
||||||
|
|
@ -143,7 +143,7 @@ class RowTableShared(DataView):
|
||||||
'<a href="{base_url}{database}/{table}/{flat_pks_quoted}">{flat_pks}</a>'.format(
|
'<a href="{base_url}{database}/{table}/{flat_pks_quoted}">{flat_pks}</a>'.format(
|
||||||
base_url=base_url,
|
base_url=base_url,
|
||||||
database=database,
|
database=database,
|
||||||
table=urllib.parse.quote_plus(table),
|
table=dash_encode(table),
|
||||||
flat_pks=str(markupsafe.escape(pk_path)),
|
flat_pks=str(markupsafe.escape(pk_path)),
|
||||||
flat_pks_quoted=path_from_row_pks(row, pks, not pks),
|
flat_pks_quoted=path_from_row_pks(row, pks, not pks),
|
||||||
)
|
)
|
||||||
|
|
@ -200,8 +200,8 @@ class RowTableShared(DataView):
|
||||||
link_template.format(
|
link_template.format(
|
||||||
database=database,
|
database=database,
|
||||||
base_url=base_url,
|
base_url=base_url,
|
||||||
table=urllib.parse.quote_plus(other_table),
|
table=dash_encode(other_table),
|
||||||
link_id=urllib.parse.quote_plus(str(value)),
|
link_id=dash_encode(str(value)),
|
||||||
id=str(markupsafe.escape(value)),
|
id=str(markupsafe.escape(value)),
|
||||||
label=str(markupsafe.escape(label)) or "-",
|
label=str(markupsafe.escape(label)) or "-",
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -140,7 +140,7 @@ def test_database_page(app_client):
|
||||||
assert queries_ul is not None
|
assert queries_ul is not None
|
||||||
assert [
|
assert [
|
||||||
(
|
(
|
||||||
"/fixtures/%F0%9D%90%9C%F0%9D%90%A2%F0%9D%90%AD%F0%9D%90%A2%F0%9D%90%9E%F0%9D%90%AC",
|
"/fixtures/-F0-9D-90-9C-F0-9D-90-A2-F0-9D-90-AD-F0-9D-90-A2-F0-9D-90-9E-F0-9D-90-AC",
|
||||||
"𝐜𝐢𝐭𝐢𝐞𝐬",
|
"𝐜𝐢𝐭𝐢𝐞𝐬",
|
||||||
),
|
),
|
||||||
("/fixtures/from_async_hook", "from_async_hook"),
|
("/fixtures/from_async_hook", "from_async_hook"),
|
||||||
|
|
@ -193,11 +193,11 @@ def test_row_redirects_with_url_hash(app_client_with_hash):
|
||||||
|
|
||||||
|
|
||||||
def test_row_strange_table_name_with_url_hash(app_client_with_hash):
|
def test_row_strange_table_name_with_url_hash(app_client_with_hash):
|
||||||
response = app_client_with_hash.get("/fixtures/table%2Fwith%2Fslashes.csv/3")
|
response = app_client_with_hash.get("/fixtures/table-2Fwith-2Fslashes-2Ecsv/3")
|
||||||
assert response.status == 302
|
assert response.status == 302
|
||||||
assert response.headers["Location"].endswith("/table%2Fwith%2Fslashes.csv/3")
|
assert response.headers["Location"].endswith("/table-2Fwith-2Fslashes-2Ecsv/3")
|
||||||
response = app_client_with_hash.get(
|
response = app_client_with_hash.get(
|
||||||
"/fixtures/table%2Fwith%2Fslashes.csv/3", follow_redirects=True
|
"/fixtures/table-2Fwith-2Fslashes-2Ecsv/3", follow_redirects=True
|
||||||
)
|
)
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,7 @@ def test_database(ds, base_url, format, expected):
|
||||||
("/", "name", None, "/_memory/name"),
|
("/", "name", None, "/_memory/name"),
|
||||||
("/prefix/", "name", None, "/prefix/_memory/name"),
|
("/prefix/", "name", None, "/prefix/_memory/name"),
|
||||||
("/", "name", "json", "/_memory/name.json"),
|
("/", "name", "json", "/_memory/name.json"),
|
||||||
("/", "name.json", "json", "/_memory/name.json?_format=json"),
|
("/", "name.json", "json", "/_memory/name-2Ejson.json"),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_table_and_query(ds, base_url, name, format, expected):
|
def test_table_and_query(ds, base_url, name, format, expected):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue