Skip to content

Commit

Permalink
Facet "selected" key and toggle_url now toggles, refs #255
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw authored and Simon Willison committed May 16, 2018
1 parent 2b79f2b commit de05cf2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
12 changes: 12 additions & 0 deletions datasette/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ def path_with_added_args(request, args, path=None):
return path + query_string


def path_with_removed_args(request, args, path=None):
path = path or request.path
current = []
for key, value in urllib.parse.parse_qsl(request.query_string):
if key not in args:
current.append((key, value))
query_string = urllib.parse.urlencode(current)
if query_string:
query_string = '?{}'.format(query_string)
return path + query_string


def path_with_ext(request, ext):
path = request.path
path += ext
Expand Down
23 changes: 16 additions & 7 deletions datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
is_url,
path_from_row_pks,
path_with_added_args,
path_with_removed_args,
to_css_class,
urlsafe_components
)
Expand Down Expand Up @@ -505,17 +506,25 @@ async def data(self, request, name, hash, table):
facet_rows = await self.execute(
name, facet_sql, params, truncate=False, custom_time_limit=200
)
facet_results[column] = [
{
facet_results[column] = []
for row in facet_rows:
selected = other_args.get(column) == row["value"]
if selected:
toggle_path = path_with_removed_args(
request, {column: row["value"]}
)
else:
toggle_path = path_with_added_args(
request, {column: row["value"]}
)
facet_results[column].append({
"value": row["value"],
"count": row["count"],
"toggle_url": urllib.parse.urljoin(
request.url,
path_with_added_args(request, {column: row["value"]}),
request.url, toggle_path
),
}
for row in facet_rows
]
"selected": selected,
})
except sqlite3.OperationalError:
# Hit time limit
pass
Expand Down
5 changes: 5 additions & 0 deletions docs/facets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,33 @@ This works for both the HTML interface and the ``.json`` view. When enabled, fac
{
"value": "CA",
"count": 10,
"selected": false,
"toggle_url": "http://...&state=CA"
},
{
"value": "MI",
"count": 4,
"selected": false,
"toggle_url": "http://...&state=MI"
}
],
"city": [
{
"value": "San Francisco",
"count": 6,
"selected": false,
"toggle_url": "http://...=San+Francisco"
},
{
"value": "Detroit",
"count": 4,
"selected": false,
"toggle_url": "http://...&city=Detroit"
},
{
"value": "Los Angeles",
"count": 4,
"selected": false,
"toggle_url": "http://...=Los+Angeles"
}
]
Expand Down
9 changes: 8 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,28 +897,33 @@ def test_page_size_matching_max_returned_rows(app_client_returend_rows_matches_p
{
"value": "CA",
"count": 10,
"selected": False,
"toggle_url": "_facet=state&_facet=city&state=CA",
},
{
"value": "MI",
"count": 4,
"selected": False,
"toggle_url": "_facet=state&_facet=city&state=MI",
},
],
"city": [
{
"value": "San Francisco",
"count": 6,
"selected": False,
"toggle_url": "_facet=state&_facet=city&city=San+Francisco",
},
{
"value": "Detroit",
"count": 4,
"selected": False,
"toggle_url": "_facet=state&_facet=city&city=Detroit",
},
{
"value": "Los Angeles",
"count": 4,
"selected": False,
"toggle_url": "_facet=state&_facet=city&city=Los+Angeles",
},
],
Expand All @@ -930,13 +935,15 @@ def test_page_size_matching_max_returned_rows(app_client_returend_rows_matches_p
{
"value": "MI",
"count": 4,
"toggle_url": "_facet=state&_facet=city&state=MI",
"selected": True,
"toggle_url": "_facet=state&_facet=city",
},
],
"city": [
{
"value": "Detroit",
"count": 4,
"selected": False,
"toggle_url": "_facet=state&_facet=city&state=MI&city=Detroit",
},
],
Expand Down
13 changes: 13 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ def test_path_with_added_args(path, added_args, expected):
assert expected == actual


@pytest.mark.parametrize('path,args,expected', [
('/foo?bar=1', {'bar'}, '/foo'),
('/foo?bar=1&baz=2', {'bar'}, '/foo?baz=2'),
])
def test_path_with_removed_args(path, args, expected):
request = Request(
path.encode('utf8'),
{}, '1.1', 'GET', None
)
actual = utils.path_with_removed_args(request, args)
assert expected == actual


@pytest.mark.parametrize('row,pks,expected_path', [
({'A': 'foo', 'B': 'bar'}, ['A', 'B'], 'foo,bar'),
({'A': 'f,o', 'B': 'bar'}, ['A', 'B'], 'f%2Co,bar'),
Expand Down

0 comments on commit de05cf2

Please sign in to comment.