Created
August 7, 2012 07:18
-
-
Save jerrykan/3282728 to your computer and use it in GitHub Desktop.
Script to send out emails about open issues in roundup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# | |
# This script should be run from a cronjob. | |
# | |
import roundup.instance | |
from roundup import roundupdb | |
from roundup.mailer import Mailer | |
class ReminderEmails(object): | |
def __init__(self, instance): | |
""" Set up the class variables """ | |
self.instance = instance | |
def _email_reminder(self, nodeid): | |
""" Send out email to remind the assignedto user that the issue is | |
open | |
""" | |
# Get assignedto email | |
assignedto_id = self.db.issue.get(nodeid, 'assignedto') | |
if not assignedto_id: | |
return | |
try: | |
email = self.db.user.get(assignedto_id, 'address') | |
except KeyError: | |
return | |
to = [email] | |
# Get the issue title | |
title = self.db.issue.get(nodeid, 'title') | |
subject = '[issue%(nodeid)s] Reminder: %(title)s' % { | |
'nodeid': nodeid, | |
'title': title, | |
} | |
# Get issue message | |
msg_ids = self.db.issue.get(nodeid, 'messages') | |
if len(msg_ids) > 0: | |
try: | |
msg = self.db.msg.get(msg_ids[0], 'content') | |
except KeyError: | |
raise roundupdb.DetectorError, "No email sent: Could not find the message" | |
else: | |
raise roundupdb.DetectorError, "No email sent: No message associated with the issue" | |
# Get issue URL | |
url = self.db.config['TRACKER_WEB'] + 'issue' + nodeid | |
content = """\ | |
This is a reminder that the issue%(nodeid)s is still open. Please attend to | |
this issue: | |
%(url)s | |
======== | |
%(msg)s | |
""" % locals() | |
# Get the "from" address | |
author = (self.db.config['TRACKER_NAME'], self.db.config['TRACKER_EMAIL']) | |
# Send the email | |
try: | |
self.mailer.standard_message(to, subject, content, author=author) | |
except roundupdb.MessageSendError, msg: | |
content = "%s\n\n================%s================" % (msg, content) | |
mailer.standard_message([self.db.config['ADMIN_EMAIL']], 'Error Sending Email Notification', content) | |
def run(self): | |
""" | |
Search for all issues that are still open and send out a reminder | |
email to the assignedto users | |
""" | |
# Open DB | |
self.db = self.instance.open('admin') | |
# Setup the mailer | |
self.mailer = Mailer(self.db.config) | |
# Search for all issues that are open (based on status IDs) | |
nodeids = self.db.issue.filter(None, {'status': ['-1', '1', '2', '3', '4', '5', '6', '7']}) | |
for nodeid in nodeids: | |
self._email_reminder(nodeid) | |
# Close DB | |
self.db.close() | |
if __name__ == "__main__": | |
instance = roundup.instance.open('/path/to/roundup/instance') | |
# Search for reminders and send emails | |
reminders = ReminderEmails(instance) | |
reminders.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment