Refs #29983 -- Added pathlib.Path support to the file email backend.

This commit is contained in:
Jon Dufresne 2019-11-06 00:33:07 -08:00 committed by Carlton Gibson
parent 422b875c65
commit fbbff7f808
5 changed files with 25 additions and 5 deletions

View File

@ -17,9 +17,6 @@ class EmailBackend(ConsoleEmailBackend):
self.file_path = file_path
else:
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)
try:
os.makedirs(self.file_path, exist_ok=True)

View File

@ -1283,6 +1283,10 @@ Default: Not defined
The directory used by the :ref:`file email backend <topic-email-file-backend>`
to store output files.
.. versionchanged:: 3.1
Support for :class:`pathlib.Path` was added.
.. setting:: EMAIL_HOST
``EMAIL_HOST``

View File

@ -119,7 +119,8 @@ CSRF
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
~~~~~~~~~~~~

View File

@ -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
convenience that can be used during development.
.. versionchanged:: 3.1
Support for :class:`pathlib.Path` was added.
.. _topic-email-memory-backend:
In-memory backend

View File

@ -12,6 +12,7 @@ from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr
from io import StringIO
from pathlib import Path
from smtplib import SMTP, SMTPAuthenticationError, SMTPException
from ssl import SSLError
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),
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)
@override_settings(
@ -1154,7 +1159,7 @@ class FileBackendTests(BaseEmailBackendTests, SimpleTestCase):
def setUp(self):
super().setUp()
self.tmp_dir = tempfile.mkdtemp()
self.tmp_dir = self.mkdtemp()
self.addCleanup(shutil.rmtree, self.tmp_dir)
self._settings_override = override_settings(EMAIL_FILE_PATH=self.tmp_dir)
self._settings_override.enable()
@ -1163,6 +1168,9 @@ class FileBackendTests(BaseEmailBackendTests, SimpleTestCase):
self._settings_override.disable()
super().tearDown()
def mkdtemp(self):
return tempfile.mkdtemp()
def flush_mailbox(self):
for filename in os.listdir(self.tmp_dir):
os.unlink(os.path.join(self.tmp_dir, filename))
@ -1209,6 +1217,12 @@ class FileBackendTests(BaseEmailBackendTests, SimpleTestCase):
connection.close()
class FileBackendPathLibTests(FileBackendTests):
def mkdtemp(self):
tmp_dir = super().mkdtemp()
return Path(tmp_dir)
class ConsoleBackendTests(BaseEmailBackendTests, SimpleTestCase):
email_backend = 'django.core.mail.backends.console.EmailBackend'