Skip to content

Commit

Permalink
Fixed AdminEmailHandler to format the subject message correctly when …
Browse files Browse the repository at this point in the history
…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
jezdez committed Feb 12, 2012
1 parent 328c70e commit 1c9c29b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion django/utils/log.py
Expand Up @@ -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)
Expand Down
74 changes: 57 additions & 17 deletions tests/regressiontests/logging_tests/tests.py
Expand Up @@ -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

Expand Down Expand Up @@ -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-'
Expand All @@ -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'),),
Expand Down

0 comments on commit 1c9c29b

Please sign in to comment.