Fixed #22407 -- Added AdminEmailHandler.send_mail().

This commit is contained in:
Berker Peksag 2014-11-10 23:21:31 +02:00 committed by Tim Graham
parent a305695f28
commit d552da1f8d
4 changed files with 38 additions and 6 deletions

View File

@ -127,9 +127,10 @@ class AdminEmailHandler(logging.Handler):
message = "%s\n\nRequest repr(): %s" % (self.format(record), request_repr)
reporter = ExceptionReporter(request, is_email=True, *exc_info)
html_message = reporter.get_traceback_html() if self.include_html else None
mail.mail_admins(subject, message, fail_silently=True,
html_message=html_message,
connection=self.connection())
self.send_mail(subject, message, fail_silently=True, html_message=html_message)
def send_mail(self, subject, message, *args, **kwargs):
mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
def connection(self):
return get_connection(backend=self.email_backend, fail_silently=True)

View File

@ -271,6 +271,13 @@ Internationalization
reusable apps. It also allows overriding those custom formats in your main
Django project.
Logging
^^^^^^^
* The :class:`django.utils.log.AdminEmailHandler` class now has a
:meth:`~django.utils.log.AdminEmailHandler.send_mail` method to make it more
subclass friendly.
Management Commands
^^^^^^^^^^^^^^^^^^^

View File

@ -563,8 +563,15 @@ Python logging module.
By default, an instance of the email backend specified in
:setting:`EMAIL_BACKEND` will be used.
.. _Sentry: https://pypi.python.org/pypi/sentry
.. method:: send_mail(subject, message, *args, **kwargs)
.. versionadded:: 1.8
Sends emails to admin users. To customize this behavior, you can
subclass the :class:`~django.utils.log.AdminEmailHandler` class and
override this method.
.. _Sentry: https://pypi.python.org/pypi/sentry
Filters
-------

View File

@ -10,8 +10,9 @@ from django.test import TestCase, RequestFactory, override_settings
from django.test.utils import patch_logger
from django.utils.encoding import force_text
from django.utils.deprecation import RemovedInNextVersionWarning
from django.utils.log import (CallbackFilter, RequireDebugFalse,
RequireDebugTrue)
from django.utils.log import (
AdminEmailHandler, CallbackFilter, RequireDebugFalse, RequireDebugTrue,
)
from django.utils.six import StringIO
from admin_scripts.tests import AdminScriptTestCase
@ -341,6 +342,22 @@ class AdminEmailHandlerTest(TestCase):
self.assertEqual(msg.subject, "[Django] ERROR (EXTERNAL IP): message")
self.assertIn("path:%s" % url_path, msg.body)
@override_settings(
MANAGERS=(('manager', 'manager@example.com'),),
DEBUG=False,
)
def test_customize_send_mail_method(self):
class ManagerEmailHandler(AdminEmailHandler):
def send_mail(self, subject, message, *args, **kwargs):
mail.mail_managers(subject, message, *args, connection=self.connection(), **kwargs)
handler = ManagerEmailHandler()
record = self.logger.makeRecord('name', logging.ERROR, 'function', 'lno', 'message', None, None)
self.assertEqual(len(mail.outbox), 0)
handler.emit(record)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to, ['manager@example.com'])
class SettingsConfigTest(AdminScriptTestCase):
"""