Ticket #1649: url.patch

File url.patch, 3.4 kB (added by stefanha, 1 year ago)
  • tg/tests/test_tg_controller_dispatch.py

    old new  
    11# -*- coding: utf-8 -*- 
    22 
    33import tg, pylons 
    4 from tg.controllers import TurboGearsController 
     4from tg.controllers import TurboGearsController, url 
    55from pylons.decorators import expose 
    66from routes import Mapper 
    77from routes.middleware import RoutesMiddleware 
     
    7777        tg.flash("Wow, flash!") 
    7878        return tg.get_flash() 
    7979 
     80    @expose() 
     81    def do_url(self, tgpath, **kw): 
     82        return url(tgpath.split(','), **kw) 
     83 
    8084class TestTGController(TestWSGIController): 
    8185    def __init__(self, *args, **kargs): 
    8286        TestWSGIController.__init__(self, *args, **kargs) 
     
    143147        resp = self.app.get('/flash_unicode').follow() 
    144148        content = resp.body.decode('utf8') 
    145149        self.failUnless(u'Привет, мир!' in content, resp) 
     150 
     151    def test_url(self): 
     152        resp = self.app.get('/do_url?tgpath=/foo') 
     153        assert '/foo' in resp.body 
     154        resp = self.app.get('/do_url?tgpath=foo,bar') 
     155        assert 'foo/bar' in resp.body 
     156        resp = self.app.get('/do_url?tgpath=/foo&bar=1&baz=2') 
     157        assert resp.body in ['/foo?bar=1&baz=2', '/foo?baz=2&bar=1'] 
     158        resp = self.app.get('/do_url?tgpath=/foo&bar=1&baz=2') 
     159        assert resp.body in ['/foo?bar=1&baz=2', '/foo?baz=2&bar=1'] 
  • tg/controllers.py

    old new  
    1818    def _dispatch_call(self): 
    1919        return self._perform_call(None, None) 
    2020 
    21 def redirect(url, params={}, **kw): 
     21def redirect(redirect_url, redirect_params={}, **kw): 
    2222    """Generate an HTTP redirect. The function raises an exception internally, 
    2323    which is handled by the framework. The URL may be either absolute (e.g. 
    2424    http://example.com or /myfile.html) or relative. Relative URLs are 
     
    2727    browser; if the request is POST, the browser will issue GET for the second 
    2828    request. 
    2929    """ 
    30     url = urlparse.urljoin(request.path_info, url) 
    31     params.update(kw) 
    32     if params: 
    33         url += (('?' in url) and '&' or '?') + urllib.urlencode(params, True) 
    34     found = HTTPFound(url) 
     30    tgpath = url(redirect_url, redirect_params, **kw) 
     31    tgpath = urlparse.urljoin(request.path_info, tgpath) 
     32    found = HTTPFound(tgpath) 
    3533    # Merging cookies in global response into redirect 
    3634    for c in response.cookies.values(): 
    3735        found.headers.append(('Set-Cookie', c.output(header=''))) 
    3836    raise found 
    3937 
    40 def url(tgpath, tgparams=None, **kw): 
     38def url(tgpath, tgparams={}, **kw): 
    4139    """Broken url() re-implementation from TG1. 
    4240 
    4341    See #1649 for more info. 
    4442    """ 
    45     from tg import request 
    4643    if not isinstance(tgpath, basestring): 
    4744        tgpath = "/".join(list(tgpath)) 
    48     path = request.relative_url(tgpath) 
    49     print 'path', path 
    50     base_url = request.path_url 
    51     return path[len(base_url):] 
     45    tgpath = urlparse.urljoin(request.path_info, tgpath) 
     46    if tgpath.startswith(request.path_info): 
     47        tgpath = tgpath[len(request.path_info):] 
     48    tgparams = tgparams.copy() 
     49    tgparams.update(kw) 
     50    if tgparams: 
     51        tgpath += (('?' in tgpath) and '&' or '?') + urllib.urlencode(tgparams, True) 
     52    return tgpath