From a498d0fe6590f9bdbc4faf9e0dd5faeb3b06002c Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 21 Mar 2020 19:40:29 -0700 Subject: [PATCH] Fix bug with over-riding default sort, closes #702 --- datasette/views/table.py | 8 ++++++-- tests/test_html.py | 22 ++++++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/datasette/views/table.py b/datasette/views/table.py index 96e8922b..7267c1db 100644 --- a/datasette/views/table.py +++ b/datasette/views/table.py @@ -402,8 +402,12 @@ class TableView(RowTableShared): ) # Allow for custom sort order - sort = special_args.get("_sort") or table_metadata.get("sort") - sort_desc = special_args.get("_sort_desc") or table_metadata.get("sort_desc") + sort = special_args.get("_sort") + sort_desc = special_args.get("_sort_desc") + + if not sort and not sort_desc: + sort = table_metadata.get("sort") + sort_desc = table_metadata.get("sort_desc") if sort and sort_desc: raise DatasetteError("Cannot use _sort and _sort_desc at the same time") diff --git a/tests/test_html.py b/tests/test_html.py index a0216672..d54446c7 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -1105,7 +1105,7 @@ def test_metadata_sort(app_client): ths = table.findAll("th") assert ["id", "name\xa0▼"] == [th.find("a").string.strip() for th in ths] rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")] - assert [ + expected = [ [ '3', 'Detroit', @@ -1122,7 +1122,14 @@ def test_metadata_sort(app_client): '1', 'San Francisco', ], - ] == rows + ] + assert expected == rows + # Make sure you can reverse that sort order + response = app_client.get("/fixtures/facet_cities?_sort_desc=name") + assert response.status == 200 + table = Soup(response.body, "html.parser").find("table") + rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")] + assert list(reversed(expected)) == rows def test_metadata_sort_desc(app_client): @@ -1133,7 +1140,7 @@ def test_metadata_sort_desc(app_client): ths = table.findAll("th") assert ["pk\xa0▲", "name"] == [th.find("a").string.strip() for th in ths] rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")] - assert [ + expected = [ [ '2', 'Paranormal', @@ -1142,4 +1149,11 @@ def test_metadata_sort_desc(app_client): '1', 'Museum', ], - ] == rows + ] + assert expected == rows + # Make sure you can reverse that sort order + response = app_client.get("/fixtures/attraction_characteristic?_sort=pk") + assert response.status == 200 + table = Soup(response.body, "html.parser").find("table") + rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")] + assert list(reversed(expected)) == rows