Commits

Shu Zong Chen  committed 5b05fe6

Extract from original repo, set up as standalone

  • Participants

Comments (0)

Files changed (11)

+# use glob syntax.
+syntax: glob
+
+*.swp
+*.pyc
+build/*
+dist/*
+Copyright (C) 2012 by Shu Zong Chen <shu.chen@freelancedreams.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.

File django_mandrill/__init__.py

Empty file added.

File django_mandrill/mail/__init__.py

+from mandrillmail import MandrillMail, MandrillTemplateMail

File django_mandrill/mail/backends/__init__.py

Empty file added.

File django_mandrill/mail/backends/mandrillbackend.py

+from django.core.mail.backends.base import BaseEmailBackend
+from django.core.exceptions import ImproperlyConfigured
+from django.conf import settings
+
+import threading
+import mandrill
+
+class EmailBackend(BaseEmailBackend):
+	def __init__(self, *args, **kwargs):
+		api_key = getattr(settings, "MANDRILL_API_KEY", None)
+		if api_key is None:
+			raise ImproperlyConfigured("missing MANDRILL_API_KEY")
+
+		self.mandrill = mandrill.Mandrill(api_key)
+		self._lock = threading.RLock()
+		super(EmailBackend, self).__init__(*args, **kwargs)
+	
+	def send_messages(self, email_messages):
+		if not email_messages:
+			return
+
+		num_sent = 0
+		with self._lock:
+			for message in email_messages:
+				sent = self._send(message)
+				if sent:
+					num_sent += 1
+
+		return num_sent
+
+	def _send(self, message):
+		try:
+			subtype = getattr(message, 'alternative_subtype', None)
+			f = {
+				'mandrill_message': self._send_message,
+				'mandrill_template': self._send_template,
+			}.get(subtype, self._send_raw)
+			f(message)
+		except:
+			if not self.fail_silently:
+				raise
+			return False
+		return True
+
+	def _send_raw(self, message):
+		if not message.recipients():
+			return False
+		self.mandrill.messages.send_raw(message.message().as_string())
+		return True
+
+	def _send_template(self, message):
+		self.mandrill.messages.send_template(
+			message.mandrill_template_name,
+			message.mandrill_template_content,
+			message.mandrill_args,
+		)
+		return True
+
+	def _send_message(self, message):
+		self.mandrill.messages.send(message.mandrill_args)
+		return True

File django_mandrill/mail/mandrillmail.py

+from django.core.mail.message import EmailMessage
+
+class MandrillMail(EmailMessage):
+	alternative_subtype = "mandrill_message"
+
+	def __init__(self, args):
+		self.mandrill_args = args
+		self.connection = None
+	
+	def send(self, fail_silently=False):
+		return self.get_connection(fail_silently).send_messages([self])
+
+class MandrillTemplateMail(EmailMessage):
+	alternative_subtype = "mandrill_template"
+
+	def __init__(self, template, content, args):
+		self.mandrill_template_name = template
+		self.mandrill_template_content = content
+		self.mandrill_args = args
+		self.connection = None
+
+	def send(self, fail_silently=False):
+		return self.get_connection(fail_silently).send_messages([self])

File django_mandrill/models.py

+from django.db import models
+
+# Create your models here.

File django_mandrill/tests.py

+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.assertEqual(1 + 1, 2)

File django_mandrill/views.py

+# Create your views here.
+#!/usr/bin/env python
+
+from setuptools import setup, find_packages
+
+setup(
+	name = 'django-mandrill',
+	version = '0.1.0',
+	#packages = ['django-mandrill',],
+	packages = find_packages(),
+	include_package_data = True,
+	author = 'Shu Zong Chen',
+	author_email = 'shu.chen@freelancedreams.com',
+	description = 'Mandrill integration for Django',
+	long_description = \
+"""
+Send emails using Mandrill as a backend.  Support for
+Django's mail objects and Mandrill Templates.
+""",
+	license = "MIT License",
+	keywords = "django mandrill email email-backend",
+	classifiers = [
+		'Development Status :: 5 - Production/Stable',
+    'Environment :: Web Environment',
+    'Framework :: Django',
+    'Intended Audience :: Developers',
+    'License :: OSI Approved :: MIT License',
+    'Operating System :: OS Independent',
+    'Programming Language :: Python',
+    'Topic :: Software Development :: Libraries :: Application Frameworks',
+    'Topic :: Software Development :: Libraries :: Python Modules',
+	],
+	platforms = ['any'],
+	url = 'https://bitbucket.org/sirpengi/django-mandrill',
+	download_url = 'https://bitbucket.org/sirpengi/django-mandrill/downloads',
+)
+