Cool!.

It works perfectlly on controllers files, but causes infinite recursion in 
auth model file.

So if you want to apply it to your whole app, writing it once and in one 
place, this can be used in auth model, after defining auth:

if request.url != auth.settings.login_url:
    auth.requires_login()(lambda: None)()

Thanks!


El viernes, 13 de febrero de 2015, 0:35:52 (UTC+1), Anthony escribió:
>
> A decorator is just a special type of function that takes a callable and 
> returns another callable. You typically use it via the "@" syntax, but you 
> don't have to use it that way.
>
> @auth.requires_login()
> def myfunc():
>     return dict()
>
> is equivalent to:
>
> def myfunc():
>     return dict()
> myfunc = auth.requires_login()(myfunc)
>
> You are simply passing myfunc to a function and getting back a new 
> function.
>
> In this case, we don't really have a function to decorate, so we just pass 
> in a dummy function (i.e., lambda: None). What we really want is for 
> auth.requires_login() to execute its redirect logic in case the user isn't 
> logged in. Otherwise, it should just do nothing and move on.
>
> Anthony
>
> On Thursday, February 12, 2015 at 4:29:42 PM UTC-5, Tom Campbell wrote:
>>
>> To take advantage of the decorators at a file level, you can also do this 
>>> at the top level of a file:
>>>
>>> auth.requires_login()(lambda: None)()
>>>
>>> The decorators ultimately call auth.requires, which itself returns a 
>>> decorator. The above passes a dummy function to that decorator and then 
>>> simply calls it. Note, you don't prepend with "@" in this case, as you are 
>>> not decorating a Python function.
>>>
>>> I think that's brilliant... but I don't quite understand. Without the 
>> decorators how to the functions in the file get forced to go through 
>> auth.requires_login()? 
>>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to