Fixed #35364 -- Stopped AdminEmailHandler rendering email unnecessarily.

This commit is contained in:
Adam Johnson 2024-04-09 16:01:37 +01:00 committed by Sarah Boyce
parent b0f2289426
commit 50a702f3fd
3 changed files with 15 additions and 2 deletions

View File

@ -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" % (

View File

@ -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)

View File

@ -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=[