[Twisted-Python] Twisted.web and adbapi example?

I've been pouring through the docs and examples, but can't find an example of a .rpy that generates a page containing the results of a database query. How would such a thing be properly architected? Clearly the adbapi would be involved, but then how do I get the query results into the string returned by my resource? Or would I use a deferred instead?
Would someone please share a little code snippet (or point me at one)illustrating how that would best be accomplished?
Thanks.

An example that uses threads (and not adpapi) is in January's mail achive, at: http://twistedmatrix.com/pipermail/twisted-python/2003-January/ 002694.html
Here's some old code that uses adpapi (save as a .rpy):
###
from twisted.web import resource from twisted.web.server import NOT_DONE_YET
dbname = 'test' dbuser = 'dbuser' dbpass = 'dbpass'
from twisted.enterprise import row class FooRow(row.RowObject): rowColumns = [ ('foo_id', 'int'), ('name', 'varchar'), ('weight', 'int'), ] rowKeyColumns = [('foo_id', 'int4')] rowTableName = 'foo' #rowFactoryMethod = ['testFactoryMethod']
def databaseResult_adbapi(result, httpRequest): if result: for foo in result: httpRequest.write('foo: '+str(foo.__dict__)+'<br/>') else: httpRequest.write('stop:') httpRequest.finish()
def errBack_adbapi(err, httpRequest): httpRequest.write('error: '+str(err) ) httpRequest.finish()
class MyResource_adbapi(resource.Resource): def render(self, request): from twisted.enterprise import adbapi, reflector from twisted.enterprise.sqlreflector import SQLReflector dbpool = adbapi.ConnectionPool('pyPgSQL.PgSQL',database=dbname,user=dbuser,passwo rd=dbpass) r = SQLReflector(dbpool,[FooRow]) d = r.loadObjectsFrom('foo',whereClause=[('foo_id', reflector.LESSTHAN, 5)]) d.addCallback(databaseResult_adbapi, request) d.addErrback(errBack_adbapi, request) return NOT_DONE_YET
resource = MyResource_adbapi()
###
I've been pouring through the docs and examples, but can't find an example of a .rpy that generates a page containing the results of a database query. How would such a thing be properly architected? Clearly the adbapi would be involved, but then how do I get the query results into the string returned by my resource? Or would I use a deferred instead?
Would someone please share a little code snippet (or point me at one)illustrating how that would best be accomplished?
Thanks.

Hi, Mario Ruggier wrote:
Here's some old code that uses adpapi (save as a .rpy):
... and edit to remove the line wraps your editor added.
Please turn wrapping off before pasting. Thank you.

hi!
I got one using t.w.w.p.Page & t.e.adbapi .. i'll post later/tomorrow .. (if i forget just drop me a private mail ;)
regards <quote who="Matthias Urlichs">
Hi, Mario Ruggier wrote:
Here's some old code that uses adpapi (save as a .rpy):
... and edit to remove the line wraps your editor added.
Please turn wrapping off before pasting. Thank you.
-- Matthias Urlichs | {M:U} IT Design @ m-u-it.de | smurf@smurf.noris.de Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de -- "I have to convince you, or at least snow you ..." -- Prof. Romas Aleliunas, CS 435
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

sorry really forgot about that .. here it comes ;)
regards P.S.: how the db looks like is left as an exercise to the user grin
<quote who="Morrow, Paul">
I've been pouring through the docs and examples, but can't find an example of a .rpy that generates a page containing the results of a database query. How would such a thing be properly architected? Clearly the adbapi would be involved, but then how do I get the query results into the string returned by my resource? Or would I use a deferred instead?
Would someone please share a little code snippet (or point me at one)illustrating how that would best be accomplished?
Thanks.
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (4)
-
Mario Ruggier
-
Matthias Urlichs
-
Morrow, Paul
-
Thomas Raschbacher