Thursday, September 24, 2009

PloneGazette: exporting all subscribers as csv

A custom script. It's worth noting that in PloneGazette 3.0, calling getFolderContents on a NewsletterBTree will give its parent's contents. That's acquisition for you. So we use the getSubcribers method.

There's also a nice trick to copy the results to the Plone log.


## Script (Python) "exportEmailSubscriptions"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
response = context.REQUEST.RESPONSE
response.setHeader('Content-type','application/csv;; charset=utf-8')
response.addHeader("Content-Disposition","inline; filename=export.csv")
response.setHeader('Cache-Control','no-cache')
response.setHeader('Pragma','No-Cache')

def copy_to_log(message):
message = str(message)
script_name = "EXPORT_SUBSCRIBERS"
context.plone_log("%s: %s" % (script_name, message))
return message

from Products.CMFCore.utils import getToolByName
catalog = getToolByName(context, 'portal_catalog')
results = catalog(portal_type="NewsletterBTree")
for result in results:
folder = result.getObject()
subscr_brains = folder.getSubscribers()
for subscr_brain in subscr_brains:
subscr = subscr_brain.getObject()
print copy_to_log("%s,%s,%s" % ( subscr.Title(), subscr.active, subscr.format, ))

return printed

3 comments:

Anonymous said...

Hello,
i read your article on exporting all subscribers to CSV. but i m using older version of plonegazette. how can make use of the same script to work for plone gazette2.5.0 running on plone 2.5.2

Kees Hink said...
This comment has been removed by the author.
Kees Hink said...

http://catb.org/~esr/faqs/smart-questions.html