Skip to content

Commit

Permalink
Rename route match groups for consistency, refs #1667, #1660
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 19, 2022
1 parent 764738d commit 6141938
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 40 deletions.
28 changes: 12 additions & 16 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ def add_route(view, regex):
# Generate a regex snippet to match all registered renderer file extensions
renderer_regex = "|".join(r"\." + key for key in self.renderers.keys())

add_route(IndexView.as_view(self), r"/(?P<as_format>(\.jsono?)?$)")
add_route(IndexView.as_view(self), r"/(?P<format>(\.jsono?)?$)")
# TODO: /favicon.ico and /-/static/ deserve far-future cache expires
add_route(favicon, "/favicon.ico")

Expand Down Expand Up @@ -1020,21 +1020,21 @@ def add_route(view, regex):
)
add_route(
JsonDataView.as_view(self, "metadata.json", lambda: self.metadata()),
r"/-/metadata(?P<as_format>(\.json)?)$",
r"/-/metadata(?P<format>(\.json)?)$",
)
add_route(
JsonDataView.as_view(self, "versions.json", self._versions),
r"/-/versions(?P<as_format>(\.json)?)$",
r"/-/versions(?P<format>(\.json)?)$",
)
add_route(
JsonDataView.as_view(
self, "plugins.json", self._plugins, needs_request=True
),
r"/-/plugins(?P<as_format>(\.json)?)$",
r"/-/plugins(?P<format>(\.json)?)$",
)
add_route(
JsonDataView.as_view(self, "settings.json", lambda: self._settings),
r"/-/settings(?P<as_format>(\.json)?)$",
r"/-/settings(?P<format>(\.json)?)$",
)
add_route(
permanent_redirect("/-/settings.json"),
Expand All @@ -1046,15 +1046,15 @@ def add_route(view, regex):
)
add_route(
JsonDataView.as_view(self, "threads.json", self._threads),
r"/-/threads(?P<as_format>(\.json)?)$",
r"/-/threads(?P<format>(\.json)?)$",
)
add_route(
JsonDataView.as_view(self, "databases.json", self._connected_databases),
r"/-/databases(?P<as_format>(\.json)?)$",
r"/-/databases(?P<format>(\.json)?)$",
)
add_route(
JsonDataView.as_view(self, "actor.json", self._actor, needs_request=True),
r"/-/actor(?P<as_format>(\.json)?)$",
r"/-/actor(?P<format>(\.json)?)$",
)
add_route(
AuthTokenView.as_view(self),
Expand All @@ -1080,22 +1080,18 @@ def add_route(view, regex):
PatternPortfolioView.as_view(self),
r"/-/patterns$",
)
add_route(
DatabaseDownload.as_view(self), r"/(?P<db_name>[^/]+?)(?P<as_db>\.db)$"
)
add_route(DatabaseDownload.as_view(self), r"/(?P<database>[^/]+?)\.db$")
add_route(
DatabaseView.as_view(self),
r"/(?P<db_name>[^/]+?)(?P<as_format>"
+ renderer_regex
+ r"|.jsono|\.csv)?$",
r"/(?P<database>[^/]+?)(?P<format>" + renderer_regex + r"|.jsono|\.csv)?$",
)
add_route(
TableView.as_view(self),
r"/(?P<db_name>[^/]+)/(?P<table>[^\/\.]+)(\.[a-zA-Z0-9_]+)?$",
r"/(?P<database>[^/]+)/(?P<table>[^\/\.]+)(\.[a-zA-Z0-9_]+)?$",
)
add_route(
RowView.as_view(self),
r"/(?P<db_name>[^/]+)/(?P<table>[^/]+?)/(?P<pk_path>[^/]+?)(?P<as_format>"
r"/(?P<database>[^/]+)/(?P<table>[^/]+?)/(?P<pks>[^/]+?)(?P<format>"
+ renderer_regex
+ r")?$",
)
Expand Down
4 changes: 2 additions & 2 deletions datasette/blob_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ async def render_blob(datasette, database, rows, columns, request, table, view_n
filename_bits = []
if table:
filename_bits.append(to_css_class(table))
if "pk_path" in request.url_vars:
filename_bits.append(request.url_vars["pk_path"])
if "pks" in request.url_vars:
filename_bits.append(request.url_vars["pks"])
filename_bits.append(to_css_class(blob_column))
if blob_hash:
filename_bits.append(blob_hash[:6])
Expand Down
2 changes: 1 addition & 1 deletion datasette/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def get_format(self, request):
return None

async def get(self, request):
db_name = request.url_vars["db_name"]
db_name = request.url_vars["database"]
database = tilde_decode(db_name)
_format = self.get_format(request)
data_kwargs = {}
Expand Down
6 changes: 3 additions & 3 deletions datasette/views/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DatabaseView(DataView):
name = "database"

async def data(self, request, default_labels=False, _size=None):
database = tilde_decode(request.url_vars["db_name"])
database = tilde_decode(request.url_vars["database"])
await self.check_permissions(
request,
[
Expand Down Expand Up @@ -162,7 +162,7 @@ class DatabaseDownload(DataView):
name = "database_download"

async def get(self, request):
database = tilde_decode(request.url_vars["db_name"])
database = tilde_decode(request.url_vars["database"])
await self.check_permissions(
request,
[
Expand Down Expand Up @@ -205,7 +205,7 @@ async def data(
named_parameters=None,
write=False,
):
database = tilde_decode(request.url_vars["db_name"])
database = tilde_decode(request.url_vars["database"])
params = {key: request.args.get(key) for key in request.args}
if "sql" in params:
params.pop("sql")
Expand Down
2 changes: 1 addition & 1 deletion datasette/views/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class IndexView(BaseView):
name = "index"

async def get(self, request):
as_format = request.url_vars["as_format"]
as_format = request.url_vars["format"]
await self.check_permission(request, "view-instance")
databases = []
for name, db in self.ds.databases.items():
Expand Down
2 changes: 1 addition & 1 deletion datasette/views/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, datasette, filename, data_callback, needs_request=False):
self.needs_request = needs_request

async def get(self, request):
as_format = request.url_vars["as_format"]
as_format = request.url_vars["format"]
await self.check_permission(request, "view-instance")
if self.needs_request:
data = self.data_callback(request)
Expand Down
8 changes: 4 additions & 4 deletions datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class TableView(RowTableShared):
name = "table"

async def post(self, request):
db_name = tilde_decode(request.url_vars["db_name"])
db_name = tilde_decode(request.url_vars["database"])
table = tilde_decode(request.url_vars["table"])
# Handle POST to a canned query
canned_query = await self.ds.get_canned_query(db_name, table, request.actor)
Expand Down Expand Up @@ -327,7 +327,7 @@ async def data(
_next=None,
_size=None,
):
database = tilde_decode(request.url_vars["db_name"])
database = tilde_decode(request.url_vars["database"])
table = tilde_decode(request.url_vars["table"])
try:
db = self.ds.databases[database]
Expand Down Expand Up @@ -938,7 +938,7 @@ class RowView(RowTableShared):
name = "row"

async def data(self, request, default_labels=False):
database = tilde_decode(request.url_vars["db_name"])
database = tilde_decode(request.url_vars["database"])
table = tilde_decode(request.url_vars["table"])
await self.check_permissions(
request,
Expand All @@ -948,7 +948,7 @@ async def data(self, request, default_labels=False):
"view-instance",
],
)
pk_values = urlsafe_components(request.url_vars["pk_path"])
pk_values = urlsafe_components(request.url_vars["pks"])
db = self.ds.databases[database]
sql, params, pks = await _sql_params_pks(db, table, pk_values)
results = await db.execute(sql, params, truncate=True)
Expand Down
24 changes: 12 additions & 12 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ def routes():
@pytest.mark.parametrize(
"path,expected_class,expected_matches",
(
("/", "IndexView", {"as_format": ""}),
("/foo", "DatabaseView", {"as_format": None, "db_name": "foo"}),
("/foo.csv", "DatabaseView", {"as_format": ".csv", "db_name": "foo"}),
("/foo.json", "DatabaseView", {"as_format": ".json", "db_name": "foo"}),
("/foo.humbug", "DatabaseView", {"as_format": None, "db_name": "foo.humbug"}),
("/foo/humbug", "TableView", {"db_name": "foo", "table": "humbug"}),
("/foo/humbug.json", "TableView", {"db_name": "foo", "table": "humbug"}),
("/foo/humbug.blah", "TableView", {"db_name": "foo", "table": "humbug"}),
("/", "IndexView", {"format": ""}),
("/foo", "DatabaseView", {"format": None, "database": "foo"}),
("/foo.csv", "DatabaseView", {"format": ".csv", "database": "foo"}),
("/foo.json", "DatabaseView", {"format": ".json", "database": "foo"}),
("/foo.humbug", "DatabaseView", {"format": None, "database": "foo.humbug"}),
("/foo/humbug", "TableView", {"database": "foo", "table": "humbug"}),
("/foo/humbug.json", "TableView", {"database": "foo", "table": "humbug"}),
("/foo/humbug.blah", "TableView", {"database": "foo", "table": "humbug"}),
(
"/foo/humbug/1",
"RowView",
{"as_format": None, "db_name": "foo", "pk_path": "1", "table": "humbug"},
{"format": None, "database": "foo", "pks": "1", "table": "humbug"},
),
(
"/foo/humbug/1.json",
"RowView",
{"as_format": ".json", "db_name": "foo", "pk_path": "1", "table": "humbug"},
{"format": ".json", "database": "foo", "pks": "1", "table": "humbug"},
),
("/-/metadata.json", "JsonDataView", {"as_format": ".json"}),
("/-/metadata", "JsonDataView", {"as_format": ""}),
("/-/metadata.json", "JsonDataView", {"format": ".json"}),
("/-/metadata", "JsonDataView", {"format": ""}),
),
)
def test_routes(routes, path, expected_class, expected_matches):
Expand Down

0 comments on commit 6141938

Please sign in to comment.