From a5881e105a02830d26f07e98177248d5910893da Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 19 Nov 2017 12:33:30 -0800 Subject: [PATCH] ?_filter_column=col&_filter_op=isnull__1 redirect if filter_op contains a __ the value is set to the right hand side. e.g. ?_filter_column=col&_filter_op=isnull__1&_filter_value=x Redirects to: ?col__isnull=1 Refs #86 --- datasette/app.py | 2 ++ tests/test_app.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/datasette/app.py b/datasette/app.py index 9c0d29f1..fc1e8dfd 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -431,6 +431,8 @@ class TableView(BaseView): filter_column = special_args['_filter_column'] filter_op = special_args.get('_filter_op') or '' filter_value = special_args.get('_filter_value') or '' + if '__' in filter_op: + filter_op, filter_value = filter_op.split('__', 1) return self.redirect(request, path_with_added_args(request, { '{}__{}'.format(filter_column, filter_op): filter_value, '_filter_column': None, diff --git a/tests/test_app.py b/tests/test_app.py index 5df5ea25..003b5aca 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -249,6 +249,16 @@ def test_add_filter_redirects(app_client): assert response.status == 302 assert response.headers['Location'].endswith('?content__startswith=x&foo=bar') + # Test that op with a __x suffix overrides the filter value + path = path_base + '?' + urllib.parse.urlencode({ + '_filter_column': 'content', + '_filter_op': 'isnull__5', + '_filter_value': 'x' + }) + response = app_client.get(path, allow_redirects=False, gather_request=False) + assert response.status == 302 + assert response.headers['Location'].endswith('?content__isnull=5') + TABLES = ''' CREATE TABLE simple_primary_key (