I'm attempting to create a script which will utilize the web2py DAL to access a remote database. I'd like the app to have access to another web2py application's auth table. The script is as follows:
if MODE == 'server':
DIR_GLUON = '...'
else:
DIR_GLUON = '...'
sys_path.insert(0, DIR_GLUON)
from gluon import DAL, Field, current
from gluon.tools import Auth
from gluon.storage import Storage
import gluon.contrib.plural_rules as plural_rules
# establish DB connection
db = DAL(..., pool_size = 1, check_reserved=['all'], lazy_tables = True, fake_migrate_all = True)
auth = Auth(db)
The last line produces the following error:
Traceback (most recent call last):
File "standalone_script.py", line 39, in <module>
auth = Auth(db)
File "C:\...\web2py\gluon\tools.py", line 1754, in __init__
request = current.request
AttributeError: 'thread._local' object has no attribute 'request'
Is there a reasonable way to resolve the above error in a standalone environment? Thank you.
Authis not intended to be used outside of a web2py environment. If you just need access to theauth_usertable, the easiest option might be to define it explicitly in your code. The proper definition can be found in the book (note, you don't need to bother defining the validators if you won't be doing any form submissions).You might even be able to skip the explicit table definition if you can point the DAL to the application's
/databasesfolder (if the application is on a remote server, perhaps you could mount the remote folder, though a copy on the local machine would be much faster). Your DAL setup would look like the following (as described here):The above will create the table definitions based on the migration metadata files in
/path/to/app/databases. Note, in this case, the definitions will not include any web2py specific attributes, such as field validators (it will only include attributes needed by the database, such as field names and types as well as database constraints).Finally, depending on how you are running your code, it may be possible to execute it within the environment of your application (you would need a local copy of the application):
The above will create a web2py execution environment, run your application's model files (which will define your database tables, including the Auth tables), and then execute your code in that context. In this case, you wouldn't need to bother with any table definitions or
auto_import, as the table definitions will come from you app code.As an aside, note, you should probably disable migrations when connecting to the application's database remotely (assuming you don't want your code to result in the DAL making changes to the database schema):
In that case, there is no need for
fake_migrate_all=True-- that is only necessary if you have enabled migrations and need to generate the migration metadata files. Also note that even if you did need to usefake_migrate_all, you would only use it a single time to generate the metadata files (otherwise, it will unnecessarily generate the same set of files on every run).