Re: [web2py] Re: Odd auth problem
On Sat, 17 Mar 2012 15:03:39 -0700 (PDT), abasta...@gmail.com said: Instead, you can use auth.user_id, which will simply return None when the user isn't logged in rather than an error. Thanks, that's fixed the problem. -- You can have everything in life you want if you help enough other people get what they want - Zig Ziglar. Who did you help today?
[web2py] Re: Odd auth problem
i suspect it's an order of operations issue. i'm no expert, but i bet the parser parses the function definition when the file is loaded and tries to make use of an auth parameter that is not yet initialized. (the decorator is not run until the code is executedwhich is sometime after parsing) perhaps the reason you see this after a few days is because the user got logged out due to timeout/session expiration. given that controller methods can't take parameters.can't you decorate the controller and then not call this method unless you are logged in? and in the method itself make the default for user_id=None and set user_id to auth.user.id if user_id is None? cfh On Saturday, March 17, 2012 9:54:54 AM UTC-7, backseat wrote: I have a function whose definition starts as follows: @auth.requires_login() def rbb_time(category_id, from_date, to_date, user_id=auth.user.id): It works fine. I'll leave the development of this application for a few days, come back, run it, and get: Traceback (most recent call last): File /home/kae/hg/kae/web2py.1.99.4.pytrack2/gluon/restricted.py, line 204, in restricted exec ccode in environment File [...]/controllers/default.py, line 100, in module def rbb_time(category_id, from_date, to_date, user_id=auth.user.id): AttributeError: 'NoneType' object has no attribute 'id' The only way to fix it (and this isn't 100% reliable) is to comment out all of the @auth.requires_login() lines, hard-code the above function definition to end user_id=11 (the UID I was logged in as), and then call /default/user/logout. Can anyone offer any suggestions as to what is going on here? Thanks, Keith
[web2py] Re: Odd auth problem
When the user is not logged in, auth.user is None, so you get an error when trying to access auth.user.id. Instead, you can use auth.user_id, which will simply return None when the user isn't logged in rather than an error. See the callout at the end of this section in the book: http://web2py.com/books/default/chapter/29/9#Authentication. Anthony On Saturday, March 17, 2012 12:54:54 PM UTC-4, backseat wrote: I have a function whose definition starts as follows: @auth.requires_login() def rbb_time(category_id, from_date, to_date, user_id=auth.user.id): It works fine. I'll leave the development of this application for a few days, come back, run it, and get: Traceback (most recent call last): File /home/kae/hg/kae/web2py.1.99.4.pytrack2/gluon/restricted.py, line 204, in restricted exec ccode in environment File [...]/controllers/default.py, line 100, in module def rbb_time(category_id, from_date, to_date, user_id=auth.user.id): AttributeError: 'NoneType' object has no attribute 'id' The only way to fix it (and this isn't 100% reliable) is to comment out all of the @auth.requires_login() lines, hard-code the above function definition to end user_id=11 (the UID I was logged in as), and then call /default/user/logout. Can anyone offer any suggestions as to what is going on here? Thanks, Keith