|
1 | 1 | """ |
2 | 2 | Common to Django tags (sorting_tags) and Jinja2 globals (jinja2_globals) |
3 | 3 | """ |
4 | | -from urllib.parse import urlencode |
5 | 4 | from operator import attrgetter |
6 | 5 |
|
7 | 6 | from .settings import SORT_DIRECTIONS |
8 | 7 |
|
9 | 8 |
|
10 | 9 | def render_sort_anchor(request, field_name, title): |
11 | | - sort_by = request.GET.get("sort", "") |
| 10 | + get_params = request.GET.copy() |
| 11 | + sort_by = get_params.get("sort", None) |
12 | 12 | if sort_by == field_name: |
13 | 13 | # Render anchor link to next direction |
14 | | - sort_direction = SORT_DIRECTIONS[request.GET.get("dir", "")] |
15 | | - next_direction_code = sort_direction["next"] |
16 | | - icon = sort_direction["icon"] |
| 14 | + current_direction = SORT_DIRECTIONS[get_params.get("dir", "")] |
| 15 | + icon = current_direction["icon"] |
| 16 | + next_direction_code = current_direction["next"] |
17 | 17 | else: |
18 | | - # Just a fast code path |
19 | | - next_direction_code = "asc" |
20 | 18 | icon = "" |
| 19 | + next_direction_code = "asc" |
21 | 20 |
|
22 | | - url_sort_by = urlencode({"sort": field_name}) |
23 | | - url_append = f"?{url_sort_by}" |
24 | | - if next_direction_code: |
25 | | - url_sort_direction = urlencode({"dir": next_direction_code}) |
26 | | - url_append += f"&{url_sort_direction}" |
27 | | - |
| 21 | + # Not usual dict (can't update to replace) |
| 22 | + get_params["sort"] = field_name |
| 23 | + get_params["dir"] = next_direction_code |
| 24 | + url_append = "?" + get_params.urlencode() if get_params else "" |
28 | 25 | return f'<a href="{request.path}{url_append}" title="{title}">{title}{icon}</a>' |
29 | 26 |
|
30 | 27 |
|
|
0 commit comments