Skip to content

Commit 2ad558e

Browse files
authored
fix: range filters wrong arguments (#293)
1 parent de85e4f commit 2ad558e

File tree

8 files changed

+33
-19
lines changed

8 files changed

+33
-19
lines changed

src/unfold/contrib/filters/admin.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ def __init__(
5757

5858
if self.parameter_name in params:
5959
value = params.pop(self.parameter_name)
60-
self.used_parameters[self.parameter_name] = value
60+
value = value[0] if isinstance(value, list) else value
61+
62+
if value not in EMPTY_VALUES:
63+
self.used_parameters[self.parameter_name] = value
6164

6265
def queryset(
6366
self, request: HttpRequest, queryset: QuerySet[Any]
@@ -94,11 +97,16 @@ class RangeNumericMixin:
9497
def init_used_parameters(self, params: Dict[str, Any]) -> None:
9598
if self.parameter_name + "_from" in params:
9699
value = params.pop(self.parameter_name + "_from")
97-
self.used_parameters[self.parameter_name + "_from"] = value
100+
101+
self.used_parameters[self.parameter_name + "_from"] = (
102+
value[0] if isinstance(value, list) else value
103+
)
98104

99105
if self.parameter_name + "_to" in params:
100106
value = params.pop(self.parameter_name + "_to")
101-
self.used_parameters[self.parameter_name + "_to"] = value
107+
self.used_parameters[self.parameter_name + "_to"] = (
108+
value[0] if isinstance(value, list) else value
109+
)
102110

103111
def queryset(self, request: HttpRequest, queryset: QuerySet) -> QuerySet:
104112
filters = {}
@@ -302,11 +310,17 @@ def __init__(
302310

303311
if self.parameter_name + "_from" in params:
304312
value = params.pop(self.field_path + "_from")
305-
self.used_parameters[self.field_path + "_from"] = value
313+
value = value[0] if isinstance(value, list) else value
314+
315+
if value not in EMPTY_VALUES:
316+
self.used_parameters[self.field_path + "_from"] = value
306317

307318
if self.parameter_name + "_to" in params:
308319
value = params.pop(self.field_path + "_to")
309-
self.used_parameters[self.field_path + "_to"] = value
320+
value = value[0] if isinstance(value, list) else value
321+
322+
if value not in EMPTY_VALUES:
323+
self.used_parameters[self.field_path + "_to"] = value
310324

311325
def queryset(self, request: HttpRequest, queryset: QuerySet) -> QuerySet:
312326
filters = {}

src/unfold/contrib/filters/templates/unfold/filters/filters_date_range.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
{% with choices.0 as choice %}
44
<div class="flex flex-col mb-6">
5-
<h3 class="font-medium mb-4 text-sm">{% blocktrans with filter_title=title %}By {{ filter_title }}{% endblocktrans %}</h3>
5+
<h3 class="font-medium mb-4 text-gray-700 text-sm dark:text-gray-200">{% blocktrans with filter_title=title %}By {{ filter_title }}{% endblocktrans %}</h3>
66

77
<div class="flex flex-col space-y-4">
88
{% for field in choice.form %}

src/unfold/contrib/filters/templates/unfold/filters/filters_datetime_range.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
{% with choices.0 as choice %}
44
<div class="flex flex-col mb-6">
5-
<h3 class="font-medium mb-4 text-sm">{% blocktrans with filter_title=title %}By {{ filter_title }}{% endblocktrans %}</h3>
5+
<h3 class="font-medium mb-4 text-gray-700 text-sm dark:text-gray-200">{% blocktrans with filter_title=title %}By {{ filter_title }}{% endblocktrans %}</h3>
66

77
<div class="flex flex-col space-y-4">
88
{% for field in choice.form %}

src/unfold/contrib/filters/templates/unfold/filters/filters_numeric_range.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
{% with choices.0 as choice %}
44
<div class="flex flex-col mb-6">
5-
<h3 class="font-medium mb-4 text-sm">{% blocktrans with filter_title=title %}By {{ filter_title }}{% endblocktrans %}</h3>
5+
<h3 class="font-medium mb-4 text-gray-700 text-sm dark:text-gray-200">{% blocktrans with filter_title=title %}By {{ filter_title }}{% endblocktrans %}</h3>
66

77
<div class="flex flex-row gap-4">
88
{% for field in choice.form %}

src/unfold/contrib/filters/templates/unfold/filters/filters_numeric_single.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
{% with choices.0 as choice %}
44
<div class="flex flex-col mb-6">
5-
<h3 class="font-medium mb-4 text-sm">{% blocktrans with filter_title=title %}By {{ filter_title }}{% endblocktrans %}</h3>
5+
<h3 class="font-medium mb-4 text-gray-700 text-sm dark:text-gray-200">{% blocktrans with filter_title=title %}By {{ filter_title }}{% endblocktrans %}</h3>
66

77
{% for field in choice.form %}
88
<div class="flex flex-row flex-wrap group relative{% if field.errors %} errors{% endif %}">

src/unfold/contrib/filters/templates/unfold/filters/filters_numeric_slider.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
{% with choices.0 as choice %}
55
<div class="admin-numeric-filter-wrapper mb-6">
6-
<h3 class="font-medium mb-4 text-sm">
6+
<h3 class="font-medium mb-4 text-gray-700 text-sm dark:text-gray-200">
77
{% blocktrans with filter_title=title %}By {{ filter_title }}{% endblocktrans %}
88
</h3>
99

src/unfold/templates/admin/filter.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ <h3 class="font-medium mb-4 text-gray-700 text-sm dark:text-gray-200">
55
{% blocktranslate with filter_title=title %} By {{ filter_title }} {% endblocktranslate %}
66
</h3>
77

8+
{% for choice in choices %}
9+
{% if choice.selected %}
10+
<input type="hidden" name="{{ spec.lookup_kwarg }}" value="{{ spec.lookup_val.0 }}" />
11+
{% endif %}
12+
{% endfor %}
13+
814
{% if spec|class_name == "BooleanFieldListFilter" %}
915
<ul class="border-l-4 border-gray-200 flex pl-4 py-2 text-gray-500 text-sm dark:border-gray-700">
1016
{% for choice in choices %}
11-
<li class="{% if choice.selected %}font-medium text-primary-600 underline{% else %}text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200{% endif %} pr-4">
17+
<li class="{% if choice.selected %}font-medium text-primary-500 underline dark{% else %}text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200{% endif %} pr-4">
1218
<a href="{{ choice.query_string|iriencode }}" title="{{ choice.display }}">
1319
{{ choice.display }}
1420
</a>
@@ -18,7 +24,7 @@ <h3 class="font-medium mb-4 text-gray-700 text-sm dark:text-gray-200">
1824
{% else %}
1925
<ul class="border-l-4 border-gray-200 flex flex-col pl-4 py-4 text-gray-500 text-sm dark:border-gray-700">
2026
{% for choice in choices %}
21-
<li class="mb-4 last:mb-0 {% if choice.selected %}font-medium text-primary-600 underline {% else %}text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200{% endif %}">
27+
<li class="mb-4 last:mb-0 {% if choice.selected %}font-medium text-primary-500 underline {% else %}text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200{% endif %}">
2228
<a href="{{ choice.query_string|iriencode }}" title="{{ choice.display }}">
2329
{{ choice.display }}
2430
</a>

src/unfold/templates/unfold/change_list_filter.html

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div id="changelist-filter" class="backdrop-blur-sm bg-opacity-80 bg-gray-900 flex fixed inset-0 z-50" x-show="filterOpen">
44
<label for="show-filters" id="changelist-filter-close" class="flex-grow"></label>
55

6-
<div class="bg-white flex mr-4 my-4 overflow-hidden rounded shadow-sm w-96 dark:bg-gray-800">
6+
<div class="bg-white flex mr-4 my-4 overflow-hidden rounded shadow-sm w-96 dark:bg-gray-800" x-on:click.outside="filterOpen = false">
77
<div class="flex-grow h-full overflow-auto relative">
88
<h3 class="border-b flex font-medium mb-6 px-6 py-4 text-gray-700 text-sm dark:text-gray-200 dark:border-gray-700">
99
{% trans "Filter results" %}
@@ -22,12 +22,6 @@ <h3 class="border-b flex font-medium mb-6 px-6 py-4 text-gray-700 text-sm dark:t
2222
<div class="px-6{% if cl.model_admin.list_filter_submit %} pb-16{% endif %}">
2323
{% if cl.model_admin.list_filter_submit %}
2424
<form method="get">
25-
26-
{% for k, v in request.GET.items %}
27-
{% if not k == choice.parameter_name|add:'_from' and not k == choice.parameter_name|add:'_to' %}
28-
<input type="hidden" name="{{ k }}" value="{{ v }}">
29-
{% endif %}
30-
{% endfor %}
3125
{% endif %}
3226

3327

0 commit comments

Comments
 (0)