utils.path_with_added_args() improvements

* Now covered by unit tests
* Preserves original order
* Can handle multiple args of the same name, e.g. ?bar=1&bar=2
This commit is contained in:
Simon Willison 2018-05-12 18:35:25 -03:00
commit 70ff615f1b
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
3 changed files with 29 additions and 9 deletions

View file

@ -150,17 +150,17 @@ def path_with_added_args(request, args, path=None):
if isinstance(args, dict):
args = args.items()
arg_keys = set(a[0] for a in args)
current = [
(key, value)
for key, value in request.raw_args.items()
if key not in arg_keys
]
current = []
for key, values in request.args.items():
current.extend(
[(key, value) for value in values if key not in arg_keys]
)
current.extend([
(key, value)
for key, value in args
if value is not None
])
query_string = urllib.parse.urlencode(sorted(current))
query_string = urllib.parse.urlencode(current)
if query_string:
query_string = '?{}'.format(query_string)
return path + query_string