Browse Source

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

Berker Peksag 10 năm trước cách đây
mục cha
commit
d552da1f8d
4 tập tin đã thay đổi với 38 bổ sung6 xóa
  1. 4 3
      django/utils/log.py
  2. 7 0
      docs/releases/1.8.txt
  3. 8 1
      docs/topics/logging.txt
  4. 19 2
      tests/logging_tests/tests.py

+ 4 - 3
django/utils/log.py

@@ -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)

+ 7 - 0
docs/releases/1.8.txt

@@ -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
 ^^^^^^^^^^^^^^^^^^^
 

+ 8 - 1
docs/topics/logging.txt

@@ -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
 -------

+ 19 - 2
tests/logging_tests/tests.py

@@ -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):
     """