Facet "selected" key and toggle_url now toggles, refs #255

This commit is contained in:
Simon Willison 2018-05-14 17:42:10 -03:00 committed by Simon Willison
commit de05cf21aa
5 changed files with 54 additions and 8 deletions

View file

@ -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

View file

@ -13,6 +13,7 @@ from datasette.utils import (
is_url,
path_from_row_pks,
path_with_added_args,
path_with_removed_args,
to_css_class,
urlsafe_components
)
@ -505,17 +506,25 @@ class TableView(RowTableShared):
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