Skip to content

Commit

Permalink
Backported the url changes from 2.0.
Browse files Browse the repository at this point in the history
url function now behaves properly while given an empty dict.
  • Loading branch information
trollfot committed Jun 2, 2010
1 parent e61c319 commit a052693
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
3 changes: 1 addition & 2 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ Changes
1.13.5 (unreleased)
-------------------

- Nothing changed yet.

- Fix the url() function to behave properly while passed an empty data dict.

1.13.4 (2010-06-01)
-------------------
Expand Down
8 changes: 8 additions & 0 deletions src/grokcore/view/ftests/url/url_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@
>>> url(request, herd, None, data=dict(name="Peter"))
'http://127.0.0.1/herd?name=Peter'
Providing an empty dict gives the same result than giving None:
>>> url(request, herd, data={})
'http://127.0.0.1/herd'
>>> url(request, herd, data=None)
'http://127.0.0.1/herd'
Since order in dictionairies is arbitrary we'll test the presence of multiple
keywords by using find()
Expand Down
11 changes: 6 additions & 5 deletions src/grokcore/view/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@
from zope.traversing.browser.interfaces import IAbsoluteURL
from zope.traversing.browser.absoluteurl import _safe as SAFE_URL_CHARACTERS


def url(request, obj, name=None, data=None):
url = getMultiAdapter((obj, request), IAbsoluteURL)()
if name is not None:
url += '/' + urllib.quote(name.encode('utf-8'), SAFE_URL_CHARACTERS)
if data is None:

if not data:
return url

if not isinstance(data, dict):
raise TypeError('url() data argument must be a dict.')

for k,v in data.items():
if isinstance(v, unicode):
data[k] = v.encode('utf-8')
if isinstance(v, (list, set, tuple)):
data[k] = [
isinstance(item, unicode) and item.encode('utf-8')
or item for item in v]

return url + '?' + urllib.urlencode(data, doseq=True)

0 comments on commit a052693

Please sign in to comment.