Code

fixed roundup-reminder script to use default schema (thanks Klamer Schutte)
[roundup.git] / scripts / roundup-reminder
1 #! /usr/bin/env python2.2
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.7 2004-02-11 00:00:01 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...
30 '''
32 import sys, cStringIO, MimeWriter, smtplib
33 from roundup import instance, date
34 from roundup.mailgw import openSMTPConnection
36 # open the instance
37 if len(sys.argv) != 2:
38     print 'You need to specify an instance home dir'
39 instance_home = sys.argv[1]
40 instance = instance.open(instance_home)
41 db = instance.open('admin')
43 resolved_id = db.status.lookup('resolved')
45 def listCompare(x, y):
46     "compare two tuples such that order is positive on [0] and negative on [1]"
47     if x[0] < y[0]:
48         return -1
49     if x[0] > y[0]:
50         return 1
51     if x[1] > y[1]:
52         return -1
53     if x[1] < y[1]:
54         return 1
55     return 0
57 # loop through all the users
58 for user_id in db.user.list():
59     # make sure we care aboue this user
60     name = db.user.get(user_id, 'realname')
61     if name is None:
62         name = db.user.get(user_id, 'username')
63     address = db.user.get(user_id, 'address')
64     if address is None:
65         continue
67     # extract this user's issues
68     l = []
69     for issue_id in db.issue.find(assignedto=user_id):
70         if db.issue.get(issue_id, 'status') == resolved_id:
71             continue
72         order = db.priority.get(db.issue.get(issue_id, 'priority'), 'order')
73         l.append((order, db.issue.get(issue_id, 'activity'),
74             db.issue.get(issue_id, 'creation'), issue_id))
76     # sort the issues by timeliness and creation date
77     l.sort(listCompare)
78     if not l:
79         continue
81     # generate the email message
82     message = cStringIO.StringIO()
83     writer = MimeWriter.MimeWriter(message)
84     writer.addheader('Subject', 'Your active %s issues'%db.config.TRACKER_NAME)
85     writer.addheader('To', address)
86     writer.addheader('From', '%s <%s>'%(db.config.TRACKER_NAME,
87         db.config.ADMIN_EMAIL))
88     writer.addheader('Reply-To', '%s <%s>'%(db.config.TRACKER_NAME,
89         db.config.ADMIN_EMAIL))
90     writer.addheader('MIME-Version', '1.0')
91     writer.addheader('X-Roundup-Name', db.config.TRACKER_NAME)
93     # start the multipart
94     part = writer.startmultipartbody('alternative')
95     part = writer.nextpart()
96     body = part.startbody('text/plain')
97     
98     # do the plain text bit
99     print >>body, 'Created     ID   Urgency   Title'
100     print >>body, '='*75
101     #             '2 months    213  immediate cc_daemon barfage
102     old_priority = None
103     for priority_order, activity_date, creation_date, issue_id in l:
104         priority = db.issue.get(issue_id, 'priority')
105         if (priority != old_priority):
106             old_priority = priority
107             print >>body, '    ', db.priority.get(priority,'name')
108         # pretty creation
109         creation = (date.Date('.') - creation_date).pretty()
110         if creation is None:
111             creation = creation_date.pretty()
112         activity = (date.Date('.') - activity_date).pretty()
113         title = db.issue.get(issue_id, 'title')
114         if len(title) > 42:
115             title = title[:38] + ' ...'
116         print >>body, '%-11s %-4s %-9s %-42s'%(creation, issue_id,
117             activity, title)
119     # some help to finish off
120     print >>body, '''
121 To view or respond to any of the issues listed above, visit the URL
123    %s
125 and click on "My Issues". Do NOT respond to this message.
126 '''%db.config.TRACKER_WEB
129     # now the HTML one
130     part = writer.nextpart()
131     body = part.startbody('text/html')
132     colours = {
133         'immediate': ' bgcolor="#ffcdcd"',
134         'day': ' bgcolor="#ffdecd"',
135         'week': ' bgcolor="#ffeecd"',
136         'month': ' bgcolor="#ffffcd"',
137         'whenever': ' bgcolor="#ffffff"',
138     }
139     print >>body, '''<table border>
140 <tr><th>Created</th> <th>ID</th> <th>Activity</th> <th>Title</th></tr>
141 '''
142     old_priority = None
143     for priority_order, activity_date, creation_date, issue_id in l:
144         priority = db.issue.get(issue_id,'priority')
145         if (priority != old_priority):
146            old_priority = priority
147            print >>body, '<tr><td>-></td><td>-></td><td>-></td><td><b>%s</b></td></tr>'%db.priority.get(priority,'name')
148         creation = (date.Date('.') - creation_date).pretty()
149         if creation is None:
150             creation = creation_date.pretty()
151         title = db.issue.get(issue_id, 'title')
152         issue_id = '<a href="%sissue%s">%s</a>'%(db.config.TRACKER_WEB,
153             issue_id, issue_id)
154         activity = (date.Date('.') - activity_date).pretty()
155         print >>body, '''<tr><td>%s</td><td>%s</td><td>%s</td>
156     <td>%s</td></tr>'''%(creation, issue_id, activity, title)
157     print >>body, '</table>'
159     print >>body, '''<p>To view or respond to any of the issues listed
160         above, simply click on the issue ID. Do <b>not</b> respond to
161         this message.</p>'''
163     # finish of the multipart
164     writer.lastpart()
166     # all done, send!
167     smtp = openSMTPConnection(db.config)
168     smtp.sendmail(db.config.ADMIN_EMAIL, address, message.getvalue())
170 # vim: set filetype=python ts=4 sw=4 et si