@@ -28,10 +28,11 @@ TODO: possibly make this more general and configurable...
2828'''
2929
3030from __future__ import print_function
31- import sys , MimeWriter , smtplib
31+ import sys
32+ from email .mime .multipart import MIMEMultipart
33+ from email .utils import make_msgid
3234from roundup import instance , date
33- from roundup .mailer import SMTPConnection
34- from roundup .anypy .strings import StringIO
35+ from roundup .mailer import Mailer
3536
3637# open the instance
3738if len (sys .argv ) != 2 :
@@ -42,9 +43,28 @@ db = instance.open('admin')
4243
4344resolved_id = db .status .lookup ('resolved' )
4445
46+ class Reverse :
47+ """Class reversing sort order."""
48+
49+ def __init__ (self , val ):
50+ self .val = val
51+
52+ def __lt__ (self , other ):
53+ return other .val < self .val
54+ def __le__ (self , other ):
55+ return other .val <= self .val
56+ def __eq__ (self , other ):
57+ return other .val == self .val
58+ def __ne__ (self , other ):
59+ return other .val != self .val
60+ def __gt__ (self , other ):
61+ return other .val > self .val
62+ def __ge__ (self , other ):
63+ return other .val >= self .val
64+
4565def listKey (x ):
4666 "key for tuples such that order is positive on [0] and negative on [1]"
47- return (x [0 ], - x [1 ])
67+ return (x [0 ], Reverse ( x [1 ]) )
4868 return 0
4969
5070# loop through all the users
@@ -72,88 +92,86 @@ for user_id in db.user.list():
7292 continue
7393
7494 # generate the email message
75- message = 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 )
85-
86- # start the multipart
87- part = writer .startmultipartbody ('alternative' )
88- part = writer .nextpart ()
89- body = part .startbody ('text/plain' )
90-
95+ mailer = Mailer (db .config )
96+ message = MIMEMultipart ('alternative' )
97+ mailer .set_message_attributes (
98+ message ,
99+ [address ],
100+ 'Your active %s issues' % db .config .TRACKER_NAME )
101+ message ['Reply-To' ] = '%s <%s>' % (db .config .TRACKER_NAME ,
102+ db .config .ADMIN_EMAIL )
103+ message ['Message-Id' ] = make_msgid ()
104+
91105 # do the plain text bit
92- print ('Created ID Activity Title' , file = body )
93- print ('=' * 75 , file = body )
106+ text_lines = []
107+ text_lines .append ('Created ID Activity Title' )
108+ text_lines .append ('=' * 75 )
94109 # '2 months 213 immediate cc_daemon barfage
95110 old_priority = None
96111 for priority_order , activity_date , creation_date , issue_id in l :
97112 priority = db .issue .get (issue_id , 'priority' )
98113 if (priority != old_priority ):
99114 old_priority = priority
100- print (' ' , db .priority .get (priority ,'name' ), file = body )
115+ text_lines . append (' ' + db .priority .get (priority ,'name' ))
101116 # pretty creation
102117 creation = (creation_date - date .Date ('.' )).pretty ()
103118 activity = (activity_date - date .Date ('.' )).pretty ()
104119 title = db .issue .get (issue_id , 'title' )
105120 if len (title ) > 42 :
106121 title = title [:38 ] + ' ...'
107- print ('%-11s %-4s %-9s %-42s' % (creation , issue_id ,
108- activity , title ), file = body )
122+ text_lines . append ('%-11s %-4s %-9s %-42s' % (creation , issue_id ,
123+ activity , title ))
109124
110125 # some help to finish off
111- print ('''
126+ text_lines . append ('''
112127To view or respond to any of the issues listed above, visit the URL
113128
114129 %s
115130
116131and click on "My Issues". Do NOT respond to this message.
117- ''' % db .config .TRACKER_WEB , file = body )
132+ ''' % db .config .TRACKER_WEB )
133+ text = '\n ' .join (text_lines ) + '\n '
134+ part = mailer .get_text_message ()
135+ part .set_payload (text , part .get_charset ())
136+ message .attach (part )
118137
119138
120139 # now the HTML one
121- part = writer .nextpart ()
122- body = part .startbody ('text/html' )
140+ html_lines = []
123141 colours = {
124142 'immediate' : ' bgcolor="#ffcdcd"' ,
125143 'day' : ' bgcolor="#ffdecd"' ,
126144 'week' : ' bgcolor="#ffeecd"' ,
127145 'month' : ' bgcolor="#ffffcd"' ,
128146 'whenever' : ' bgcolor="#ffffff"' ,
129147 }
130- print ('''<table border>
148+ html_lines . append ('''<table border>
131149<tr><th>Created</th> <th>ID</th> <th>Activity</th> <th>Title</th></tr>
132- ''' , file = body )
150+ ''' )
133151 old_priority = None
134152 for priority_order , activity_date , creation_date , issue_id in l :
135153 priority = db .issue .get (issue_id ,'priority' )
136154 if (priority != old_priority ):
137155 old_priority = priority
138- print ('<tr><td>-></td><td>-></td><td>-></td><td><b>%s</b></td></tr>' % db .priority .get (priority ,'name' ), file = body )
156+ html_lines . append ('<tr><td>-></td><td>-></td><td>-></td><td><b>%s</b></td></tr>' % db .priority .get (priority ,'name' ))
139157 creation = (creation_date - date .Date ('.' )).pretty ()
140158 title = db .issue .get (issue_id , 'title' )
141159 issue_id = '<a href="%sissue%s">%s</a>' % (db .config .TRACKER_WEB ,
142160 issue_id , issue_id )
143161 activity = (activity_date - date .Date ('.' )).pretty ()
144- print ('''<tr><td>%s</td><td>%s</td><td>%s</td>
145- <td>%s</td></tr>''' % (creation , issue_id , activity , title ), file = body )
146- print ('</table>' , file = body )
162+ html_lines . append ('''<tr><td>%s</td><td>%s</td><td>%s</td>
163+ <td>%s</td></tr>''' % (creation , issue_id , activity , title ))
164+ html_lines . append ('</table>' )
147165
148- print ('''<p>To view or respond to any of the issues listed
166+ html_lines . append ('''<p>To view or respond to any of the issues listed
149167 above, simply click on the issue ID. Do <b>not</b> respond to
150- this message.</p>''' , file = body )
151-
152- # finish of the multipart
153- writer .lastpart ()
168+ this message.</p>''' )
169+ html = '\n ' .join (html_lines ) + '\n '
170+ part = mailer .get_text_message ('utf-8' , 'html' )
171+ part .set_payload (html , part .get_charset ())
172+ message .attach (part )
154173
155174 # all done, send!
156- smtp = SMTPConnection (db .config )
157- smtp .sendmail (db .config .ADMIN_EMAIL , address , message .getvalue ())
175+ mailer .smtp_send ([address ], message .as_string ())
158176
159177# vim: set filetype=python ts=4 sw=4 et si
0 commit comments