2012/03/01

Script to List Items

不需要建立模組,在 ZMI 新增 Python Script 就能在 Plone 環境執行簡單的查詢工作,例如顯示 News Item 的基本欄位資料。

from Products.CMFCore.utils import getToolByName

request = container.REQUEST
catalog = getToolByName(context, 'portal_catalog')
path = '/mysite/news'

for brain in catalog(portal_type='News Item', path=path):
    try:
        obj = brain.getObject()
        print "%s, %s, %s, %s" % \
              (brain.getPath(), obj.Title(), obj.Contributors()[0], obj.EffectiveDate())
    except:
        pass
return printed

上述執行結果,是方便另存成 CSV 檔案格式。

from Products.CMFCore.utils import getToolByName

catalog = getToolByName(context, 'portal_catalog')
areas = ['TaipeiCity', 'NewTaipeiCity']
path = '/mysite/myfolder'

for i in areas:
    brains = catalog(portal_type='Image', path=path+'/'+i)
    print "%s: %d" % (i, len(brains))
print "Total: %d" % len(catalog(portal_type='Image', path=path))

return printed

No comments: