mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
parent
b9c2b1cfc8
commit
798f075ef9
3 changed files with 1 additions and 56 deletions
|
|
@ -731,26 +731,6 @@ def module_from_path(path, name):
|
||||||
return mod
|
return mod
|
||||||
|
|
||||||
|
|
||||||
async def resolve_table_and_format(
|
|
||||||
table_and_format, table_exists, allowed_formats=None
|
|
||||||
):
|
|
||||||
if allowed_formats is None:
|
|
||||||
allowed_formats = []
|
|
||||||
if "." in table_and_format:
|
|
||||||
# Check if a table exists with this exact name
|
|
||||||
it_exists = await table_exists(table_and_format)
|
|
||||||
if it_exists:
|
|
||||||
return table_and_format, None
|
|
||||||
|
|
||||||
# Check if table ends with a known format
|
|
||||||
formats = list(allowed_formats) + ["csv", "jsono"]
|
|
||||||
for _format in formats:
|
|
||||||
if table_and_format.endswith(f".{_format}"):
|
|
||||||
table = table_and_format[: -(len(_format) + 1)]
|
|
||||||
return table, _format
|
|
||||||
return table_and_format, None
|
|
||||||
|
|
||||||
|
|
||||||
def path_with_format(
|
def path_with_format(
|
||||||
*, request=None, path=None, format=None, extra_qs=None, replace_format=None
|
*, request=None, path=None, format=None, extra_qs=None, replace_format=None
|
||||||
):
|
):
|
||||||
|
|
|
||||||
|
|
@ -19,12 +19,10 @@ from datasette.utils import (
|
||||||
LimitedWriter,
|
LimitedWriter,
|
||||||
call_with_supported_arguments,
|
call_with_supported_arguments,
|
||||||
tilde_decode,
|
tilde_decode,
|
||||||
tilde_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,
|
||||||
path_with_format,
|
path_with_format,
|
||||||
resolve_table_and_format,
|
|
||||||
sqlite3,
|
sqlite3,
|
||||||
HASH_LENGTH,
|
HASH_LENGTH,
|
||||||
)
|
)
|
||||||
|
|
@ -372,18 +370,10 @@ class DataView(BaseView):
|
||||||
|
|
||||||
return AsgiStream(stream_fn, headers=headers, content_type=content_type)
|
return AsgiStream(stream_fn, headers=headers, content_type=content_type)
|
||||||
|
|
||||||
def get_format(self, request):
|
|
||||||
# Format is the bit from the path following the ., if one exists
|
|
||||||
last_path_component = request.path.split("/")[-1]
|
|
||||||
if "." in last_path_component:
|
|
||||||
return last_path_component.split(".")[-1]
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def get(self, request):
|
async def get(self, request):
|
||||||
db_name = request.url_vars["database"]
|
db_name = request.url_vars["database"]
|
||||||
database = tilde_decode(db_name)
|
database = tilde_decode(db_name)
|
||||||
_format = self.get_format(request)
|
_format = request.url_vars["format"]
|
||||||
data_kwargs = {}
|
data_kwargs = {}
|
||||||
|
|
||||||
if _format == "csv":
|
if _format == "csv":
|
||||||
|
|
|
||||||
|
|
@ -351,31 +351,6 @@ def test_compound_keys_after_sql():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def table_exists(table):
|
|
||||||
return table == "exists.csv"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"table_and_format,expected_table,expected_format",
|
|
||||||
[
|
|
||||||
("blah", "blah", None),
|
|
||||||
("blah.csv", "blah", "csv"),
|
|
||||||
("blah.json", "blah", "json"),
|
|
||||||
("blah.baz", "blah.baz", None),
|
|
||||||
("exists.csv", "exists.csv", None),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
async def test_resolve_table_and_format(
|
|
||||||
table_and_format, expected_table, expected_format
|
|
||||||
):
|
|
||||||
actual_table, actual_format = await utils.resolve_table_and_format(
|
|
||||||
table_and_format, table_exists, ["json"]
|
|
||||||
)
|
|
||||||
assert expected_table == actual_table
|
|
||||||
assert expected_format == actual_format
|
|
||||||
|
|
||||||
|
|
||||||
def test_table_columns():
|
def test_table_columns():
|
||||||
conn = sqlite3.connect(":memory:")
|
conn = sqlite3.connect(":memory:")
|
||||||
conn.executescript(
|
conn.executescript(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue