Skip to content

Commit

Permalink
urlparams should leave multi-value query string params alone. [bug 61…
Browse files Browse the repository at this point in the history
…4092]
  • Loading branch information
James Socol committed Nov 23, 2010
1 parent 768b040 commit 0fd3fbf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 7 additions & 5 deletions apps/sumo/helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import cgi
import datetime
import json as jsonlib
import re
import urlparse

from django.conf import settings
from django.http import QueryDict
from django.utils.encoding import smart_unicode, smart_str
from django.utils.http import urlencode
from django.utils.tzinfo import LocalTimezone
Expand Down Expand Up @@ -51,11 +51,13 @@ def urlparams(url_, hash=None, **query):
fragment = hash if hash is not None else url_.fragment

q = url_.query
query_dict = dict(urlparse.parse_qsl(smart_str(q))) if q else {}
query_dict.update(query.items())
query_dict = (QueryDict(smart_str(q), mutable=True) if
q else QueryDict('', mutable=True))
for k, v in query.items():
query_dict[k] = v # Replace, don't append.

query_string = urlencode([(k, v) for k, v in query_dict.items() if
v is not None])
query_string = urlencode([(k, v) for k, l in query_dict.lists() for
v in l if v is not None])
new = urlparse.ParseResult(url_.scheme, url_.netloc, url_.path,
url_.params, query_string, fragment)
return new.geturl()
Expand Down
8 changes: 6 additions & 2 deletions apps/sumo/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def test_urlparams_valid(self):
def test_urlparams_query_string(self):
eq_(u'/foo?a=foo&b=bar', urlparams('/foo?a=foo', b='bar'))

def test_urlparams_multivalue(self):
eq_(u'/foo?a=foo&a=bar', urlparams('/foo?a=foo&a=bar'))
eq_(u'/foo?a=bar', urlparams('/foo?a=foo', a='bar'))

def test_profile_url(self):
user = User.objects.create(pk=500000, username=u'testuser')
eq_(u'/tiki-user_information.php?locale=en-US&userId=500000',
Expand Down Expand Up @@ -87,8 +91,8 @@ def test_label_with_help(self):
class TestDateTimeFormat(TestCase):

def setUp(self):
url = reverse('forums.threads', args=[u'testslug'])
self.context = {'request': test_utils.RequestFactory().get(url)}
url_ = reverse('forums.threads', args=[u'testslug'])
self.context = {'request': test_utils.RequestFactory().get(url_)}
self.context['request'].locale = u'en-US'

def test_today(self):
Expand Down

0 comments on commit 0fd3fbf

Please sign in to comment.