[Zope] Database query -> HTML table

Rik Hoekstra rik.hoekstra@inghist.nl
Fri, 14 Apr 2000 09:32:32 +0200


Mike Orr wrote:
> 
> I'm trying to find a generic way to turn a database query into an
> HTML table the way the ZSQL Method's "test" routine does.
> 
> I can do this with a literal list calling a Python method:
> Python method: table
> Parameter list:
>         headers, data,
>         table='<TABLE BORDER>', ntable='</TABLE>', tr='<TR>', ntr='</TR>',
>         td='<TD ALIGN="left" VALIGN="top">', ntd='</TD>',
>         th='<TH ALIGN="left" VALIGN="top">', nth='</TH>'
> Code:
>         """
>         in : headers, list of strings, the column titles.
>            : data, list of tuples, each row of data.
>            : table, ntable, tr, ntr, td, ntd, th, nth,
>                strings, how to render these HTML tags.
>         """
>         headers_len = len(headers)
>         data_len = len(data)
>         if data_len == 0:
>                 return "<!-- No data for table -->"
>         if data_len > headers_len:
>                 # Why doesn't Zope have a list() function?
>                 headers = headers[:] # Make a copy.
>                 ext = [ "&nbsp;" ] * (data_len - headers_len)
>                 headers.extend(ext)
>         ret = [table, tr]
>         for h in headers:
>                 ret.extend([th, h, nth])
>         ret.append(ntr)
>         for row in data:
>                 ret.append(tr)
>                 for d in row:
>                         ret.extend([td, d, ntd])
>                 ret.append(ntr)
>         ret.append(ntable)
>         return _.string.join(ret, '\n')
> 
> I can then call it with a literal list:
> <dtml-var expr="table(['header1', 'header2'], [('value 1a', 'value 1b'),
>         ('value2a', 'value2b')])">
> 
> What I want is a Python method that constructs the header and value
> lists and calls table(header, value).  So that my documents can have a nice,
> clean:
> <dtml-var expr="sql_table( my_zsql_method(arg1, arg2, ...) )">
> 
> Where can I find some documentation on using a ZQSL Method object in
> Python?
> 
> Or is there a way to access the table-creation code Zope uses in the
> "test" routine?


This is perhaps a stupid question, but have you tried creating a ZSearch
Interface (avalable in the management screen) from a ZSQL method? For me
it mostly works quite well use the results of a ZSearch Interface as a
starting point to make something to my liking. 
If you really want to do this from python, it may be a good idea to take
the code for the search interface as a starting point. It is in
<ZOPE_HOME>/lib/python/Shared/DC/ZRDB look in Search.py and
customDefaultReport.dtml

hth

Rik