Ticket #1456: url_multi_value.patch

File url_multi_value.patch, 1.9 kB (added by imix, 1 year ago)
  • turbogears/controllers.py

    old new  
    485485 
    486486    Query parameters for the URL can be passed in as a dictionary in 
    487487    the second argument *or* as keyword parameters. 
     488 
     489    Values which are a list or a tuple are used to create multiple 
     490    key-value pairs. 
    488491    """ 
    489492    if not isinstance(tgpath, basestring): 
    490493        tgpath = "/".join(list(tgpath)) 
     
    504507    for key, value in tgparams.iteritems(): 
    505508        if value is None: 
    506509            continue 
    507         if isinstance(value, unicode): 
    508             value = value.encode("utf8") 
    509         args.append("%s=%s" % (key, urllib.quote(str(value)))) 
     510        if isinstance(value, (list, tuple)): 
     511            pairs = [(key, v) for v in value] 
     512        else: 
     513            pairs = [(key, value)] 
     514        for (k,v) in pairs: 
     515            if isinstance(value, unicode): 
     516                value = value.encode("utf8") 
     517            args.append("%s=%s" % (k, urllib.quote(str(v)))) 
    510518    if args: 
    511519        result += "?" + "&".join(args) 
    512520    return result 
  • turbogears/tests/test_controllers.py

    old new  
    564564            print e.urls 
    565565            assert "http://localhost/coolsite/root/subthing/foo" in e.urls 
    566566 
     567    def test_multi_values(self): 
     568        testutil.createRequest("/") 
     569        assert url("/foo", bar=[1,2]) in \ 
     570                ["/foo?bar=1&bar=2", "/foo?bar=2&bar=1"] 
     571        assert url("/foo", bar=("asdf","qwer")) in \ 
     572                ["/foo?bar=qwer&bar=asdf", "/foo?bar=asdf&bar=qwer"] 
    567573 
    568574    def tearDown(self): 
    569575        turbogears.config.update({"server.webpath" : ""})