DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

SQL Export In Zope

03.08.2006
| 5306 views |
  • submit to reddit
        Takes the result of a Zope SQL query, and an ordered list of field names, and outputs a CSV file as the resultant web page.  Should be called like this:

return context.sql.ordered-sql2csv(context.sql.exportOrders().dictionaries(), ["fullname","email","phone","address","town","postcode","children","adults","comments"])


## Script (Python) "ordered-sql2csv"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=sql, fo
##title=
##
container.REQUEST.RESPONSE.setHeader("Content-Type", "text/csv", 0)
sep = ""
for dk in fo:
  container.REQUEST.RESPONSE.write(sep + dk)
  sep = ","
sep = "\n"

for rec in sql:
  for i in range(0,len(fo)):
    data=rec[fo[i]]
    dl = str(data).split(',')
    data1 = " ".join(dl)
    container.REQUEST.RESPONSE.write(sep + str(data1))
    sep = ","
  sep="\n"