mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-27 12:04:02 +03:00
26 lines
986 B
Python
26 lines
986 B
Python
from django.utils.six.moves.urllib import parse as urlparse
|
|
|
|
|
|
def replace_query_param(url, key, val):
|
|
"""
|
|
Given a URL and a key/val pair, set or replace an item in the query
|
|
parameters of the URL, and return the new URL.
|
|
"""
|
|
(scheme, netloc, path, query, fragment) = urlparse.urlsplit(url)
|
|
query_dict = urlparse.parse_qs(query)
|
|
query_dict[key] = [val]
|
|
query = urlparse.urlencode(sorted(list(query_dict.items())), doseq=True)
|
|
return urlparse.urlunsplit((scheme, netloc, path, query, fragment))
|
|
|
|
|
|
def remove_query_param(url, key):
|
|
"""
|
|
Given a URL and a key/val pair, remove an item in the query
|
|
parameters of the URL, and return the new URL.
|
|
"""
|
|
(scheme, netloc, path, query, fragment) = urlparse.urlsplit(url)
|
|
query_dict = urlparse.parse_qs(query)
|
|
query_dict.pop(key, None)
|
|
query = urlparse.urlencode(sorted(list(query_dict.items())), doseq=True)
|
|
return urlparse.urlunsplit((scheme, netloc, path, query, fragment))
|