Skip to content

Commit

Permalink
Fix CSV export hidden form fields, closes #393
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 3, 2019
1 parent 8b8ae55 commit 996e882
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
6 changes: 2 additions & 4 deletions datasette/templates/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,8 @@ <h3>Advanced export</h3>
{% if expandable_columns %}<label><input type="checkbox" name="_labels" checked> expand labels</label>{% endif %}
{% if next_url and config.allow_csv_stream %}<label><input type="checkbox" name="_stream"> stream all rows</label>{% endif %}
<input type="submit" value="Export CSV">
{% for key, value in url_csv_args.items() %}
{% if key != "_labels" %}
<input type="hidden" name="{{ key }}" value="{{ value }}">
{% endif %}
{% for key, value in url_csv_hidden_args %}
<input type="hidden" name="{{ key }}" value="{{ value }}">
{% endfor %}
</p>
</form>
Expand Down
6 changes: 5 additions & 1 deletion datasette/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,11 @@ async def view_get(self, request, database, hash, **kwargs):
}),
"url_csv": url_csv,
"url_csv_path": url_csv_path,
"url_csv_args": url_csv_args,
"url_csv_hidden_args": [
(key, value)
for key, value in urllib.parse.parse_qsl(request.query_string)
if key not in ("_labels", "_facet", "_size")
] + [("_size", "max")],
"datasette_version": __version__,
"config": self.ds.config_dict(),
}
Expand Down
13 changes: 7 additions & 6 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,16 @@ def test_table_html_simple_primary_key(app_client):


def test_table_csv_json_export_interface(app_client):
response = app_client.get('/fixtures/simple_primary_key')
response = app_client.get('/fixtures/simple_primary_key?id__gt=2')
assert response.status == 200
# The links at the top of the page
links = Soup(response.body, "html.parser").find("p", {
"class": "export-links"
}).findAll("a")
actual = [l["href"].split("/")[-1] for l in links]
expected = [
"simple_primary_key.json",
"simple_primary_key.csv?_size=max",
"simple_primary_key.json?id__gt=2",
"simple_primary_key.csv?id__gt=2&_size=max",
"#export"
]
assert expected == actual
Expand All @@ -420,9 +420,9 @@ def test_table_csv_json_export_interface(app_client):
})
json_links = [a["href"].split("/")[-1] for a in div.find("p").findAll("a")]
assert [
"simple_primary_key.json",
"simple_primary_key.json?_shape=array",
"simple_primary_key.json?_shape=object"
"simple_primary_key.json?id__gt=2",
"simple_primary_key.json?id__gt=2&_shape=array",
"simple_primary_key.json?id__gt=2&_shape=object"
] == json_links
# And the CSV form
form = div.find("form")
Expand All @@ -431,6 +431,7 @@ def test_table_csv_json_export_interface(app_client):
assert [
'<input name="_dl" type="checkbox"/>',
'<input type="submit" value="Export CSV"/>',
'<input name="id__gt" type="hidden" value="2"/>',
'<input name="_size" type="hidden" value="max"/>'
] == inputs

Expand Down

0 comments on commit 996e882

Please sign in to comment.