## Script (Python) "pin_statistics"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
# $Id: pin_statistics.py 5657 2011-01-24 16:24:16Z henrik $
request = context.REQUEST
setheader = request.RESPONSE.setHeader
fields = ("id",
          "date",
          "used",
          "total",
          "cost",
          )

lines = []
lines.append(','.join(fields))
format = '"%(' + ')s","%('.join(fields) + ')s"'               

batches = []
for id, object in context.objectItems():
    batch = {}
    batch['id'] = id
    batch['date'] = object.creation_date.strftime("%d/%m/%y")
    batch_doc = object.getContent()
    batch['used'] = batch_doc.getNumberOfUsedPins()
    batch['total'] = batch_doc.no_of_pins
    batch['cost'] = batch_doc.cost
    lines.append(format % batch)

setheader('Content-type','text/semicolon-seperated-values')
setheader('Content-Disposition:', 'attachment; filename="PinStatistics.csv"')
setheader('Expires',  'Mon, 26 Jul 1997 05:00:00GMT') # Date in the past
setheader('Cache-Control', 'no-store, no-cache,must-revalidate') # HTTP/1.1
setheader('Cache-Control', 'post-check=0,pre-check=0')
return '\n'.join(lines)

