mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Default to _labels=on on JSON/CSV links with foreign keys, refs #266
This commit is contained in:
parent
6a32684ebb
commit
0d7ba1ba67
3 changed files with 34 additions and 11 deletions
|
|
@ -169,20 +169,20 @@ class BaseView(RenderMixin):
|
||||||
raise
|
raise
|
||||||
# Convert rows and columns to CSV
|
# Convert rows and columns to CSV
|
||||||
headings = data["columns"]
|
headings = data["columns"]
|
||||||
# if there are columns_expanded we need to add additional headings
|
# if there are expanded_columns we need to add additional headings
|
||||||
columns_expanded = set(data.get("columns_expanded") or [])
|
expanded_columns = set(data.get("expanded_columns") or [])
|
||||||
if columns_expanded:
|
if expanded_columns:
|
||||||
headings = []
|
headings = []
|
||||||
for column in data["columns"]:
|
for column in data["columns"]:
|
||||||
headings.append(column)
|
headings.append(column)
|
||||||
if column in columns_expanded:
|
if column in expanded_columns:
|
||||||
headings.append("{}_label".format(column))
|
headings.append("{}_label".format(column))
|
||||||
|
|
||||||
async def stream_fn(r):
|
async def stream_fn(r):
|
||||||
writer = csv.writer(r)
|
writer = csv.writer(r)
|
||||||
writer.writerow(headings)
|
writer.writerow(headings)
|
||||||
for row in data["rows"]:
|
for row in data["rows"]:
|
||||||
if not columns_expanded:
|
if not expanded_columns:
|
||||||
# Simple path
|
# Simple path
|
||||||
writer.writerow(row)
|
writer.writerow(row)
|
||||||
else:
|
else:
|
||||||
|
|
@ -349,17 +349,24 @@ class BaseView(RenderMixin):
|
||||||
extras = await extras
|
extras = await extras
|
||||||
else:
|
else:
|
||||||
extras = extra_template_data
|
extras = extra_template_data
|
||||||
|
url_labels_extra = {}
|
||||||
|
if data.get("expandable_columns"):
|
||||||
|
url_labels_extra = {"_labels": "on"}
|
||||||
context = {
|
context = {
|
||||||
**data,
|
**data,
|
||||||
**extras,
|
**extras,
|
||||||
**{
|
**{
|
||||||
"url_json": path_with_format(request, "json"),
|
"url_json": path_with_format(request, "json", {
|
||||||
|
**url_labels_extra,
|
||||||
|
}),
|
||||||
"url_csv": path_with_format(request, "csv", {
|
"url_csv": path_with_format(request, "csv", {
|
||||||
"_size": "max"
|
"_size": "max",
|
||||||
|
**url_labels_extra
|
||||||
}),
|
}),
|
||||||
"url_csv_dl": path_with_format(request, "csv", {
|
"url_csv_dl": path_with_format(request, "csv", {
|
||||||
"_dl": "1",
|
"_dl": "1",
|
||||||
"_size": "max"
|
"_size": "max",
|
||||||
|
**url_labels_extra
|
||||||
}),
|
}),
|
||||||
"extra_css_urls": self.ds.extra_css_urls(),
|
"extra_css_urls": self.ds.extra_css_urls(),
|
||||||
"extra_js_urls": self.ds.extra_js_urls(),
|
"extra_js_urls": self.ds.extra_js_urls(),
|
||||||
|
|
|
||||||
|
|
@ -573,7 +573,7 @@ class TableView(RowTableShared):
|
||||||
filter_columns = filter_columns[1:]
|
filter_columns = filter_columns[1:]
|
||||||
|
|
||||||
# Expand labeled columns if requested
|
# Expand labeled columns if requested
|
||||||
columns_expanded = []
|
expanded_columns = []
|
||||||
expandable_columns = self.expandable_columns(name, table)
|
expandable_columns = self.expandable_columns(name, table)
|
||||||
columns_to_expand = None
|
columns_to_expand = None
|
||||||
try:
|
try:
|
||||||
|
|
@ -595,7 +595,7 @@ class TableView(RowTableShared):
|
||||||
column = fk["column"]
|
column = fk["column"]
|
||||||
if column not in columns_to_expand:
|
if column not in columns_to_expand:
|
||||||
continue
|
continue
|
||||||
columns_expanded.append(column)
|
expanded_columns.append(column)
|
||||||
# Gather the values
|
# Gather the values
|
||||||
column_index = columns.index(column)
|
column_index = columns.index(column)
|
||||||
values = [row[column_index] for row in rows]
|
values = [row[column_index] for row in rows]
|
||||||
|
|
@ -771,7 +771,8 @@ class TableView(RowTableShared):
|
||||||
"truncated": results.truncated,
|
"truncated": results.truncated,
|
||||||
"table_rows_count": table_rows_count,
|
"table_rows_count": table_rows_count,
|
||||||
"filtered_table_rows_count": filtered_table_rows_count,
|
"filtered_table_rows_count": filtered_table_rows_count,
|
||||||
"columns_expanded": columns_expanded,
|
"expanded_columns": expanded_columns,
|
||||||
|
"expandable_columns": expandable_columns,
|
||||||
"columns": columns,
|
"columns": columns,
|
||||||
"primary_keys": pks,
|
"primary_keys": pks,
|
||||||
"units": units,
|
"units": units,
|
||||||
|
|
|
||||||
|
|
@ -289,6 +289,21 @@ def test_table_csv_json_export_links(app_client):
|
||||||
assert expected == actual
|
assert expected == actual
|
||||||
|
|
||||||
|
|
||||||
|
def test_csv_json_export_links_include_labels_if_foreign_keys(app_client):
|
||||||
|
response = app_client.get('/fixtures/facetable')
|
||||||
|
assert response.status == 200
|
||||||
|
links = Soup(response.body, "html.parser").find("p", {
|
||||||
|
"class": "export-links"
|
||||||
|
}).findAll("a")
|
||||||
|
actual = [l["href"].split("/")[-1] for l in links]
|
||||||
|
expected = [
|
||||||
|
"facetable.json?_labels=on",
|
||||||
|
"facetable.csv?_labels=on&_size=max",
|
||||||
|
"facetable.csv?_dl=1&_labels=on&_size=max"
|
||||||
|
]
|
||||||
|
assert expected == actual
|
||||||
|
|
||||||
|
|
||||||
def test_row_html_simple_primary_key(app_client):
|
def test_row_html_simple_primary_key(app_client):
|
||||||
response = app_client.get('/fixtures/simple_primary_key/1')
|
response = app_client.get('/fixtures/simple_primary_key/1')
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue