From 50a702f3fd87e271945aa5e88ae8a39d7a2149fd Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Tue, 9 Apr 2024 16:01:37 +0100 Subject: [PATCH] Fixed #35364 -- Stopped AdminEmailHandler rendering email unnecessarily. --- django/utils/log.py | 7 +++++++ tests/logging_tests/tests.py | 8 +++++++- tests/view_tests/tests/test_defaults.py | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/django/utils/log.py b/django/utils/log.py index fd0cc1bdc1f..a25b97a7d5a 100644 --- a/django/utils/log.py +++ b/django/utils/log.py @@ -92,6 +92,13 @@ class AdminEmailHandler(logging.Handler): ) def emit(self, record): + # Early return when no email will be sent. + if ( + not settings.ADMINS + # Method not overridden. + and self.send_mail.__func__ is AdminEmailHandler.send_mail + ): + return try: request = record.request subject = "%s (%s IP): %s" % ( diff --git a/tests/logging_tests/tests.py b/tests/logging_tests/tests.py index 747c929cde5..610bdc11243 100644 --- a/tests/logging_tests/tests.py +++ b/tests/logging_tests/tests.py @@ -1,6 +1,7 @@ import logging from contextlib import contextmanager from io import StringIO +from unittest import mock from admin_scripts.tests import AdminScriptTestCase @@ -482,7 +483,12 @@ class AdminEmailHandlerTest(SimpleTestCase): None, None, ) - handler.emit(record) + with mock.patch.object( + handler, + "format_subject", + side_effect=AssertionError("Should not be called"), + ): + handler.emit(record) self.assertEqual(len(mail.outbox), 0) diff --git a/tests/view_tests/tests/test_defaults.py b/tests/view_tests/tests/test_defaults.py index 415a9a8c674..66bc1da1688 100644 --- a/tests/view_tests/tests/test_defaults.py +++ b/tests/view_tests/tests/test_defaults.py @@ -123,7 +123,7 @@ class DefaultsTests(TestCase): ) def test_custom_bad_request_template(self): response = self.client.get("/raises400/") - self.assertIs(response.wsgi_request, response.context[-1].request) + self.assertIs(response.wsgi_request, response.context.request) @override_settings( TEMPLATES=[