_sort/_next links now use new path_with_replaced_args method

This commit is contained in:
Simon Willison 2018-05-15 06:34:45 -03:00 committed by Simon Willison
commit a82175276c
5 changed files with 85 additions and 4 deletions

View file

@ -7,9 +7,9 @@
{{ column.name }}
{% else %}
{% if column.name == sort %}
<a href="{{ path_with_added_args(request, {'_sort_desc': column.name, '_sort': None, '_next': None}) }}" rel="nofollow">{{ column.name }}&nbsp;</a>
<a href="{{ path_with_replaced_args(request, {'_sort_desc': column.name, '_sort': None, '_next': None}) }}" rel="nofollow">{{ column.name }}&nbsp;</a>
{% else %}
<a href="{{ path_with_added_args(request, {'_sort': column.name, '_sort_desc': None, '_next': None}) }}" rel="nofollow">{{ column.name }}{% if column.name == sort_desc %}&nbsp;▲{% endif %}</a>
<a href="{{ path_with_replaced_args(request, {'_sort': column.name, '_sort_desc': None, '_next': None}) }}" rel="nofollow">{{ column.name }}{% if column.name == sort_desc %}&nbsp;▲{% endif %}</a>
{% endif %}
{% endif %}
</th>

View file

@ -177,6 +177,22 @@ def path_with_removed_args(request, args, path=None):
return path + query_string
def path_with_replaced_args(request, args, path=None):
path = path or request.path
if isinstance(args, dict):
args = args.items()
keys_to_replace = {p[0] for p in args}
current = []
for key, value in urllib.parse.parse_qsl(request.query_string):
if key not in keys_to_replace:
current.append((key, value))
current.extend([p for p in args if p[1] is not None])
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

@ -14,6 +14,7 @@ from datasette.utils import (
path_from_row_pks,
path_with_added_args,
path_with_removed_args,
path_with_replaced_args,
to_css_class,
urlsafe_components
)
@ -562,7 +563,7 @@ class TableView(RowTableShared):
else:
added_args = {"_next": next_value}
next_url = urllib.parse.urljoin(
request.url, path_with_added_args(request, added_args)
request.url, path_with_replaced_args(request, added_args)
)
rows = rows[:page_size]
@ -650,7 +651,7 @@ class TableView(RowTableShared):
"filter_columns": filter_columns,
"display_rows": display_rows,
"is_sortable": any(c["sortable"] for c in display_columns),
"path_with_added_args": path_with_added_args,
"path_with_replaced_args": path_with_replaced_args,
"request": request,
"sort": sort,
"sort_desc": sort_desc,