The above does not work as expected if at all. Made changes and the below script works. It requires an "expire" workflow transation to be set up but if you don't want to do this then just change that part of the script (to "hide" or whatever you want).
from DateTime import DateTime
for r in context.portal_catalog():
date1 = r.expires #the expiration date
date2 = context.ZopeTime() #the current date
t = int(date1.earliestTime()-date2.earliestTime()) #the integer difference between dates
print t
if t==14: #if there is 14 days between dates...
contentObject = r.getObject() #get object via catalog
objectHistory=contentObject.portal_workflow.getInfoFor(contentObject, 'review_history')
thisRevision=objectHistory[-1]
nowDate=thisRevision['time'].aCommon()
authorComment=thisRevision['comments']
mailList=[]
try:
mailhost=getattr(context, context.portal_url.superValues('Mail Host')[0].id)
except:
raise AttributeError, "Cannot find a Mail Host object"
mMsg = 'An item you created on the website is nearing expiry.\n\nPlease update the item.\n\nThis email was generated and sent by the COMESA website.\n\n'
mMsg += 'Title: ' + contentObject.TitleOrId() + '\n\n'
mMsg += 'Description: ' + contentObject.Description() + '\n\n'
mMsg += contentObject.absolute_url() + '\n'
#mMsg += 'Author: ' + contentObject.Creator()
mMsg += 'Author comment:' + authorComment + '\n\n'
mTo = context.portal_membership.getMemberById(contentObject.Creator()).email
mFrom = 'Webmaster@comesa.int'
mSubj = 'Website item nearing expiration date'
mailhost.send(mMsg, mTo, mFrom, mSubj) #send email to author
elif t==0: #else if expiration date=today's date
contentObject = r.getObject() #get object via catalog
try: #change review_state to expired
context.portal_workflow.doActionFor(contentObject, 'expire')
except:
print 'didnt change state' #this is here just in case
return printed
|