Skip to content
Browse files

file based backend implementation

  • Loading branch information...
1 parent 5bad8b9 commit 1dc33680f78d87904a1532f110526db5122345a4 Vladimir Gorej committed Mar 22, 2012
Showing with 68 additions and 0 deletions.
  1. +6 −0 docs/backend_filebased.rst
  2. +62 −0 sendsms/backends/filebased.py
View
6 docs/backend_filebased.rst
@@ -0,0 +1,6 @@
+Console SmsBackend
+==================
+
+.. automodule:: sendsms.backends.filebased
+ :members:
+ :undoc-members:
View
62 sendsms/backends/filebased.py
@@ -0,0 +1,62 @@
+"""
+SMS backend that writes messages to file instead of sending them.
+
+This is a total ripoff of django.core.mail.backends.filebased
+"""
+
+import datetime
+import os
+
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+from sendsms.backends.console import SmsBackend as ConsoleSmsBackend
+
+class SmsBackend(ConsoleSmsBackend):
+ def __init__(self, *args, **kwargs):
+ self._fname = None
+ if 'file_path' in kwargs:
+ self.file_path = kwargs.pop('file_path')
+ else:
+ self.file_path = getattr(settings, 'SMS_FILE_PATH', None)
+ # Make sure self.file_path is a string.
+ if not isinstance(self.file_path, basestring):
+ raise ImproperlyConfigured('Path for saving SMS 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):
+ raise ImproperlyConfigured('Path for saving SMS messages exists, but is not a directory: %s' % self.file_path)
+ # Try to create it, if it not exists.
+ elif not os.path.exists(self.file_path):
+ try:
+ os.makedirs(self.file_path)
+ except OSError, err:
+ raise ImproperlyConfigured('Could not create directory for saving SMS messages: %s (%s)' % (self.file_path, err))
+ # 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(SmsBackend, self).__init__(*args, **kwargs)
+
+ 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:
+ self.stream = open(self._get_filename(), 'a')
+ return True
+ return False
+
+ def close(self):
+ try:
+ if self.stream is not None:
+ self.stream.close()
+ finally:
+ self.stream = None

0 comments on commit 1dc3368

Please sign in to comment.
Something went wrong with that request. Please try again.