1 #! /usr/bin/env python
2 # Copyright (c) 2002 ekit.com Inc (http://www.ekit-inc.com/)
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 # SOFTWARE.
22 # $Id: roundup-reminder,v 1.6 2003-04-24 07:19:59 richard Exp $
24 '''
25 Simple script that emails all users of a tracker with the issues that
26 are currently assigned to them.
28 TODO: introduce some structure ;)
29 TODO: possibly make this more general and configurable...
31 Note: The instance that this script was designed for has a modified schema!
32 You will want to modify this script to customise it for your own schema!
33 '''
35 import sys, cStringIO, MimeWriter, smtplib
36 from roundup import instance, date
37 from roundup.mailgw import openSMTPConnection
39 # open the instance
40 if len(sys.argv) != 2:
41 print 'You need to specify an instance home dir'
42 instance_home = sys.argv[1]
43 instance = instance.open(instance_home)
44 db = instance.open('admin')
46 resolved_id = db.status.lookup('resolved')
48 # loop through all the users
49 for user_id in db.user.list():
50 # make sure we care aboue this user
51 name = db.user.get(user_id, 'realname')
52 if name is None:
53 name = db.user.get(user_id, 'username')
54 address = db.user.get(user_id, 'address')
55 if address is None:
56 continue
58 # extract this user's issues
59 l = []
60 for issue_id in db.issue.find(assignedto=user_id):
61 if db.issue.get(issue_id, 'status') == resolved_id: continue
62 timeliness_id = db.issue.get(issue_id, 'timeliness')
63 if timeliness_id:
64 timeliness = db.timeliness.get(timeliness_id, 'name')
65 else:
66 timeliness = '~~~'
67 l.append((timeliness, db.issue.get(issue_id, 'creation'), issue_id))
69 # sort the issues by timeliness and creation date
70 l.sort()
71 if not l:
72 continue
74 # generate the email message
75 message = cStringIO.StringIO()
76 writer = MimeWriter.MimeWriter(message)
77 writer.addheader('Subject', 'Your active %s issues'%db.config.TRACKER_NAME)
78 writer.addheader('To', address)
79 writer.addheader('From', '%s <%s>'%(db.config.TRACKER_NAME,
80 db.config.ADMIN_EMAIL))
81 writer.addheader('Reply-To', '%s <%s>'%(db.config.TRACKER_NAME,
82 db.config.ADMIN_EMAIL))
83 writer.addheader('MIME-Version', '1.0')
84 writer.addheader('X-Roundup-Name', db.config.TRACKER_NAME)
86 # start the multipart
87 part = writer.startmultipartbody('alternative')
88 part = writer.nextpart()
89 body = part.startbody('text/plain')
91 # do the plain text bit
92 print >>body, 'Created ID Urgency Title'
93 print >>body, '='*75
94 # '2 months 213 immediate cc_daemon barfage
95 for timeliness, creation_date, issue_id in l:
96 # pretty creation
97 creation = (date.Date('.') - creation_date).pretty()
98 if creation is None:
99 creation = creation_date.pretty()
101 if not timeliness: timeliness = ''
102 title = db.issue.get(issue_id, 'title')
103 if len(title) > 52: title = title[:48] + ' ...'
104 print >>body, '%-11s %-4s %-9s %-52s'%(creation, issue_id,
105 timeliness, title)
107 # some help to finish off
108 print >>body, '''
109 To view or respond to any of the issues listed above, visit the URL
111 %s
113 and click on "My Issues". Do NOT respond to this message.
114 '''%db.config.TRACKER_WEB
117 # now the HTML one
118 part = writer.nextpart()
119 body = part.startbody('text/html')
120 colours = {
121 'immediate': ' bgcolor="#ffcdcd"',
122 'day': ' bgcolor="#ffdecd"',
123 'week': ' bgcolor="#ffeecd"',
124 'month': ' bgcolor="#ffffcd"',
125 'whenever': ' bgcolor="#ffffff"',
126 }
127 print >>body, '''<table border>
128 <tr><th>Created</th> <th>ID</th> <th>Urgency</th> <th>Title</th></tr>
129 '''
130 for timeliness, creation_date, issue_id in l:
131 creation = (date.Date('.') - creation_date).pretty()
132 if creation is None:
133 creation = creation_date.pretty()
134 if not timeliness_id: timeliness_id = ' '
135 title = db.issue.get(issue_id, 'title')
136 issue_id = '<a href="%sissue%s">%s</a>'%(db.config.TRACKER_WEB,
137 issue_id, issue_id)
138 colour = colours.get(timeliness, '')
139 print >>body, '''<tr%s><td>%s</td><td>%s</td><td>%s</td>
140 <td>%s</td></tr>'''%(colour, creation, issue_id, timeliness, title)
141 print >>body, '</table>'
143 print >>body, '''<p>To view or respond to any of the issues listed
144 above, simply click on the issue ID. Do <b>not</b> respond to
145 this message.</p>'''
147 # finish of the multipart
148 writer.lastpart()
150 # all done, send!
151 smtp = openSMTPConnection(db.config)
152 smtp.sendmail(db.config.ADMIN_EMAIL, address, message.getvalue())
154 # vim: set filetype=python ts=4 sw=4 et si