accessing tables using web2py scheduler

71 views
Skip to first unread message

Cory

unread,
Jun 24, 2014, 6:53:57 PM6/24/14
to web...@googlegroups.com
Hi,
I am trying to access tables within my scheduler function. The tables I am trying to access require "@auth.requires_login()".
Am I able to access these tables within my scheduler.py model file? right now I am passing table row fields as parameters
to the scheduler function and it is very messy. 
In scheduler.py I have tried to include:

db = DAL("postgres://postgres......blah)
from gluon.tools import Auth
auth = Auth(db)

if auth.is_logged_in():


thanks!


Niphlod

unread,
Jun 25, 2014, 5:55:06 AM6/25/14
to web...@googlegroups.com
why on hearth should you define tables based on the fact that a user is logged-in or not ?
Scheduler functions are executed in a world that does not care for the usual request/response/session environment, and surely they do no count to be "logged in"...  

Cory Bekker

unread,
Jun 25, 2014, 10:46:42 AM6/25/14
to web...@googlegroups.com

I am trying to access tables that require the user to be logged in.  Am I able to access these tables within my scheduler functions?

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/_-MWC1WrOkY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Niphlod

unread,
Jun 25, 2014, 10:52:09 AM6/25/14
to web...@googlegroups.com
there's no such thing (conceptually) as "tables that exists only if the user is logged in".
the concept is "a user logged in can read/update/delete/etc some tables". That's what you do in "controllers". 
tables are "defined" (not accessed) instead in "models", so are available to everybody, scheduler included.

Massimo Di Pierro

unread,
Jun 25, 2014, 11:54:55 AM6/25/14
to web...@googlegroups.com
You can do:

if auth.user and "some other condition":
    db.define_table('thing',Field('name'))

so the table is always in the DB but web2py only sees it conditionally. Sometimes I do that for speed reasons.

cory bekker

unread,
Jun 25, 2014, 11:55:48 AM6/25/14
to web...@googlegroups.com
Here is a better example of what I mean. 

in controller:

@auth.requires_login()  
def foo():
row = db(db.mytable.id == auth.user_id).select().first()
return dict()

in scheduler.py 

def sched_function():
row = db(db.mytable.id == auth.user_id).select().first()
return dict()


I am unable to fetch a row doing the same syntax in my scheduler function.
It returns an empty row. I also tried to include "@auth.requires_login"  but
it doesn't seem to work. 

sorry for the miscomunnication

Niphlod

unread,
Jun 25, 2014, 11:57:41 AM6/25/14
to web...@googlegroups.com
ok, the question at this point is really, what you'd expect "auth.user_id" to be assumed as a value when you run inside a queued task on the scheduler....

cory bekker

unread,
Jun 25, 2014, 12:19:50 PM6/25/14
to web...@googlegroups.com
ah yes, you got me to think differently about the problem.  Thanks Nophlod, I will try a different approach. 
Reply all
Reply to author
Forward
0 new messages