mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
parent
b72b2423c7
commit
8d5779acf0
10 changed files with 27 additions and 62 deletions
|
|
@ -55,6 +55,7 @@ class DatasetteError(Exception):
|
|||
|
||||
class BaseView:
|
||||
ds = None
|
||||
has_json_alternate = True
|
||||
|
||||
def __init__(self, datasette):
|
||||
self.ds = datasette
|
||||
|
|
@ -137,10 +138,20 @@ class BaseView:
|
|||
],
|
||||
},
|
||||
}
|
||||
# Hacky cheat to add extra headers
|
||||
headers = {}
|
||||
if "_extra_headers" in context:
|
||||
headers.update(context["_extra_headers"])
|
||||
if self.has_json_alternate:
|
||||
alternate_url_json = self.ds.absolute_url(
|
||||
request,
|
||||
self.ds.urls.path(path_with_format(request=request, format="json")),
|
||||
)
|
||||
template_context["alternate_url_json"] = alternate_url_json
|
||||
headers.update(
|
||||
{
|
||||
"Link": '{}; rel="alternate"; type="application/json+datasette"'.format(
|
||||
alternate_url_json
|
||||
)
|
||||
}
|
||||
)
|
||||
return Response.html(
|
||||
await self.ds.render_template(
|
||||
template,
|
||||
|
|
|
|||
|
|
@ -123,10 +123,6 @@ class DatabaseView(DataView):
|
|||
|
||||
attached_databases = [d.name for d in await db.attached_databases()]
|
||||
|
||||
alternate_url_json = self.ds.absolute_url(
|
||||
request,
|
||||
self.ds.urls.path(path_with_format(request=request, format="json")),
|
||||
)
|
||||
return (
|
||||
{
|
||||
"database": database,
|
||||
|
|
@ -144,7 +140,6 @@ class DatabaseView(DataView):
|
|||
),
|
||||
},
|
||||
{
|
||||
"alternate_url_json": alternate_url_json,
|
||||
"database_actions": database_actions,
|
||||
"show_hidden": request.args.get("_show_hidden"),
|
||||
"editable": True,
|
||||
|
|
@ -153,11 +148,6 @@ class DatabaseView(DataView):
|
|||
and not db.is_mutable
|
||||
and not db.is_memory,
|
||||
"attached_databases": attached_databases,
|
||||
"_extra_headers": {
|
||||
"Link": '{}; rel="alternate"; type="application/json+datasette"'.format(
|
||||
alternate_url_json
|
||||
)
|
||||
},
|
||||
},
|
||||
(f"database-{to_css_class(database)}.html", "database.html"),
|
||||
)
|
||||
|
|
@ -318,14 +308,7 @@ class QueryView(DataView):
|
|||
else:
|
||||
|
||||
async def extra_template():
|
||||
alternate_url_json = self.ds.absolute_url(
|
||||
request,
|
||||
self.ds.urls.path(
|
||||
path_with_format(request=request, format="json")
|
||||
),
|
||||
)
|
||||
return {
|
||||
"alternate_url_json": alternate_url_json,
|
||||
"request": request,
|
||||
"path_with_added_args": path_with_added_args,
|
||||
"path_with_removed_args": path_with_removed_args,
|
||||
|
|
@ -333,11 +316,6 @@ class QueryView(DataView):
|
|||
"canned_query": canned_query,
|
||||
"success_message": request.args.get("_success") or "",
|
||||
"canned_write": True,
|
||||
"_extra_headers": {
|
||||
"Link": '{}; rel="alternate"; type="application/json+datasette"'.format(
|
||||
alternate_url_json
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
@ -470,12 +448,7 @@ class QueryView(DataView):
|
|||
show_hide_link = path_with_added_args(request, {"_hide_sql": 1})
|
||||
show_hide_text = "hide"
|
||||
hide_sql = show_hide_text == "show"
|
||||
alternate_url_json = self.ds.absolute_url(
|
||||
request,
|
||||
self.ds.urls.path(path_with_format(request=request, format="json")),
|
||||
)
|
||||
return {
|
||||
"alternate_url_json": alternate_url_json,
|
||||
"display_rows": display_rows,
|
||||
"custom_sql": True,
|
||||
"named_parameter_values": named_parameter_values,
|
||||
|
|
@ -489,11 +462,6 @@ class QueryView(DataView):
|
|||
"show_hide_text": show_hide_text,
|
||||
"show_hide_hidden": markupsafe.Markup(show_hide_hidden),
|
||||
"hide_sql": hide_sql,
|
||||
"_extra_headers": {
|
||||
"Link": '{}; rel="alternate"; type="application/json+datasette"'.format(
|
||||
alternate_url_json
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
|
|||
|
|
@ -851,12 +851,7 @@ class TableView(RowTableShared):
|
|||
for table_column in table_columns
|
||||
if table_column not in columns
|
||||
]
|
||||
alternate_url_json = self.ds.absolute_url(
|
||||
request,
|
||||
self.ds.urls.path(path_with_format(request=request, format="json")),
|
||||
)
|
||||
d = {
|
||||
"alternate_url_json": alternate_url_json,
|
||||
"table_actions": table_actions,
|
||||
"use_rowid": use_rowid,
|
||||
"filters": filters,
|
||||
|
|
@ -887,11 +882,6 @@ class TableView(RowTableShared):
|
|||
"metadata": metadata,
|
||||
"view_definition": await db.get_view_definition(table),
|
||||
"table_definition": await db.get_table_definition(table),
|
||||
"_extra_headers": {
|
||||
"Link": '{}; rel="alternate"; type="application/json+datasette"'.format(
|
||||
alternate_url_json
|
||||
)
|
||||
},
|
||||
}
|
||||
d.update(extra_context_from_filters)
|
||||
return d
|
||||
|
|
@ -975,12 +965,7 @@ class RowView(RowTableShared):
|
|||
)
|
||||
for column in display_columns:
|
||||
column["sortable"] = False
|
||||
alternate_url_json = self.ds.absolute_url(
|
||||
request,
|
||||
self.ds.urls.path(path_with_format(request=request, format="json")),
|
||||
)
|
||||
return {
|
||||
"alternate_url_json": alternate_url_json,
|
||||
"foreign_key_tables": await self.foreign_key_tables(
|
||||
database, table, pk_values
|
||||
),
|
||||
|
|
@ -995,11 +980,6 @@ class RowView(RowTableShared):
|
|||
.get(database, {})
|
||||
.get("tables", {})
|
||||
.get(table, {}),
|
||||
"_extra_headers": {
|
||||
"Link": '{}; rel="alternate"; type="application/json+datasette"'.format(
|
||||
alternate_url_json
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
data = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue