[ZPT] zpt + zsql

Donald Braman donald.braman@aya.yale.edu
Fri, 7 Dec 2001 12:18:45 -0500


That does help...a lot. Thanks.

-----Original Message-----
From: Wade Leftwich [mailto:wade@lightlink.com]
Sent: Friday, December 07, 2001 11:13 AM
To: donald.braman@aya.yale.edu
Cc: zpt@zope.org
Subject: Re: [ZPT] zpt + zsql


Hello Donald,

I do quite a bit of work with ZSQL, python scripts and templates -- in fact
you really don't need much else to make a very nice web application (well,
maybe a database).

I don't have any examples handy, but for what they are worth here are a few
tips.

The script, not the template, is the hub of your page (my opinion). You
should have a script that does all the calculating that's required before
you
even start work on a template. Your page's Url is the script, which will
call
the template like a function. This will (a) help you resist the temptation
to
write code in the template, and (b) allow you to use the same script for
output to html, xml, wireless, etc.

An annoying fact about the recordsets returned by ZSQL methods: each record
acts somewhat but not enough like a dictionary. In particular, you cannot
add
a (key, value) pair to a record. So if you need to associate a computed
value
with each record, you make a list of dicts out of it, like this:

def recs2lod(recset, names=None):
    names = names or recset.names()
    L = [ ]
    for rec in recset:
        D = {}
        for name in names:
            D[name] = rec[name]
        L.append(D)
    return (L)

Finally -- optional ZSQL selection args are very useful. To take advantage
of
them, call your ZSQL with a dictionary as argument. To trigger the 'option'
default for a given key, leave that key out of the dict.

Here's a quick example script:

#  getpeople() is a ZSQL method with optional selection args City and State
# select fname, lname from  addresses
# <dtml-sqlgroup where>
# <dtml-sqltest city type=str op=eq optional>
# <dtml-and>
# <dtml-sqltest state type=str op=eq optional>
# </dtml-sqlgroup>

sqlargs = {'city':'Miami'}  # with no state arg, we'll get results from
Miami
FL and Miami Ohio

recset = container.getpeople(sqlargs)   # (fname, lname)
people = recs2lod(recset)
for person in people:
    person['fullname'] = ' '.join((person.get('fname',''),
person.get('lname','')))

return container.myTemplate(people=people) # myTemplate receives data in
options/people

################

hth

Wade Leftwich
(Trumbull '76)
Ithaca, NY



On Thursday 06 December 2001 12:01, zpt-request@zope.org wrote:
> Message: 2
> From: "Donald Braman" <donald.braman@aya.yale.edu>
> To: <zpt@zope.org>
> Date: Thu, 6 Dec 2001 11:48:06 -0500
> Subject: [ZPT] zpt + zsql
>
> I'd like to turn some old DTML docs into ZPTs so my design partner can
make
> them pretty. The pages use ZSQL Methods to display and update data. My
> first goal is simply to access data from a database using a ZSQL method
and
> display it in a template; my second to manipulate the data.
>
> Here are my questions:
>
> 1. Is there already a tutorial or documentation on ZPT + ZSQL (+Python)?
>
> 2. If not, where is a good place for me to turn for related documentation.
> I've seen some past posts here, but they don't really make sense to me
> (yet), as I've never used python with zsql methods (or in Zope in any
> fashion for that matter).
>
> Also, while I'm at it, I thought I'd try to provide documentation to other
> people trying to do this, along with of a simple generic ZPT, Python
> Script, and ZSQL Method that all work together. Or has this been done
> already?
>
>
> Donald Braman
> donald.braman@aya.yale.edu