kopia lustrzana https://github.com/simonw/datasette
Default to _labels=on on JSON/CSV links with foreign keys, refs #266
rodzic
6a32684ebb
commit
0d7ba1ba67
|
@ -169,20 +169,20 @@ class BaseView(RenderMixin):
|
|||
raise
|
||||
# Convert rows and columns to CSV
|
||||
headings = data["columns"]
|
||||
# if there are columns_expanded we need to add additional headings
|
||||
columns_expanded = set(data.get("columns_expanded") or [])
|
||||
if columns_expanded:
|
||||
# if there are expanded_columns we need to add additional headings
|
||||
expanded_columns = set(data.get("expanded_columns") or [])
|
||||
if expanded_columns:
|
||||
headings = []
|
||||
for column in data["columns"]:
|
||||
headings.append(column)
|
||||
if column in columns_expanded:
|
||||
if column in expanded_columns:
|
||||
headings.append("{}_label".format(column))
|
||||
|
||||
async def stream_fn(r):
|
||||
writer = csv.writer(r)
|
||||
writer.writerow(headings)
|
||||
for row in data["rows"]:
|
||||
if not columns_expanded:
|
||||
if not expanded_columns:
|
||||
# Simple path
|
||||
writer.writerow(row)
|
||||
else:
|
||||
|
@ -349,17 +349,24 @@ class BaseView(RenderMixin):
|
|||
extras = await extras
|
||||
else:
|
||||
extras = extra_template_data
|
||||
url_labels_extra = {}
|
||||
if data.get("expandable_columns"):
|
||||
url_labels_extra = {"_labels": "on"}
|
||||
context = {
|
||||
**data,
|
||||
**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", {
|
||||
"_size": "max"
|
||||
"_size": "max",
|
||||
**url_labels_extra
|
||||
}),
|
||||
"url_csv_dl": path_with_format(request, "csv", {
|
||||
"_dl": "1",
|
||||
"_size": "max"
|
||||
"_size": "max",
|
||||
**url_labels_extra
|
||||
}),
|
||||
"extra_css_urls": self.ds.extra_css_urls(),
|
||||
"extra_js_urls": self.ds.extra_js_urls(),
|
||||
|
|
|
@ -573,7 +573,7 @@ class TableView(RowTableShared):
|
|||
filter_columns = filter_columns[1:]
|
||||
|
||||
# Expand labeled columns if requested
|
||||
columns_expanded = []
|
||||
expanded_columns = []
|
||||
expandable_columns = self.expandable_columns(name, table)
|
||||
columns_to_expand = None
|
||||
try:
|
||||
|
@ -595,7 +595,7 @@ class TableView(RowTableShared):
|
|||
column = fk["column"]
|
||||
if column not in columns_to_expand:
|
||||
continue
|
||||
columns_expanded.append(column)
|
||||
expanded_columns.append(column)
|
||||
# Gather the values
|
||||
column_index = columns.index(column)
|
||||
values = [row[column_index] for row in rows]
|
||||
|
@ -771,7 +771,8 @@ class TableView(RowTableShared):
|
|||
"truncated": results.truncated,
|
||||
"table_rows_count": 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,
|
||||
"primary_keys": pks,
|
||||
"units": units,
|
||||
|
|
|
@ -289,6 +289,21 @@ def test_table_csv_json_export_links(app_client):
|
|||
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):
|
||||
response = app_client.get('/fixtures/simple_primary_key/1')
|
||||
assert response.status == 200
|
||||
|
|
Ładowanie…
Reference in New Issue