Fixed internal links to respect db.route, refs #1668

This commit is contained in:
Simon Willison 2022-03-19 17:31:23 -07:00
commit cdbae2b93f
3 changed files with 17 additions and 13 deletions

View file

@ -28,7 +28,8 @@ class Urls:
return self.path("-/logout") return self.path("-/logout")
def database(self, database, format=None): def database(self, database, format=None):
return self.path(tilde_encode(database), format=format) db = self.ds.get_database(database)
return self.path(tilde_encode(db.route), format=format)
def table(self, database, table, format=None): def table(self, database, table, format=None):
path = f"{self.database(database)}/{tilde_encode(table)}" path = f"{self.database(database)}/{tilde_encode(table)}"

View file

@ -141,10 +141,9 @@ class RowTableShared(DataView):
"is_special_link_column": is_special_link_column, "is_special_link_column": is_special_link_column,
"raw": pk_path, "raw": pk_path,
"value": markupsafe.Markup( "value": markupsafe.Markup(
'<a href="{base_url}{database}/{table}/{flat_pks_quoted}">{flat_pks}</a>'.format( '<a href="{table_path}/{flat_pks_quoted}">{flat_pks}</a>'.format(
base_url=base_url, base_url=base_url,
database=database, table_path=self.ds.urls.table(database, table),
table=tilde_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),
) )

View file

@ -61,7 +61,7 @@ async def ds_with_route():
ds = Datasette() ds = Datasette()
ds.remove_database("_memory") ds.remove_database("_memory")
db = Database(ds, is_memory=True, memory_name="route-name-db") db = Database(ds, is_memory=True, memory_name="route-name-db")
ds.add_database(db, name="name", route="route-name") ds.add_database(db, name="original-name", route="custom-route-name")
await db.execute_write_script( await db.execute_write_script(
""" """
create table if not exists t (id integer primary key); create table if not exists t (id integer primary key);
@ -75,8 +75,8 @@ async def ds_with_route():
async def test_db_with_route_databases(ds_with_route): async def test_db_with_route_databases(ds_with_route):
response = await ds_with_route.client.get("/-/databases.json") response = await ds_with_route.client.get("/-/databases.json")
assert response.json()[0] == { assert response.json()[0] == {
"name": "name", "name": "original-name",
"route": "route-name", "route": "custom-route-name",
"path": None, "path": None,
"size": 0, "size": 0,
"is_mutable": True, "is_mutable": True,
@ -90,12 +90,12 @@ async def test_db_with_route_databases(ds_with_route):
"path,expected_status", "path,expected_status",
( (
("/", 200), ("/", 200),
("/name", 404), ("/original-name", 404),
("/name/t", 404), ("/original-name/t", 404),
("/name/t/1", 404), ("/original-name/t/1", 404),
("/route-name", 200), ("/custom-route-name", 200),
("/route-name/t", 200), ("/custom-route-name/t", 200),
("/route-name/t/1", 200), ("/custom-route-name/t/1", 200),
), ),
) )
async def test_db_with_route_that_does_not_match_name( async def test_db_with_route_that_does_not_match_name(
@ -103,3 +103,7 @@ async def test_db_with_route_that_does_not_match_name(
): ):
response = await ds_with_route.client.get(path) response = await ds_with_route.client.get(path)
assert response.status_code == expected_status assert response.status_code == expected_status
# There should be links to custom-route-name but none to original-name
if response.status_code == 200:
assert "/custom-route-name" in response.text
assert "/original-name" not in response.text