>>> URL(r=request, c='hello', f='world', args=['hi'], vars={'q':'greetings'}, >>> anchor='the_world') '/welcome/hello/world/hi#the_world?q=greetings' >>> URL(c='hello', f='world', args=['hi'], vars={'q':'greetings'}, >>> anchor='the_world') '/welcome/hello/world/hi#the_world?q=greetings'
Attached is the diff This should not effect any current usages of URL, but from this point forward we shouldn't have to specify r=request! -- Thadeus On Wed, Jul 28, 2010 at 5:21 PM, mdipierro <mdipie...@cs.depaul.edu> wrote: > ok > > On Jul 28, 4:52 pm, Thadeus Burgess <thade...@thadeusb.com> wrote: >> For a little more advanced version of what Massimo just posted: >> >> This allows you to use both. >> >> URL(r=request, c=<controller>, f=.....) >> and >> URL(c=<controller>, f=<function>.....) >> >> http://packages.python.org/web2py_utils/init.html#gurlhttp://hg.thadeusb.com/Web/web2py_utils/file/697470f78d16/web2py_util... >> >> The reason we have to pass the request to URL each time is because it >> is just imported into the context. Since it is just a python function, >> it has no knowledge of request. >> >> Why can't we do something like gURL from within web2py? When building >> the context, why can't we make URL a loaded function that will get >> request passed to it, exactly how gURL works. >> >> Effectively, we won't have to pass request into URL function anymore... ever. >> >> If your interested, I can work on the patch. >> >> -- >> Thadeus >> >> On Wed, Jul 28, 2010 at 3:56 PM, mdipierro <mdipie...@cs.depaul.edu> wrote: >> > 1) It is a bit more complex because of static, appadmin, and admin. >> > You can map >> >> > /function/var1/var2 >> >> > into >> >> > /app/controller/function/var1/var2 >> >> > using a file routes.py >> > ------- begin file >> > routes_in=[ >> > ('/admin/$anything','/admin/$anything'), >> > ('/static/$anything','/app/static/$anything'), >> > ('/appadmin/$anything','/app/appadmin/$anything'), >> > ('/$anything','/app/default/$anything'), >> > ] >> > routes_out=[(y,x) for (x,y) in routes_in] >> > ----- end file >> >> > 2) in db.py define >> >> > def url(f,args={},vars={}): >> > return URL(r=request,f=f,args=args,vars=vars) >> > def go(f,args={},vars={},flash=''): >> > session.flash=flash >> > return go(url(f=f,args=args,vars=vars)) >> >> > and use >> >> > url('index') >> >> > or >> >> > go('index',flash='you are being redirected') >> >> > I do it all the time >> >> > On Jul 28, 3:29 pm, VP <vtp2...@gmail.com> wrote: >> >> I'm a newbie, so my questions probably have easy answers, but anyway. >> >> Among a few things, there are two I don't understand and think can be >> >> simplified. >> >> >> 1. this url: app/controller/function/var1/var2 >> >> >> For me at least, most of the time I probably have only one >> >> controller. If there is one controller, may be we should get rid of >> >> "controller" in the url? This will be sufficient: app/f/a/b/c >> >> >> 2. Similarly, most of the time I have only one app (current app). >> >> But I use URL() a lot and every time I have to pass in request like >> >> this URL(r=request, f='foo'). >> >> >> Why do I have to pass in request if I have only one app, one >> >> controller? >> >> >> Furthermore, isn't request supposed to be global? If so, do we have >> >> to pass it in? >
diff -r a3a934f1f4f5 gluon/compileapp.py --- a/gluon/compileapp.py Wed Jul 28 16:33:43 2010 -0500 +++ b/gluon/compileapp.py Wed Jul 28 17:51:22 2010 -0500 @@ -216,6 +216,11 @@ environment = {} for key in html.__all__: environment[key] = getattr(html, key) + + # Overwrite the URL function with a proxy + # url function which contains this request. + environment['URL'] = html._gURL(request) + for key in validators.__all__: environment[key] = getattr(validators, key) if not request.env: diff -r a3a934f1f4f5 gluon/html.py --- a/gluon/html.py Wed Jul 28 16:33:43 2010 -0500 +++ b/gluon/html.py Wed Jul 28 17:51:22 2010 -0500 @@ -200,6 +200,25 @@ if regex_crlf.search(url): raise SyntaxError, 'CRLF Injection Detected' return rewrite.filter_out(url, env) + +def _gURL(request): + """ + A proxy function for URL which contains knowledge + of a given request object. + + Usage is exactly like URL except you do not have + to specify r=request! + """ + def _URL(*args, **kwargs): + # If they use URL as just passing along + # args, we don't want to overwrite it and + # cause issues. + if not kwargs.has_key('r') and len(args) < 4: + kwargs['r'] = request + + return URL(*args, **kwargs) + _URL.__doc__ = URL.__doc__ + return _URL ON = True