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 1 1 # -*- coding: utf-8 -*- 2 2 3 3 import tg, pylons 4 from tg.controllers import TurboGearsController 4 from tg.controllers import TurboGearsController, url 5 5 from pylons.decorators import expose 6 6 from routes import Mapper 7 7 from routes.middleware import RoutesMiddleware … … 77 77 tg.flash("Wow, flash!") 78 78 return tg.get_flash() 79 79 80 @expose() 81 def do_url(self, tgpath, **kw): 82 return url(tgpath.split(','), **kw) 83 80 84 class TestTGController(TestWSGIController): 81 85 def __init__(self, *args, **kargs): 82 86 TestWSGIController.__init__(self, *args, **kargs) … … 143 147 resp = self.app.get('/flash_unicode').follow() 144 148 content = resp.body.decode('utf8') 145 149 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 18 18 def _dispatch_call(self): 19 19 return self._perform_call(None, None) 20 20 21 def redirect( url,params={}, **kw):21 def redirect(redirect_url, redirect_params={}, **kw): 22 22 """Generate an HTTP redirect. The function raises an exception internally, 23 23 which is handled by the framework. The URL may be either absolute (e.g. 24 24 http://example.com or /myfile.html) or relative. Relative URLs are … … 27 27 browser; if the request is POST, the browser will issue GET for the second 28 28 request. 29 29 """ 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) 35 33 # Merging cookies in global response into redirect 36 34 for c in response.cookies.values(): 37 35 found.headers.append(('Set-Cookie', c.output(header=''))) 38 36 raise found 39 37 40 def url(tgpath, tgparams= None, **kw):38 def url(tgpath, tgparams={}, **kw): 41 39 """Broken url() re-implementation from TG1. 42 40 43 41 See #1649 for more info. 44 42 """ 45 from tg import request46 43 if not isinstance(tgpath, basestring): 47 44 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