Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Jannis Leidel authored February 12, 2012
2  django/utils/log.py
@@ -49,7 +49,7 @@ def emit(self, record):
49 49
                 record.levelname,
50 50
                 (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
51 51
                  and 'internal' or 'EXTERNAL'),
52  
-                record.msg
  52
+                record.getMessage()
53 53
             )
54 54
             filter = get_exception_reporter_filter(request)
55 55
             request_repr = filter.get_request_repr(request)
74  tests/regressiontests/logging_tests/tests.py
@@ -4,7 +4,7 @@
4 4
 
5 5
 from django.conf import compat_patch_logging_config
6 6
 from django.core import mail
7  
-from django.test import TestCase
  7
+from django.test import TestCase, RequestFactory
8 8
 from django.test.utils import override_settings
9 9
 from django.utils.log import CallbackFilter, RequireDebugFalse, getLogger
10 10
 
@@ -124,6 +124,16 @@ def _callback(record):
124 124
 
125 125
 class AdminEmailHandlerTest(TestCase):
126 126
 
  127
+    def get_admin_email_handler(self, logger):
  128
+        # Inspired from regressiontests/views/views.py: send_log()
  129
+        # ensuring the AdminEmailHandler does not get filtered out
  130
+        # even with DEBUG=True.
  131
+        admin_email_handler = [
  132
+            h for h in logger.handlers
  133
+            if h.__class__.__name__ == "AdminEmailHandler"
  134
+            ][0]
  135
+        return admin_email_handler
  136
+
127 137
     @override_settings(
128 138
             ADMINS=(('whatever admin', 'admin@example.com'),),
129 139
             EMAIL_SUBJECT_PREFIX='-SuperAwesomeSubject-'
@@ -134,32 +144,62 @@ def test_accepts_args(self):
134 144
         setting are used to compose the email subject.
135 145
         Refs #16736.
136 146
         """
137  
-
138 147
         message = "Custom message that says '%s' and '%s'"
139 148
         token1 = 'ping'
140 149
         token2 = 'pong'
141 150
 
142 151
         logger = getLogger('django.request')
143  
-        # Inspired from regressiontests/views/views.py: send_log()
144  
-        # ensuring the AdminEmailHandler does not get filtered out
145  
-        # even with DEBUG=True.
146  
-        admin_email_handler = [
147  
-            h for h in logger.handlers
148  
-            if h.__class__.__name__ == "AdminEmailHandler"
149  
-            ][0]
  152
+        admin_email_handler = self.get_admin_email_handler(logger)
150 153
         # Backup then override original filters
151 154
         orig_filters = admin_email_handler.filters
152  
-        admin_email_handler.filters = []
  155
+        try:
  156
+            admin_email_handler.filters = []
153 157
 
154  
-        logger.error(message, token1, token2)
  158
+            logger.error(message, token1, token2)
155 159
 
156  
-        self.assertEqual(len(mail.outbox), 1)
157  
-        self.assertEqual(mail.outbox[0].to, ['admin@example.com'])
158  
-        self.assertEqual(mail.outbox[0].subject,
159  
-                         "-SuperAwesomeSubject-ERROR: Custom message that says 'ping' and 'pong'")
  160
+            self.assertEqual(len(mail.outbox), 1)
  161
+            self.assertEqual(mail.outbox[0].to, ['admin@example.com'])
  162
+            self.assertEqual(mail.outbox[0].subject,
  163
+                             "-SuperAwesomeSubject-ERROR: Custom message that says 'ping' and 'pong'")
  164
+        finally:
  165
+            # Restore original filters
  166
+            admin_email_handler.filters = orig_filters
  167
+
  168
+    @override_settings(
  169
+            ADMINS=(('whatever admin', 'admin@example.com'),),
  170
+            EMAIL_SUBJECT_PREFIX='-SuperAwesomeSubject-',
  171
+            INTERNAL_IPS=('127.0.0.1',),
  172
+        )
  173
+    def test_accepts_args_and_request(self):
  174
+        """
  175
+        Ensure that the subject is also handled if being
  176
+        passed a request object.
  177
+        """
  178
+        message = "Custom message that says '%s' and '%s'"
  179
+        token1 = 'ping'
  180
+        token2 = 'pong'
160 181
 
161  
-        # Restore original filters
162  
-        admin_email_handler.filters = orig_filters
  182
+        logger = getLogger('django.request')
  183
+        admin_email_handler = self.get_admin_email_handler(logger)
  184
+        # Backup then override original filters
  185
+        orig_filters = admin_email_handler.filters
  186
+        try:
  187
+            admin_email_handler.filters = []
  188
+            rf = RequestFactory()
  189
+            request = rf.get('/')
  190
+            logger.error(message, token1, token2,
  191
+                extra={
  192
+                    'status_code': 403,
  193
+                    'request': request,
  194
+                }
  195
+            )
  196
+            self.assertEqual(len(mail.outbox), 1)
  197
+            self.assertEqual(mail.outbox[0].to, ['admin@example.com'])
  198
+            self.assertEqual(mail.outbox[0].subject,
  199
+                             "-SuperAwesomeSubject-ERROR (internal IP): Custom message that says 'ping' and 'pong'")
  200
+        finally:
  201
+            # Restore original filters
  202
+            admin_email_handler.filters = orig_filters
163 203
 
164 204
     @override_settings(
165 205
             ADMINS=(('admin', 'admin@example.com'),),

0 notes on commit 1c9c29b

Please sign in to comment.
Something went wrong with that request. Please try again.