aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/utils
diff options
context:
space:
mode:
authorTom Christie2015-01-16 20:36:09 +0000
committerTom Christie2015-01-16 20:36:09 +0000
commitdc18040ba47325afb38ae62042a6103bfd794c4b (patch)
treef36aecf23dfe09aceba53c218350e9c2032263fe /rest_framework/utils
parentf13fcba9a9f41f7e00e0ea8956fcc65ca168c76c (diff)
parent86d2774cf30351fd4174e97501532056ed0d8f95 (diff)
downloaddjango-rest-framework-dc18040ba47325afb38ae62042a6103bfd794c4b.tar.bz2
Merge pull request #2419 from tomchristie/include-pagination-in-browsable-api
Include pagination control in browsable API.
Diffstat (limited to 'rest_framework/utils')
-rw-r--r--rest_framework/utils/urls.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/rest_framework/utils/urls.py b/rest_framework/utils/urls.py
new file mode 100644
index 00000000..880ef9ed
--- /dev/null
+++ b/rest_framework/utils/urls.py
@@ -0,0 +1,25 @@
+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))