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.4 2002-09-10 03:01:19 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 cStringIO, MimeWriter, smtplib
36 from roundup import instance, date
38 # open the instance
39 if len(sys.argv) != 2:
40 print 'You need to specify an instance home dir'
41 instance_home = sys.argv[1]
42 instance = instance.open(instance_home)
43 db = instance.open('admin')
45 resolved_id = db.status.lookup('resolved')
47 # loop through all the users
48 for user_id in db.user.list():
49 # make sure we care aboue this user
50 name = db.user.get(user_id, 'realname')
51 if name is None:
52 name = db.user.get(user_id, 'username')
53 address = db.user.get(user_id, 'address')
54 if address is None:
55 continue
57 # extract this user's issues
58 l = []
59 for issue_id in db.issue.find(assignedto=user_id):
60 if db.issue.get(issue_id, 'status') == resolved_id: continue
61 timeliness_id = db.issue.get(issue_id, 'timeliness')
62 if timeliness_id:
63 timeliness = db.timeliness.get(timeliness_id, 'name')
64 else:
65 timeliness = '~~~'
66 l.append((timeliness, db.issue.get(issue_id, 'creation'), issue_id))
68 # sort the issues by timeliness and creation date
69 l.sort()
70 if not l:
71 continue
73 # generate the email message
74 message = cStringIO.StringIO()
75 writer = MimeWriter.MimeWriter(message)
76 writer.addheader('Subject', 'Your active %s issues'%db.config.TRACKER_NAME)
77 writer.addheader('To', address)
78 writer.addheader('From', '%s <%s>'%(db.config.TRACKER_NAME,
79 db.config.ADMIN_EMAIL))
80 writer.addheader('Reply-To', '%s <%s>'%(db.config.TRACKER_NAME,
81 db.config.ADMIN_EMAIL))
82 writer.addheader('MIME-Version', '1.0')
83 writer.addheader('X-Roundup-Name', db.config.TRACKER_NAME)
85 # start the multipart
86 part = writer.startmultipartbody('alternative')
87 part = writer.nextpart()
88 body = part.startbody('text/plain')
90 # do the plain text bit
91 print >>body, 'Created ID Urgency Title'
92 print >>body, '='*75
93 # '2 months 213 immediate cc_daemon barfage
94 for timeliness, creation_date, issue_id in l:
95 # pretty creation
96 creation = (date.Date('.') - creation_date).pretty()
97 if creation is None:
98 creation = creation_date.pretty()
100 if not timeliness: timeliness = ''
101 title = db.issue.get(issue_id, 'title')
102 if len(title) > 52: title = title[:48] + ' ...'
103 print >>body, '%-11s %-4s %-9s %-52s'%(creation, issue_id,
104 timeliness, title)
106 # some help to finish off
107 print >>body, '''
108 To view or respond to any of the issues listed above, visit the URL
110 %s
112 and click on "My Issues". Do NOT respond to this message.
113 '''%db.config.TRACKER_WEB
116 # now the HTML one
117 part = writer.nextpart()
118 body = part.startbody('text/html')
119 colours = {
120 'immediate': ' bgcolor="#ffcdcd"',
121 'day': ' bgcolor="#ffdecd"',
122 'week': ' bgcolor="#ffeecd"',
123 'month': ' bgcolor="#ffffcd"',
124 'whenever': ' bgcolor="#ffffff"',
125 }
126 print >>body, '''<table border>
127 <tr><th>Created</th> <th>ID</th> <th>Urgency</th> <th>Title</th></tr>
128 '''
129 for timeliness, creation_date, issue_id in l:
130 creation = (date.Date('.') - creation_date).pretty()
131 if creation is None:
132 creation = creation_date.pretty()
133 if not timeliness_id: timeliness_id = ' '
134 title = db.issue.get(issue_id, 'title')
135 issue_id = '<a href="%sissue%s">%s</a>'%(db.config.ISSUE_TRACKER_WEB,
136 issue_id, issue_id)
137 colour = colours.get(timeliness, '')
138 print >>body, '''<tr%s><td>%s</td><td>%s</td><td>%s</td>
139 <td>%s</td></tr>'''%(colour, creation, issue_id, timeliness, title)
140 print >>body, '</table>'
142 print >>body, '''<p>To view or respond to any of the issues listed
143 above, simply click on the issue ID. Do <b>not</b> respond to
144 this message.</p>'''
146 # finish of the multipart
147 writer.lastpart()
149 # all done, send!
150 smtp = smtplib.SMTP(db.config.MAILHOST)
151 smtp.sendmail(db.config.ADMIN_EMAIL, address, message.getvalue())
153 # vim: set filetype=python ts=4 sw=4 et si