From af5fa07ca2fcc48ab357c0db4e1301bb960addca Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Fri, 27 Sep 2013 23:45:41 -0500 Subject: support a None value in query string parameters --- CHANGES.txt | 10 ++++++++++ pyramid/encode.py | 6 ++++++ pyramid/tests/test_encode.py | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index 8b2210a99..e972c08c3 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -6,6 +6,16 @@ Documentation - Added a "Quick Tutorial" to go with the Quick Tour +Backwards Incompatibilities +--------------------------- + +- The key/values in the ``_query`` parameter of ``request.route_url`` and the + ``query`` parameter of ``request.resource_url`` (and their variants), used + to encode a value of ``None`` as the string ``'None'``, leaving the resulting + query string to be ``a=b&key=None``. The value is now dropped in this + situation, leaving a query string of ``a=b&key``. + See https://github.com/Pylons/pyramid/issues/1119 + 1.5a2 (2013-09-22) ================== diff --git a/pyramid/encode.py b/pyramid/encode.py index 65bc95032..9341f7665 100644 --- a/pyramid/encode.py +++ b/pyramid/encode.py @@ -32,6 +32,10 @@ def urlencode(query, doseq=True): See the Python stdlib documentation for ``urllib.urlencode`` for more information. + + .. versionchanged:: 1.5 + In a key/value pair, if the value is ``None`` then it will be + dropped from the resulting output. """ try: # presumed to be a dictionary @@ -50,6 +54,8 @@ def urlencode(query, doseq=True): x = _enc(x) result += '%s%s=%s' % (prefix, k, x) prefix = '&' + elif v is None: + result += '%s%s' % (prefix, k) else: v = _enc(v) result += '%s%s=%s' % (prefix, k, v) diff --git a/pyramid/tests/test_encode.py b/pyramid/tests/test_encode.py index 736ecb5b3..1e0ecbe20 100644 --- a/pyramid/tests/test_encode.py +++ b/pyramid/tests/test_encode.py @@ -41,6 +41,14 @@ class UrlEncodeTests(unittest.TestCase): result = self._callFUT({'a':1}) self.assertEqual(result, 'a=1') + def test_None_value(self): + result = self._callFUT([('a', None)]) + self.assertEqual(result, 'a') + + def test_None_value_with_prefix(self): + result = self._callFUT([('a', '1'), ('b', None)]) + self.assertEqual(result, 'a=1&b') + class URLQuoteTests(unittest.TestCase): def _callFUT(self, val, safe=''): from pyramid.encode import url_quote -- cgit v1.2.3