mirror of https://github.com/django/django.git
Refs #29983 -- Added pathlib.Path support to the file email backend.
This commit is contained in:
parent
422b875c65
commit
fbbff7f808
|
@ -17,9 +17,6 @@ class EmailBackend(ConsoleEmailBackend):
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
else:
|
else:
|
||||||
self.file_path = getattr(settings, 'EMAIL_FILE_PATH', None)
|
self.file_path = getattr(settings, 'EMAIL_FILE_PATH', None)
|
||||||
# Make sure self.file_path is a string.
|
|
||||||
if not isinstance(self.file_path, str):
|
|
||||||
raise ImproperlyConfigured('Path for saving emails is invalid: %r' % self.file_path)
|
|
||||||
self.file_path = os.path.abspath(self.file_path)
|
self.file_path = os.path.abspath(self.file_path)
|
||||||
try:
|
try:
|
||||||
os.makedirs(self.file_path, exist_ok=True)
|
os.makedirs(self.file_path, exist_ok=True)
|
||||||
|
|
|
@ -1283,6 +1283,10 @@ Default: Not defined
|
||||||
The directory used by the :ref:`file email backend <topic-email-file-backend>`
|
The directory used by the :ref:`file email backend <topic-email-file-backend>`
|
||||||
to store output files.
|
to store output files.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.1
|
||||||
|
|
||||||
|
Support for :class:`pathlib.Path` was added.
|
||||||
|
|
||||||
.. setting:: EMAIL_HOST
|
.. setting:: EMAIL_HOST
|
||||||
|
|
||||||
``EMAIL_HOST``
|
``EMAIL_HOST``
|
||||||
|
|
|
@ -119,7 +119,8 @@ CSRF
|
||||||
Email
|
Email
|
||||||
~~~~~
|
~~~~~
|
||||||
|
|
||||||
* ...
|
* The :setting:`EMAIL_FILE_PATH` setting, used by the :ref:`file email backend
|
||||||
|
<topic-email-file-backend>`, now supports :class:`pathlib.Path`.
|
||||||
|
|
||||||
File Storage
|
File Storage
|
||||||
~~~~~~~~~~~~
|
~~~~~~~~~~~~
|
||||||
|
|
|
@ -527,6 +527,10 @@ To specify this backend, put the following in your settings::
|
||||||
This backend is not intended for use in production -- it is provided as a
|
This backend is not intended for use in production -- it is provided as a
|
||||||
convenience that can be used during development.
|
convenience that can be used during development.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.1
|
||||||
|
|
||||||
|
Support for :class:`pathlib.Path` was added.
|
||||||
|
|
||||||
.. _topic-email-memory-backend:
|
.. _topic-email-memory-backend:
|
||||||
|
|
||||||
In-memory backend
|
In-memory backend
|
||||||
|
|
|
@ -12,6 +12,7 @@ from email.header import Header
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.utils import parseaddr
|
from email.utils import parseaddr
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
from pathlib import Path
|
||||||
from smtplib import SMTP, SMTPAuthenticationError, SMTPException
|
from smtplib import SMTP, SMTPAuthenticationError, SMTPException
|
||||||
from ssl import SSLError
|
from ssl import SSLError
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
@ -567,6 +568,10 @@ class MailTests(HeadersCheckMixin, SimpleTestCase):
|
||||||
mail.get_connection('django.core.mail.backends.filebased.EmailBackend', file_path=tmp_dir),
|
mail.get_connection('django.core.mail.backends.filebased.EmailBackend', file_path=tmp_dir),
|
||||||
filebased.EmailBackend
|
filebased.EmailBackend
|
||||||
)
|
)
|
||||||
|
|
||||||
|
msg = 'expected str, bytes or os.PathLike object, not object'
|
||||||
|
with self.assertRaisesMessage(TypeError, msg):
|
||||||
|
mail.get_connection('django.core.mail.backends.filebased.EmailBackend', file_path=object())
|
||||||
self.assertIsInstance(mail.get_connection(), locmem.EmailBackend)
|
self.assertIsInstance(mail.get_connection(), locmem.EmailBackend)
|
||||||
|
|
||||||
@override_settings(
|
@override_settings(
|
||||||
|
@ -1154,7 +1159,7 @@ class FileBackendTests(BaseEmailBackendTests, SimpleTestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
self.tmp_dir = tempfile.mkdtemp()
|
self.tmp_dir = self.mkdtemp()
|
||||||
self.addCleanup(shutil.rmtree, self.tmp_dir)
|
self.addCleanup(shutil.rmtree, self.tmp_dir)
|
||||||
self._settings_override = override_settings(EMAIL_FILE_PATH=self.tmp_dir)
|
self._settings_override = override_settings(EMAIL_FILE_PATH=self.tmp_dir)
|
||||||
self._settings_override.enable()
|
self._settings_override.enable()
|
||||||
|
@ -1163,6 +1168,9 @@ class FileBackendTests(BaseEmailBackendTests, SimpleTestCase):
|
||||||
self._settings_override.disable()
|
self._settings_override.disable()
|
||||||
super().tearDown()
|
super().tearDown()
|
||||||
|
|
||||||
|
def mkdtemp(self):
|
||||||
|
return tempfile.mkdtemp()
|
||||||
|
|
||||||
def flush_mailbox(self):
|
def flush_mailbox(self):
|
||||||
for filename in os.listdir(self.tmp_dir):
|
for filename in os.listdir(self.tmp_dir):
|
||||||
os.unlink(os.path.join(self.tmp_dir, filename))
|
os.unlink(os.path.join(self.tmp_dir, filename))
|
||||||
|
@ -1209,6 +1217,12 @@ class FileBackendTests(BaseEmailBackendTests, SimpleTestCase):
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
|
|
||||||
|
class FileBackendPathLibTests(FileBackendTests):
|
||||||
|
def mkdtemp(self):
|
||||||
|
tmp_dir = super().mkdtemp()
|
||||||
|
return Path(tmp_dir)
|
||||||
|
|
||||||
|
|
||||||
class ConsoleBackendTests(BaseEmailBackendTests, SimpleTestCase):
|
class ConsoleBackendTests(BaseEmailBackendTests, SimpleTestCase):
|
||||||
email_backend = 'django.core.mail.backends.console.EmailBackend'
|
email_backend = 'django.core.mail.backends.console.EmailBackend'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue