comparison roundup/cgi/client.py @ 4543:d16d9bf655d8

- fix handling of traceback mails to the roundup admin - now this mail is a multipart/alternative with the HTML part *and* the traceback in text-format.
author Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
date Fri, 07 Oct 2011 19:08:54 +0000
parents c1c395058dee
children 35adb3950a39
comparison
equal deleted inserted replaced
4542:46239c21a1eb 4543:d16d9bf655d8
3 __docformat__ = 'restructuredtext' 3 __docformat__ = 'restructuredtext'
4 4
5 import base64, binascii, cgi, codecs, mimetypes, os 5 import base64, binascii, cgi, codecs, mimetypes, os
6 import quopri, random, re, rfc822, stat, sys, time 6 import quopri, random, re, rfc822, stat, sys, time
7 import socket, errno 7 import socket, errno
8 from traceback import format_exc
8 9
9 from roundup import roundupdb, date, hyperdb, password 10 from roundup import roundupdb, date, hyperdb, password
10 from roundup.cgi import templating, cgitb, TranslationService 11 from roundup.cgi import templating, cgitb, TranslationService
11 from roundup.cgi.actions import * 12 from roundup.cgi.actions import *
12 from roundup.exceptions import * 13 from roundup.exceptions import *
19 from roundup.anypy.cookie_ import CookieError, BaseCookie, SimpleCookie, \ 20 from roundup.anypy.cookie_ import CookieError, BaseCookie, SimpleCookie, \
20 get_cookie_date 21 get_cookie_date
21 from roundup.anypy.io_ import StringIO 22 from roundup.anypy.io_ import StringIO
22 from roundup.anypy import http_ 23 from roundup.anypy import http_
23 from roundup.anypy import urllib_ 24 from roundup.anypy import urllib_
25
26 from email.MIMEBase import MIMEBase
27 from email.MIMEText import MIMEText
28 from email.MIMEMultipart import MIMEMultipart
24 29
25 def initialiseSecurity(security): 30 def initialiseSecurity(security):
26 '''Create some Permissions and Roles on the security object 31 '''Create some Permissions and Roles on the security object
27 32
28 This function is directly invoked by security.Security.__init__() 33 This function is directly invoked by security.Security.__init__()
545 + ("<h1>Environment Variables</h1><table>%s</table>" 550 + ("<h1>Environment Variables</h1><table>%s</table>"
546 % cgitb.niceDict("", self.env))) 551 % cgitb.niceDict("", self.env)))
547 if not self.instance.config.WEB_DEBUG: 552 if not self.instance.config.WEB_DEBUG:
548 exc_info = sys.exc_info() 553 exc_info = sys.exc_info()
549 subject = "Error: %s" % exc_info[1] 554 subject = "Error: %s" % exc_info[1]
550 self.send_html_to_admin(subject, html) 555 self.send_error_to_admin(subject, html, format_exc())
551 self.write_html(self._(error_message)) 556 self.write_html(self._(error_message))
552 else: 557 else:
553 self.write_html(html) 558 self.write_html(html)
554 559
555 def clean_sessions(self): 560 def clean_sessions(self):
1016 self.write_file(filename) 1021 self.write_file(filename)
1017 else: 1022 else:
1018 self.additional_headers['Content-Length'] = str(len(content)) 1023 self.additional_headers['Content-Length'] = str(len(content))
1019 self.write(content) 1024 self.write(content)
1020 1025
1021 def send_html_to_admin(self, subject, content): 1026 def send_error_to_admin(self, subject, html, txt):
1022 1027 """Send traceback information to admin via email.
1028 We send both, the formatted html (with more information) and
1029 the text version of the traceback. We use
1030 multipart/alternative so the receiver can chose which version
1031 to display.
1032 """
1023 to = [self.mailer.config.ADMIN_EMAIL] 1033 to = [self.mailer.config.ADMIN_EMAIL]
1024 message = self.mailer.get_standard_message(to, subject) 1034 message = MIMEMultipart('alternative')
1025 # delete existing content-type headers 1035 self.mailer.set_message_attributes(message, to, subject)
1026 del message['Content-type'] 1036 part = MIMEBase('text', 'html')
1027 message['Content-type'] = 'text/html; charset=utf-8' 1037 part.set_charset('utf-8')
1028 message.set_payload(content) 1038 part.set_payload(html)
1029 encode_quopri(message) 1039 encode_quopri(part)
1040 message.attach(part)
1041 part = MIMEText(txt)
1042 message.attach(part)
1030 self.mailer.smtp_send(to, message.as_string()) 1043 self.mailer.smtp_send(to, message.as_string())
1031 1044
1032 def renderFrontPage(self, message): 1045 def renderFrontPage(self, message):
1033 """Return the front page of the tracker.""" 1046 """Return the front page of the tracker."""
1034 1047
1082 exc_info = sys.exc_info() 1095 exc_info = sys.exc_info()
1083 try: 1096 try:
1084 # If possible, send the HTML page template traceback 1097 # If possible, send the HTML page template traceback
1085 # to the administrator. 1098 # to the administrator.
1086 subject = "Templating Error: %s" % exc_info[1] 1099 subject = "Templating Error: %s" % exc_info[1]
1087 self.send_html_to_admin(subject, cgitb.pt_html()) 1100 self.send_error_to_admin(subject, cgitb.pt_html(), format_exc())
1088 # Now report the error to the user. 1101 # Now report the error to the user.
1089 return self._(error_message) 1102 return self._(error_message)
1090 except: 1103 except:
1091 # Reraise the original exception. The user will 1104 # Reraise the original exception. The user will
1092 # receive an error message, and the adminstrator will 1105 # receive an error message, and the adminstrator will

Roundup Issue Tracker: http://roundup-tracker.org/