Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions datasette/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,9 @@ def path_with_added_args(request, args, path=None):
args = args.items()
args_to_remove = {k for k, v in args if v is None}
current = []
for key, value in urllib.parse.parse_qsl(request.query_string):
for key, value in urllib.parse.parse_qsl(
request.query_string, keep_blank_values=True
):
if key not in args_to_remove:
current.append((key, value))
current.extend([(key, value) for key, value in args if value is not None])
Expand Down Expand Up @@ -565,7 +567,7 @@ def should_remove(key, value):
def should_remove(key, value):
return args.get(key) == value

for key, value in urllib.parse.parse_qsl(query_string):
for key, value in urllib.parse.parse_qsl(query_string, keep_blank_values=True):
if not should_remove(key, value):
current.append((key, value))
query_string = urllib.parse.urlencode(current)
Expand All @@ -580,7 +582,9 @@ def path_with_replaced_args(request, args, path=None):
args = args.items()
keys_to_replace = {p[0] for p in args}
current = []
for key, value in urllib.parse.parse_qsl(request.query_string):
for key, value in urllib.parse.parse_qsl(
request.query_string, keep_blank_values=True
):
if key not in keys_to_replace:
current.append((key, value))
current.extend([p for p in args if p[1] is not None])
Expand Down Expand Up @@ -942,6 +946,8 @@ def table_column_details(conn, table):

def filters_should_redirect(special_args):
redirect_params = []
if special_args.get("_search") == "":
redirect_params.append(("_search", None))
# Handle _filter_column=foo&_filter_op=exact&_filter_value=...
filter_column = special_args.get("_filter_column")
filter_op = special_args.get("_filter_op") or ""
Expand Down
13 changes: 13 additions & 0 deletions tests/test_table_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,19 @@ def _export_urls(soup):
]


@pytest.mark.asyncio
async def test_sort_link_preserves_blank_filter(ds_client):
response = await ds_client.get("/fixtures/simple_primary_key?content__exact=")
assert response.status_code == 200
soup = Soup(response.text, "html.parser")
table = soup.find("table")
content_header = next(
th for th in table.select("thead th") if th.get_text(strip=True) == "content"
)
assert "content__exact=" in content_header.find("a")["href"]
assert "_sort=content" in content_header.find("a")["href"]


def test_base_url_export_links_are_not_double_prefixed(app_client_base_url_prefix):
for path, expected_json, expected_csv in (
(
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def test_urlsafe_components(path, expected):
("/foo?bar=1", {"baz": 2}, "/foo?bar=1&baz=2"),
("/foo?bar=1&bar=2", {"baz": 3}, "/foo?bar=1&bar=2&baz=3"),
("/foo?bar=1", {"bar": None}, "/foo"),
("/foo?bar=", {"baz": 2}, "/foo?bar=&baz=2"),
("/foo?bar=&baz=2", {"baz": None}, "/foo?bar="),
# Test order is preserved
(
"/?_facet=prim_state&_facet=area_name",
Expand Down Expand Up @@ -74,6 +76,7 @@ def test_path_with_added_args(path, added_args, expected):
("/foo?bar=1", {"bar"}, "/foo"),
("/foo?bar=1&baz=2", {"bar"}, "/foo?baz=2"),
("/foo?bar=1&bar=2&bar=3", {"bar": "2"}, "/foo?bar=1&bar=3"),
("/foo?bar=&baz=2", {"baz"}, "/foo?bar="),
],
)
def test_path_with_removed_args(path, args, expected):
Expand All @@ -91,6 +94,7 @@ def test_path_with_removed_args(path, args, expected):
[
("/foo?bar=1", {"bar": 2}, "/foo?bar=2"),
("/foo?bar=1&baz=2", {"bar": None}, "/foo?baz=2"),
("/foo?bar=&baz=2", {"baz": 3}, "/foo?bar=&baz=3"),
],
)
def test_path_with_replaced_args(path, args, expected):
Expand Down