[ZPT] Re: ZPT and ZSQL methods

Wade Leftwich wade@okaynetwork.com
Thu, 11 Oct 2001 13:24:59 -0400


Chris --

I do this a lot, because I'm working at moving a big site off of ASP and onto Zope. For the foreseeable future, most of the data will continue to reside in MSSQL.
The combination of ZPT + ZSQL + Python script works great for this.

The basic recipe that works for me is:

Make a folder with the name you want in your Url.
In that folder, place a PT named index_html, a script (I cleverly call mine theScript) and a ZSQL method (theQuery ditto).
At the top of the PT, say tal:define="global mydata container/theScript"
In most cases I don't bother passing args to theScript, it can get what it needs from REQUEST.
theScript will send back all the data that the PT will use in one dictionary.

theScript calls theQuery. In a simple case, it will just put theQuery's return value into the return dictionary.

However, if I need to do any kind of manipulation of the query results, theScript can do something like this:
L = []
for row in container.theQuery(argdict):
    rowD = {}
    rowD['arg1'] = row['arg1'] + 42
    L.append(rowD)
results['query'] = L

As far as the PT is concerned, a list of dicts looks just like a ZSQL recordset.

Helpful ZSQL hint: In ZSQL, you have the very useful option of having WHERE clauses disappear if you don't pass them an argument, using DTML-SQLTEST OPTIONAL
To make this work from a script, pass the SQL method a dictionary containing your query args, leaving out the ones you want to disappear. (the disapperaing WHERE trick doesn't work if you pass None
as a value, the key must be missing.)

If it will help you out I can send you a sample script.

Wade Leftwich
Ithaca, NY


--------------------------------------------------------------
Date: Wed, 10 Oct 2001 17:59:08 +0100
From: Chris Keyes <chrisk@nipltd.com>
Organization: New Information Paradigms
To: zpt@zope.org
Subject: [ZPT] ZPT and ZSQL methods
Hi All
I'm a real newbie at this stuff and am playing with getting a ZPT page
to talk to a ZSQL method... Anyone seen any examples and or
documentation about that??
I'm pretty new to Zope so the obvious stuff is not all ovbious. If I
should be asking on zope@zope.org please tell me :-)
Cheers
ChrisK