Add two middleware classes:
- SQLLogMiddleware. This logs any INSERT or UPDATE performed by a request. - SMTPExceptionMiddleware. This renders a "please try again" (template email_failed.html) message when an attempt to send email failed. This uses a bit of a hack, in that the middleware looks explicitly for smtplib.SMTPException, and smtplib can raise other exceptions (particularly socket errors). utils/mail/send_smtp catches all exceptions and reraises non-smtplib exceptions as fake smtplib exceptions, and the middleware undoes the wrapping. - Legacy-Id: 224
This commit is contained in:
parent
600002f1f8
commit
cae902a373
|
@ -1,9 +1,16 @@
|
||||||
|
try:
|
||||||
# From http://www.djangosnippets.org/snippets/172/
|
import tidy
|
||||||
# Uses python-utidylib, http://utidylib.berlios.de/,
|
tidyavail = True
|
||||||
# which uses HTML Tidy, http://tidy.sourceforge.net/
|
except ImportError:
|
||||||
|
tidyavail = False
|
||||||
import tidy
|
from django.db import connection
|
||||||
|
from django.shortcuts import render_to_response
|
||||||
|
from django.template import RequestContext
|
||||||
|
from ietf.utils import log
|
||||||
|
import re
|
||||||
|
import smtplib
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
|
||||||
options = dict(
|
options = dict(
|
||||||
output_xhtml=True,
|
output_xhtml=True,
|
||||||
|
@ -16,11 +23,39 @@ options = dict(
|
||||||
|
|
||||||
|
|
||||||
class PrettifyMiddleware(object):
|
class PrettifyMiddleware(object):
|
||||||
"""Prettify middleware"""
|
"""Prettify middleware
|
||||||
|
From http://www.djangosnippets.org/snippets/172/
|
||||||
|
Uses python-utidylib, http://utidylib.berlios.de/,
|
||||||
|
which uses HTML Tidy, http://tidy.sourceforge.net/
|
||||||
|
"""
|
||||||
|
|
||||||
def process_response(self, request, response):
|
def process_response(self, request, response):
|
||||||
if response.headers['Content-Type'].split(';', 1)[0] in ['text/html']:
|
if tidyavail and response.headers['Content-Type'].split(';', 1)[0] in ['text/html']:
|
||||||
content = response.content
|
content = response.content
|
||||||
content = str(tidy.parseString(content, **options))
|
content = str(tidy.parseString(content, **options))
|
||||||
response.content = content
|
response.content = content
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
class SQLLogMiddleware(object):
|
||||||
|
def process_response(self, request, response):
|
||||||
|
for q in connection.queries:
|
||||||
|
if re.match('(update|insert)', q['sql'], re.IGNORECASE):
|
||||||
|
log(q['sql'])
|
||||||
|
return response
|
||||||
|
|
||||||
|
class SMTPExceptionMiddleware(object):
|
||||||
|
def process_exception(self, request, exception):
|
||||||
|
if isinstance(exception, smtplib.SMTPException):
|
||||||
|
type = sys.exc_info()[0]
|
||||||
|
value = sys.exc_info()[1]
|
||||||
|
# See if it's a non-smtplib exception that we faked
|
||||||
|
if type == smtplib.SMTPException and len(value.args) == 1 and isinstance(value.args[0], dict) and value.args[0].has_key('really'):
|
||||||
|
orig = value.args[0]
|
||||||
|
type = orig['really']
|
||||||
|
tb = traceback.format_tb(orig['tb'])
|
||||||
|
value = orig['value']
|
||||||
|
else:
|
||||||
|
tb = traceback.format_tb(sys.exc_info()[2])
|
||||||
|
return render_to_response('email_failed.html', {'exception': type, 'args': value, 'traceback': "".join(tb)},
|
||||||
|
context_instance=RequestContext(request))
|
||||||
|
return None
|
||||||
|
|
|
@ -81,6 +81,8 @@ MIDDLEWARE_CLASSES = (
|
||||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
'django.middleware.doc.XViewMiddleware',
|
'django.middleware.doc.XViewMiddleware',
|
||||||
# 'ietf.middleware.PrettifyMiddleware',
|
# 'ietf.middleware.PrettifyMiddleware',
|
||||||
|
'ietf.middleware.SQLLogMiddleware',
|
||||||
|
'ietf.middleware.SMTPExceptionMiddleware',
|
||||||
'django.middleware.transaction.TransactionMiddleware',
|
'django.middleware.transaction.TransactionMiddleware',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ def send_smtp(msg):
|
||||||
add_headers(msg)
|
add_headers(msg)
|
||||||
(fname, frm) = parseaddr(msg.get('From'))
|
(fname, frm) = parseaddr(msg.get('From'))
|
||||||
to = [addr for name, addr in getaddresses(msg.get_all('To') + msg.get_all('Cc', []))]
|
to = [addr for name, addr in getaddresses(msg.get_all('To') + msg.get_all('Cc', []))]
|
||||||
|
server = None
|
||||||
try:
|
try:
|
||||||
server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
|
server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
|
@ -37,11 +38,15 @@ def send_smtp(msg):
|
||||||
server.sendmail(frm, to, msg.as_string())
|
server.sendmail(frm, to, msg.as_string())
|
||||||
# note: should pay attention to the return code, as it may
|
# note: should pay attention to the return code, as it may
|
||||||
# indicate that someone didn't get the email.
|
# indicate that someone didn't get the email.
|
||||||
except smtplib.SMTPException:
|
except:
|
||||||
server.quit()
|
if server:
|
||||||
|
server.quit()
|
||||||
# need to improve log message
|
# need to improve log message
|
||||||
log("got exception '%s' (%s) trying to send email from '%s' to %s subject '%s'" % (sys.exc_info()[0], sys.exc_info()[1], frm, to, msg.get('Subject', '[no subject]')))
|
log("got exception '%s' (%s) trying to send email from '%s' to %s subject '%s'" % (sys.exc_info()[0], sys.exc_info()[1], frm, to, msg.get('Subject', '[no subject]')))
|
||||||
raise
|
if isinstance(sys.exc_info()[0], smtplib.SMTPException):
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
raise smtplib.SMTPException({'really': sys.exc_info()[0], 'value': sys.exc_info()[1], 'tb': sys.exc_info()[2]})
|
||||||
server.quit()
|
server.quit()
|
||||||
log("sent email from '%s' to %s subject '%s'" % (frm, to, msg.get('Subject', '[no subject]')))
|
log("sent email from '%s' to %s subject '%s'" % (frm, to, msg.get('Subject', '[no subject]')))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue