2009-11-03 20:53:26 +08:00
|
|
|
"""Email backend that writes messages to a file."""
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import os
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-02-10 02:19:34 +08:00
|
|
|
from django.core.mail.backends.console import \
|
|
|
|
EmailBackend as ConsoleEmailBackend
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2009-11-03 20:53:26 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2009-11-03 20:53:26 +08:00
|
|
|
class EmailBackend(ConsoleEmailBackend):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self._fname = None
|
|
|
|
if 'file_path' in kwargs:
|
|
|
|
self.file_path = kwargs.pop('file_path')
|
|
|
|
else:
|
2013-10-25 01:30:03 +08:00
|
|
|
self.file_path = getattr(settings, 'EMAIL_FILE_PATH', None)
|
2009-11-03 20:53:26 +08:00
|
|
|
# Make sure self.file_path is a string.
|
2012-07-20 20:22:00 +08:00
|
|
|
if not isinstance(self.file_path, six.string_types):
|
2009-11-03 20:53:26 +08:00
|
|
|
raise ImproperlyConfigured('Path for saving emails is invalid: %r' % self.file_path)
|
|
|
|
self.file_path = os.path.abspath(self.file_path)
|
|
|
|
# Make sure that self.file_path is an directory if it exists.
|
|
|
|
if os.path.exists(self.file_path) and not os.path.isdir(self.file_path):
|
2014-09-04 20:15:09 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'Path for saving email messages exists, but is not a directory: %s' % self.file_path
|
|
|
|
)
|
2009-11-03 20:53:26 +08:00
|
|
|
# Try to create it, if it not exists.
|
|
|
|
elif not os.path.exists(self.file_path):
|
|
|
|
try:
|
|
|
|
os.makedirs(self.file_path)
|
2012-04-29 00:09:37 +08:00
|
|
|
except OSError as err:
|
2014-09-04 20:15:09 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'Could not create directory for saving email messages: %s (%s)' % (self.file_path, err)
|
|
|
|
)
|
2009-11-03 20:53:26 +08:00
|
|
|
# Make sure that self.file_path is writable.
|
|
|
|
if not os.access(self.file_path, os.W_OK):
|
|
|
|
raise ImproperlyConfigured('Could not write to directory: %s' % self.file_path)
|
|
|
|
# Finally, call super().
|
|
|
|
# Since we're using the console-based backend as a base,
|
|
|
|
# force the stream to be None, so we don't default to stdout
|
|
|
|
kwargs['stream'] = None
|
|
|
|
super(EmailBackend, self).__init__(*args, **kwargs)
|
|
|
|
|
2013-12-31 06:45:43 +08:00
|
|
|
def write_message(self, message):
|
|
|
|
self.stream.write(message.message().as_bytes() + b'\n')
|
|
|
|
self.stream.write(b'-' * 79)
|
|
|
|
self.stream.write(b'\n')
|
|
|
|
|
2009-11-03 20:53:26 +08:00
|
|
|
def _get_filename(self):
|
|
|
|
"""Return a unique file name."""
|
|
|
|
if self._fname is None:
|
|
|
|
timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
|
|
|
|
fname = "%s-%s.log" % (timestamp, abs(id(self)))
|
|
|
|
self._fname = os.path.join(self.file_path, fname)
|
|
|
|
return self._fname
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
if self.stream is None:
|
2013-12-31 06:45:43 +08:00
|
|
|
self.stream = open(self._get_filename(), 'ab')
|
2009-11-03 20:53:26 +08:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
try:
|
|
|
|
if self.stream is not None:
|
|
|
|
self.stream.close()
|
|
|
|
finally:
|
|
|
|
self.stream = None
|