Commits

nlaurance  committed 8cff83c

insert_params remove_params

  • Participants
  • Parent commits 396a050

Comments (0)

Files changed (2)

File url_tools/helper.py

 import urlparse
 import hashlib
 
-from django.http.request import QueryDict
+try:
+    from django.http.request import QueryDict
+except ImportError: # django 1.4.2
+    from django.http import QueryDict
 from django.utils.encoding import iri_to_uri
 
 
     def get_full_quoted_path(self, **kwargs):
         return urllib.quote_plus(self.get_full_path(**kwargs), safe='/')
 
+    def insert_params(self, **kwargs):
+        for key, val in kwargs.iteritems():
+            uniques = set(self.query_dict.getlist(key))
+            uniques.add(val)
+            self.query_dict.setlist(key, list(uniques))
+        return self.path
+
+    def remove_params(self, **kwargs):
+        for key, val in kwargs.iteritems():
+            to_keep = [x for x in self.query_dict.getlist(key) if not x.startswith(val)]
+            self.query_dict.setlist(key, to_keep)
+        return self.path
+
+
     def del_param(self, param):
         try:
             del self.query_dict[param]

File url_tools/templatetags/urls.py

         return url.get_full_path()
     except:
         return ''
+    
+@register.simple_tag
+def insert_params(url, **kwargs):
+    if type(url) == UrlHelper:
+        url = url.get_full_path()
+    url = UrlHelper(url)
+    try:
+        url.insert_params(**kwargs)
+        return url.get_full_path()
+    except:
+        return ''
 
+@register.simple_tag
+def remove_params(url, **kwargs):
+    if type(url) == UrlHelper:
+        url = url.get_full_path()
+    url = UrlHelper(url)
+    try:
+        url.remove_params(**kwargs)
+        return url.get_full_path()
+    except:
+        return ''
 
 @register.assignment_tag
 def url_params(url, **kwargs):