Skip to content

Commit 016aa81

Browse files
committed
simplify input processing, make sure csv path won't be executed for native array input values
1 parent 758dfd4 commit 016aa81

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

django_filters/widgets.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,23 +178,32 @@ class QueryArrayWidget(BaseCSVWidget, forms.TextInput):
178178
"""
179179
Enables request query array notation that might be consumed by MultipleChoiceFilter
180180
181-
1. Values can be provided as csv string: ?foo=bar,baz
181+
1. Values can be provided as csv string: ?foo=bar,baz
182182
2. Values can be provided as query array: ?foo[]=bar&foo[]=baz
183+
3. Values can be provided as query array: ?foo=bar&foo=baz
184+
185+
Note: Duplicate and empty values are skipped from results
183186
"""
184187

185188
def value_from_datadict(self, data, files, name):
186189
if not isinstance(data, MultiValueDict):
187190
data = MultiValueDict(data)
188191

189-
request_data = data.getlist(name, data.getlist('%s[]' % name))
190-
if isinstance(request_data, string_types):
191-
request_data = [request_data]
192+
values_list = data.getlist(name, data.getlist('%s[]' % name)) or []
192193

193-
if not request_data:
194-
return []
194+
if isinstance(values_list, string_types):
195+
values_list = [values_list]
195196

196-
extracted_data = set()
197-
for item in request_data:
198-
extracted_data.update([x.strip() for x in item.rstrip(',').split(',') if x])
197+
# apparently its an array, so no need to process it's values as csv
198+
# ?foo=1&foo=2 -> data.getlist(foo) -> foo = [1, 2]
199+
# ?foo[]=1&foo[]=2 -> data.getlist(foo[]) -> foo = [1, 2]
200+
if len(values_list) > 1:
201+
ret = [x for x in values_list if x]
202+
elif len(values_list) == 1:
203+
# treat first element as csv string
204+
# ?foo=1,2 -> data.getlist(foo) -> foo = ['1,2']
205+
ret = [x.strip() for x in values_list[0].rstrip(',').split(',') if x]
206+
else:
207+
ret = []
199208

200-
return list(extracted_data)
209+
return list(set(ret))

0 commit comments

Comments
 (0)