Skip to content

Commit

Permalink
Implemented alternate_url_json
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Aug 8, 2023
1 parent ea24c9c commit 8fbcd7f
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion datasette/views/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ async def database_actions():
return response

assert format_ == "html"
alternate_url_json = datasette.absolute_url(
request,
datasette.urls.path(path_with_format(request=request, format="json")),
)
context = {
**json_data,
"database_actions": database_actions,
Expand All @@ -154,10 +158,16 @@ async def database_actions():
and not db.is_memory,
"attached_databases": attached_databases,
"database_color": lambda _: "#ff0000",
"alternate_url_json": alternate_url_json,
}
templates = (f"database-{to_css_class(database)}.html", "database.html")
return Response.html(
await datasette.render_template(templates, context, request=request)
await datasette.render_template(templates, context, request=request),
headers={
"Link": '{}; rel="alternate"; type="application/json+datasette"'.format(
alternate_url_json
)
},
)


Expand Down Expand Up @@ -219,6 +229,9 @@ class QueryContext:
table_columns: dict = field(
metadata={"help": "Dictionary of table name to list of column names"}
)
alternate_url_json: str = field(
metadata={"help": "URL for alternate JSON version of this page"}
)


async def get_tables(datasette, request, db):
Expand Down Expand Up @@ -522,6 +535,7 @@ async def fetch_data_for_csv(request, _next=None):
show_hide_hidden=markupsafe.Markup(show_hide_hidden),
metadata=metadata,
database_color=lambda _: "#ff0000",
alternate_url_json=alternate_url_json,
),
request=request,
),
Expand Down

0 comments on commit 8fbcd7f

Please sign in to comment.