I'll probably get flamed for talking about this...
In general I really like the 'magic' that web2py offers, where it
builds a nice structure and imports all the modules and everything.
Having stuff just exist for me is really handy! I'm not saying that
this should change.
However, there are times, like when an asterisk AGI script needs to
talk to a web2py database. Rather than use some other DB API, I want
to use web2py's DAL. Basically I'm getting a telephone to talk to
web2py's DAL.
This is the core of how I did that. It took me a couple hours of
mucking about and learning web2py's codebase to figure this out.
Perhaps this should be on the wiki, but I can't figure out how to make
the wiki work for me.
I welcome better ways, but this is the cleanest way I could figure out
how to do it. I didn't try messing about with a smaller subsection of
the gluon codebase. If importing it to a contrib directory (which is
probably recommended for consistency, you will have to muck about with
the local gluon imports, to come from the module instead of from
local. This is one area where I would like web2py to change, so that
it can handle being imported easier...
Regardless this is a working(for me) example of how I did it:
#copy web2py's gluon directory to my source tree
cp -R ~/web2py/gluon .
#whenever copying code, make sure you take the license with you!
cp ~/web2py/LICENSE gluon/
here is an example .py file:
##BEGIN FILE:
#!/usr/bin/env python
# encoding: utf-8
"""
db.py - quick simple easy external interface to web2py's DAL.
author: Tara Birl
License: Public Domain
"""
from gluon.sql import DAL,Field
db = DAL('sqlite://test.db')
db.define_table('callers',
Field('userphone','string',unique=True),
Field('status','boolean',default=False),
)
if __name__=='__main__':
db.callers.insert(userphone='18775551212',status=False)
db.callers.insert(userphone='13475551212',status=True)
#DO NOT FORGET TO COMMIT!!
db.commit()
for caller in db(db.callers.id>0).select():
print caller.userphone,caller.status
caller =
db(db.callers.userphone=='18775551212').select().first()
if caller:
caller.update_record(status=True)
#DO NOT FORGET TO COMMIT!!
db.commit()
print caller.userphone,caller.status
else:
print 'caller not found in DB'
##END FILE
I hope this helps someone else, and I welcome smarter better ways to
handle this situation from everyone here! This is my 1st week with
web2py after all!
With Love,
Tara
--
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en.