From 1c9c29b5b2a3b9b8375a32534dd60c486042b5a3 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sun, 12 Feb 2012 12:19:02 +0000 Subject: [PATCH] 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 --- django/utils/log.py | 2 +- tests/regressiontests/logging_tests/tests.py | 74 +++++++++++++++----- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/django/utils/log.py b/django/utils/log.py index 16ad5f05de..df2089f924 100644 --- a/django/utils/log.py +++ b/django/utils/log.py @@ -49,7 +49,7 @@ class AdminEmailHandler(logging.Handler): 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) diff --git a/tests/regressiontests/logging_tests/tests.py b/tests/regressiontests/logging_tests/tests.py index 8eba44bf77..4e29e25d99 100644 --- a/tests/regressiontests/logging_tests/tests.py +++ b/tests/regressiontests/logging_tests/tests.py @@ -4,7 +4,7 @@ import copy 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 @@ class CallbackFilterTest(TestCase): 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 @@ class AdminEmailHandlerTest(TestCase): 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 - # 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' + + 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'),),