Ticket #17281: django-17281.2.diff

File django-17281.2.diff, 3.1 KB (added by marw85, 3 years ago)

added tests, escape characters instead of truncate on newline

  • AUTHORS

    diff --git a/AUTHORS b/AUTHORS
    index 9ffd6cd..d2eb4a1 100644
    a b answer newbie questions, and generally made Django that much better: 
    450450    Manuel Saelices <msaelices@yaco.es>
    451451    Ivan Sagalaev (Maniac) <http://www.softwaremaniacs.org/>
    452452    Vinay Sajip <vinay_sajip@yahoo.co.uk>
     453    Bartolome Sanchez Salado <i42sasab@uco.es>
     454    Marcin Wróbel
    453455    Kadesarin Sanjek
    454456    Massimo Scamarcia <massimo.scamarcia@gmail.com>
    455457    Paulo Scardine <paulo@scardine.com.br>
  • django/utils/log.py

    diff --git a/django/utils/log.py b/django/utils/log.py
    index 8ce37f5..8be0a83 100644
    a b class AdminEmailHandler(logging.Handler): 
    5959            )
    6060            request = None
    6161            request_repr = "Request repr() unavailable."
     62        subject = self.format_subject_for_email(subject)
    6263
    6364        if record.exc_info:
    6465            exc_info = record.exc_info
    class AdminEmailHandler(logging.Handler): 
    7273        html_message = self.include_html and reporter.get_traceback_html() or None
    7374        mail.mail_admins(subject, message, fail_silently=True, html_message=html_message)
    7475
     76    def format_subject_for_email(self, subject):
     77        """
     78        Escape CR and LF characters, limit length.
     79        RFC2822 - hard limit is 998 minus 'Subject: ' equals 989
     80        """
     81        if subject is None:
     82            return 'None'
     83        else:
     84            subject_repr = subject.replace('\n', '\\n').replace('\r', '\\r')
     85            return subject_repr[:989]
     86
    7587
    7688class CallbackFilter(logging.Filter):
    7789    """
  • tests/regressiontests/logging_tests/tests.py

    diff --git a/tests/regressiontests/logging_tests/tests.py b/tests/regressiontests/logging_tests/tests.py
    index a2c178c..f91cfb9 100644
    a b class AdminEmailHandlerTest(TestCase): 
    160160
    161161        # Restore original filters
    162162        admin_email_handler.filters = orig_filters
     163
     164    @override_settings(
     165            ADMINS=(('admin', 'admin@example.com'),),
     166            EMAIL_SUBJECT_PREFIX='',
     167            DEBUG=False,
     168        )
     169    def test_subject_accepts_newlines(self):
     170        message = u'Message \r\n with newslines'
     171        expected_subject = u'ERROR: Message \\r\\n with newslines'
     172
     173        self.assertEqual(len(mail.outbox), 0)
     174
     175        logger = getLogger('django.request')
     176        logger.error(message)
     177
     178        self.assertEqual(len(mail.outbox), 1)
     179        self.assertFalse('\n' in mail.outbox[0].subject)
     180        self.assertEqual(mail.outbox[0].subject, expected_subject)
     181
     182    @override_settings(
     183            ADMINS=(('admin', 'admin@example.com'),),
     184            EMAIL_SUBJECT_PREFIX='',
     185            DEBUG=False,
     186        )
     187    def test_truncate_subject(self):
     188        message = '0123456789' * 100
     189        expected_subject = 'ERROR: ' + '0123456789' * 98 + '01'
     190
     191        self.assertEqual(len(mail.outbox), 0)
     192
     193        logger = getLogger('django.request')
     194        logger.error(message)
     195
     196        self.assertEqual(len(mail.outbox), 1)
     197        self.assertEqual(mail.outbox[0].subject, expected_subject)
Back to Top