Branko Vukelic avatar Branko Vukelic committed cc2aad3 Merge

Merged in aquavitae/django-url-tools (pull request #2)

Add toggle_param functionality

Comments (0)

Files changed (5)

     u.overload_params(bar=1)  # /foo?bar=1
     u.overload_params(bar=2)  # /foo?bar=1&bar=2
 
+UrlHelper.toggle_params(**kwargs)
+-----------------------------------
+
+This method adds or removes query parameters depending on whether they
+already exist. It looks for both a matching parameter and value, and
+adds new parameters using ``UrlHelper.overload_params``. Here is
+a simple example::
+
+    u = UrlHelper('/foo')
+    u.toggle_params(bar=1)  # /foo?bar=1
+    u.toggle_params(bar=1, foo=2)  # /foo?foo=2
+    u.toggle_params(bar=1, bar=2)  # /foo?bar=1&bar=2&foo=2
+    
 UrlHelper.get_path()
 --------------------
 
 
     /foo?bar=1&baz=2
 
+{% toggle_params %}
+---------------------
+
+This tag adds or removes parameters, depending on whether the parameter and
+value exists.  For example, if we are on a page at ``/foo?bar=1``, we can 
+toggle the state of ``bar=1`` using::
+
+    {% toggle_params request.get_full_path bar=1 %}
+
+and the output would be::
+
+    /foo
+    
+If we are on a page at ``/foo``, then the output of would be::
+
+    /foo?bar=1
+    
+Multiple parameters and values can be used.  For example, on a page at
+``/foo?bar=1&foo=2``, parameters can be toggled like this::
+
+    {% toggle_params request.get_full_path bar=3 foo=2 foo=3 %}
+    
+To give::
+
+    /foo?bar=1&bar=3&foo=3
+
 {% url_params %}
 ----------------
 

tests/test_toggle_params.py

+from __future__ import absolute_import, unicode_literals
+
+from unittest import TestCase
+
+from url_tools.templatetags.urls import toggle_params
+
+
+class toggleParamsTestCase(TestCase):
+    def test_toggle_params_basic(self):
+        self.assertEqual(toggle_params('/foo/?foo=1', foo=1), '/foo/')
+
+    def test_toggle_params_multiple(self):
+        self.assertEqual(toggle_params('/foo/?foo=1', foo=1, bar=2), '/foo/?bar=2')

tests/test_url_helper.py

         u1 = UrlHelper('/foo/bar?foo=1&bar=2')
         u2 = UrlHelper(u1)
         self.assertEqual(u2.get_full_path(), '/foo/bar?foo=1&bar=2')
+
+    def test_toggle_params_disable(self):
+        u = UrlHelper('/foo/bar?foo=1')
+        u.toggle_params(foo=1)
+        self.assertEqual(u.get_full_path(), '/foo/bar')
+
+    def test_toggle_params_enable(self):
+        u = UrlHelper('/foo/bar?foo=1')
+        u.toggle_params(bar=1)
+        self.assertEqual(u.get_full_path(), '/foo/bar?foo=1&bar=1')
+
+    def test_toggle_params_toggle_multiple(self):
+        u = UrlHelper('/foo/bar?foo=1')
+        u.toggle_params(foo=1, bar=1)
+        self.assertEqual(u.get_full_path(), '/foo/bar?bar=1')

url_tools/helper.py

                            if not x.startswith(val)]
                 self.query_dict.setlist(key, to_keep)
 
+    def toggle_params(self, **params):
+        for param, value in params.items():
+            value = unicode(value)
+            if value in self.query_dict.getlist(param):
+                self.del_params(**{param: value})
+            else:
+                self.overload_params(**{param: value})
+
     @property
     def hash(self):
         md5 = hashlib.md5()

url_tools/templatetags/urls.py

     return u.get_full_path()
 
 
+@register.simple_tag
+def toggle_params(url, **kwargs):
+    u = UrlHelper(url)
+    u.toggle_params(**kwargs)
+    return u.get_full_path()
+
+
 @register.filter(name='quote')
 def quote_param(value, safe='/'):
     return urllib.quote(value, safe)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.