Consistent treatment of format in route capturing, refs #1667

Also refs #1660
This commit is contained in:
Simon Willison 2022-03-19 13:29:10 -07:00
commit b9c2b1cfc8
3 changed files with 36 additions and 30 deletions

View file

@ -985,10 +985,7 @@ class Datasette:
def add_route(view, regex): def add_route(view, regex):
routes.append((regex, view)) routes.append((regex, view))
# Generate a regex snippet to match all registered renderer file extensions add_route(IndexView.as_view(self), r"/(\.(?P<format>jsono?))?$")
renderer_regex = "|".join(r"\." + key for key in self.renderers.keys())
add_route(IndexView.as_view(self), r"/(?P<format>(\.jsono?)?$)")
# TODO: /favicon.ico and /-/static/ deserve far-future cache expires # TODO: /favicon.ico and /-/static/ deserve far-future cache expires
add_route(favicon, "/favicon.ico") add_route(favicon, "/favicon.ico")
@ -1020,21 +1017,21 @@ class Datasette:
) )
add_route( add_route(
JsonDataView.as_view(self, "metadata.json", lambda: self.metadata()), JsonDataView.as_view(self, "metadata.json", lambda: self.metadata()),
r"/-/metadata(?P<format>(\.json)?)$", r"/-/metadata(\.(?P<format>json))?$",
) )
add_route( add_route(
JsonDataView.as_view(self, "versions.json", self._versions), JsonDataView.as_view(self, "versions.json", self._versions),
r"/-/versions(?P<format>(\.json)?)$", r"/-/versions(\.(?P<format>json))?$",
) )
add_route( add_route(
JsonDataView.as_view( JsonDataView.as_view(
self, "plugins.json", self._plugins, needs_request=True self, "plugins.json", self._plugins, needs_request=True
), ),
r"/-/plugins(?P<format>(\.json)?)$", r"/-/plugins(\.(?P<format>json))?$",
) )
add_route( add_route(
JsonDataView.as_view(self, "settings.json", lambda: self._settings), JsonDataView.as_view(self, "settings.json", lambda: self._settings),
r"/-/settings(?P<format>(\.json)?)$", r"/-/settings(\.(?P<format>json))?$",
) )
add_route( add_route(
permanent_redirect("/-/settings.json"), permanent_redirect("/-/settings.json"),
@ -1046,15 +1043,15 @@ class Datasette:
) )
add_route( add_route(
JsonDataView.as_view(self, "threads.json", self._threads), JsonDataView.as_view(self, "threads.json", self._threads),
r"/-/threads(?P<format>(\.json)?)$", r"/-/threads(\.(?P<format>json))?$",
) )
add_route( add_route(
JsonDataView.as_view(self, "databases.json", self._connected_databases), JsonDataView.as_view(self, "databases.json", self._connected_databases),
r"/-/databases(?P<format>(\.json)?)$", r"/-/databases(\.(?P<format>json))?$",
) )
add_route( add_route(
JsonDataView.as_view(self, "actor.json", self._actor, needs_request=True), JsonDataView.as_view(self, "actor.json", self._actor, needs_request=True),
r"/-/actor(?P<format>(\.json)?)$", r"/-/actor(\.(?P<format>json))?$",
) )
add_route( add_route(
AuthTokenView.as_view(self), AuthTokenView.as_view(self),
@ -1080,20 +1077,17 @@ class Datasette:
PatternPortfolioView.as_view(self), PatternPortfolioView.as_view(self),
r"/-/patterns$", r"/-/patterns$",
) )
add_route(DatabaseDownload.as_view(self), r"/(?P<database>[^/]+?)\.db$") add_route(DatabaseDownload.as_view(self), r"/(?P<database>[^\/\.]+)\.db$")
add_route( add_route(
DatabaseView.as_view(self), DatabaseView.as_view(self), r"/(?P<database>[^\/\.]+)(\.(?P<format>\w+))?$"
r"/(?P<database>[^/]+?)(?P<format>" + renderer_regex + r"|.jsono|\.csv)?$",
) )
add_route( add_route(
TableView.as_view(self), TableView.as_view(self),
r"/(?P<database>[^/]+)/(?P<table>[^\/\.]+)(\.[a-zA-Z0-9_]+)?$", r"/(?P<database>[^\/\.]+)/(?P<table>[^\/\.]+)(\.(?P<format>\w+))?$",
) )
add_route( add_route(
RowView.as_view(self), RowView.as_view(self),
r"/(?P<database>[^/]+)/(?P<table>[^/]+?)/(?P<pks>[^/]+?)(?P<format>" r"/(?P<database>[^\/\.]+)/(?P<table>[^/]+?)/(?P<pks>[^/]+?)(\.(?P<format>\w+))?$",
+ renderer_regex
+ r")?$",
) )
return [ return [
# Compile any strings to regular expressions # Compile any strings to regular expressions

View file

@ -629,8 +629,8 @@ def test_old_memory_urls_redirect(app_client_no_files, path, expected_redirect):
def test_database_page_for_database_with_dot_in_name(app_client_with_dot): def test_database_page_for_database_with_dot_in_name(app_client_with_dot):
response = app_client_with_dot.get("/fixtures.dot.json") response = app_client_with_dot.get("/fixtures~2Edot.json")
assert 200 == response.status assert response.status == 200
def test_custom_sql(app_client): def test_custom_sql(app_client):

View file

@ -12,14 +12,26 @@ def routes():
@pytest.mark.parametrize( @pytest.mark.parametrize(
"path,expected_class,expected_matches", "path,expected_class,expected_matches",
( (
("/", "IndexView", {"format": ""}), ("/", "IndexView", {"format": None}),
("/foo", "DatabaseView", {"format": None, "database": "foo"}), ("/foo", "DatabaseView", {"format": None, "database": "foo"}),
("/foo.csv", "DatabaseView", {"format": ".csv", "database": "foo"}), ("/foo.csv", "DatabaseView", {"format": "csv", "database": "foo"}),
("/foo.json", "DatabaseView", {"format": ".json", "database": "foo"}), ("/foo.json", "DatabaseView", {"format": "json", "database": "foo"}),
("/foo.humbug", "DatabaseView", {"format": None, "database": "foo.humbug"}), ("/foo.humbug", "DatabaseView", {"format": "humbug", "database": "foo"}),
("/foo/humbug", "TableView", {"database": "foo", "table": "humbug"}), (
("/foo/humbug.json", "TableView", {"database": "foo", "table": "humbug"}), "/foo/humbug",
("/foo/humbug.blah", "TableView", {"database": "foo", "table": "humbug"}), "TableView",
{"database": "foo", "table": "humbug", "format": None},
),
(
"/foo/humbug.json",
"TableView",
{"database": "foo", "table": "humbug", "format": "json"},
),
(
"/foo/humbug.blah",
"TableView",
{"database": "foo", "table": "humbug", "format": "blah"},
),
( (
"/foo/humbug/1", "/foo/humbug/1",
"RowView", "RowView",
@ -28,10 +40,10 @@ def routes():
( (
"/foo/humbug/1.json", "/foo/humbug/1.json",
"RowView", "RowView",
{"format": ".json", "database": "foo", "pks": "1", "table": "humbug"}, {"format": "json", "database": "foo", "pks": "1", "table": "humbug"},
), ),
("/-/metadata.json", "JsonDataView", {"format": ".json"}), ("/-/metadata.json", "JsonDataView", {"format": "json"}),
("/-/metadata", "JsonDataView", {"format": ""}), ("/-/metadata", "JsonDataView", {"format": None}),
), ),
) )
def test_routes(routes, path, expected_class, expected_matches): def test_routes(routes, path, expected_class, expected_matches):