Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed AdminEmailHandler to format the subject message correctly when …

…arguments are passed.

This bug was hidden before r17480 as it basically didn't take the arguments into account. Refs #14973.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17512 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1c9c29b5b2a3b9b8375a32534dd60c486042b5a3 1 parent 328c70e
@jezdez jezdez authored
Showing with 58 additions and 18 deletions.
  1. +1 −1  django/utils/log.py
  2. +57 −17 tests/regressiontests/logging_tests/tests.py
View
2  django/utils/log.py
@@ -49,7 +49,7 @@ def emit(self, record):
record.levelname,
(request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
and 'internal' or 'EXTERNAL'),
- record.msg
+ record.getMessage()
)
filter = get_exception_reporter_filter(request)
request_repr = filter.get_request_repr(request)
View
74 tests/regressiontests/logging_tests/tests.py
@@ -4,7 +4,7 @@
from django.conf import compat_patch_logging_config
from django.core import mail
-from django.test import TestCase
+from django.test import TestCase, RequestFactory
from django.test.utils import override_settings
from django.utils.log import CallbackFilter, RequireDebugFalse, getLogger
@@ -124,6 +124,16 @@ def _callback(record):
class AdminEmailHandlerTest(TestCase):
+ def get_admin_email_handler(self, logger):
+ # Inspired from regressiontests/views/views.py: send_log()
+ # ensuring the AdminEmailHandler does not get filtered out
+ # even with DEBUG=True.
+ admin_email_handler = [
+ h for h in logger.handlers
+ if h.__class__.__name__ == "AdminEmailHandler"
+ ][0]
+ return admin_email_handler
+
@override_settings(
ADMINS=(('whatever admin', 'admin@example.com'),),
EMAIL_SUBJECT_PREFIX='-SuperAwesomeSubject-'
@@ -134,32 +144,62 @@ def test_accepts_args(self):
setting are used to compose the email subject.
Refs #16736.
"""
-
message = "Custom message that says '%s' and '%s'"
token1 = 'ping'
token2 = 'pong'
logger = getLogger('django.request')
- # Inspired from regressiontests/views/views.py: send_log()
- # ensuring the AdminEmailHandler does not get filtered out
- # even with DEBUG=True.
- admin_email_handler = [
- h for h in logger.handlers
- if h.__class__.__name__ == "AdminEmailHandler"
- ][0]
+ admin_email_handler = self.get_admin_email_handler(logger)
# Backup then override original filters
orig_filters = admin_email_handler.filters
- admin_email_handler.filters = []
+ try:
+ admin_email_handler.filters = []
- logger.error(message, token1, token2)
+ logger.error(message, token1, token2)
- self.assertEqual(len(mail.outbox), 1)
- self.assertEqual(mail.outbox[0].to, ['admin@example.com'])
- self.assertEqual(mail.outbox[0].subject,
- "-SuperAwesomeSubject-ERROR: Custom message that says 'ping' and 'pong'")
+ self.assertEqual(len(mail.outbox), 1)
+ self.assertEqual(mail.outbox[0].to, ['admin@example.com'])
+ self.assertEqual(mail.outbox[0].subject,
+ "-SuperAwesomeSubject-ERROR: Custom message that says 'ping' and 'pong'")
+ finally:
+ # Restore original filters
+ admin_email_handler.filters = orig_filters
+
+ @override_settings(
+ ADMINS=(('whatever admin', 'admin@example.com'),),
+ EMAIL_SUBJECT_PREFIX='-SuperAwesomeSubject-',
+ INTERNAL_IPS=('127.0.0.1',),
+ )
+ def test_accepts_args_and_request(self):
+ """
+ Ensure that the subject is also handled if being
+ passed a request object.
+ """
+ message = "Custom message that says '%s' and '%s'"
+ token1 = 'ping'
+ token2 = 'pong'
- # Restore original filters
- admin_email_handler.filters = orig_filters
+ logger = getLogger('django.request')
+ admin_email_handler = self.get_admin_email_handler(logger)
+ # Backup then override original filters
+ orig_filters = admin_email_handler.filters
+ try:
+ admin_email_handler.filters = []
+ rf = RequestFactory()
+ request = rf.get('/')
+ logger.error(message, token1, token2,
+ extra={
+ 'status_code': 403,
+ 'request': request,
+ }
+ )
+ self.assertEqual(len(mail.outbox), 1)
+ self.assertEqual(mail.outbox[0].to, ['admin@example.com'])
+ self.assertEqual(mail.outbox[0].subject,
+ "-SuperAwesomeSubject-ERROR (internal IP): Custom message that says 'ping' and 'pong'")
+ finally:
+ # Restore original filters
+ admin_email_handler.filters = orig_filters
@override_settings(
ADMINS=(('admin', 'admin@example.com'),),
Please sign in to comment.
Something went wrong with that request. Please try again.