add subaccount support to djrill

This commit is contained in:
Sameer Al-Sakran
2014-04-11 13:15:09 -07:00
parent 956e45bfcb
commit cd8c2b8760
3 changed files with 45 additions and 0 deletions

View File

@@ -50,6 +50,8 @@ class DjrillBackend(BaseEmailBackend):
self.api_key = getattr(settings, "MANDRILL_API_KEY", None)
self.api_url = MANDRILL_API_URL
self.subaccount = getattr(settings, "MANDRILL_SUB_ACCOUNT", None)
if not self.api_key:
raise ImproperlyConfigured("You have not set your mandrill api key "
"in the settings.py file.")
@@ -187,6 +189,10 @@ class DjrillBackend(BaseEmailBackend):
'tags', 'preserve_recipients', 'view_content_link', 'subaccount',
'google_analytics_domains', 'google_analytics_campaign',
'metadata']
if self.subaccount:
msg_dict['subaccount'] = self.subaccount
for attr in mandrill_attrs:
if hasattr(message, attr):
msg_dict[attr] = getattr(message, attr)

View File

@@ -3,3 +3,4 @@ from djrill.tests.test_legacy import *
from djrill.tests.test_mandrill_send import *
from djrill.tests.test_mandrill_send_template import *
from djrill.tests.test_mandrill_webhook import *
from djrill.tests.test_mandrill_subaccounts import *

View File

@@ -0,0 +1,38 @@
from django.core import mail
from djrill.tests.mock_backend import DjrillBackendMockAPITestCase
from django.conf import settings
class DjrillMandrillSubaccountTests(DjrillBackendMockAPITestCase):
"""Test Djrill backend support for Mandrill subaccounts"""
def test_send_basic(self):
mail.send_mail('Subject here', 'Here is the message.',
'from@example.com', ['to@example.com'], fail_silently=False)
self.assert_mandrill_called("/messages/send.json")
data = self.get_api_call_data()
self.assertEqual(data['message']['subject'], "Subject here")
self.assertEqual(data['message']['text'], "Here is the message.")
self.assertFalse('from_name' in data['message'])
self.assertEqual(data['message']['from_email'], "from@example.com")
self.assertEqual(len(data['message']['to']), 1)
self.assertEqual(data['message']['to'][0]['email'], "to@example.com")
def test_send_from_subaccount(self):
settings.MANDRILL_SUB_ACCOUNT = "test_subaccount"
mail.send_mail('Subject here', 'Here is the message.',
'from@example.com', ['to@example.com'], fail_silently=False)
self.assert_mandrill_called("/messages/send.json")
data = self.get_api_call_data()
self.assertEqual(data['message']['subject'], "Subject here")
self.assertEqual(data['message']['text'], "Here is the message.")
self.assertFalse('from_name' in data['message'])
self.assertEqual(data['message']['from_email'], "from@example.com")
self.assertEqual(len(data['message']['to']), 1)
self.assertEqual(data['message']['to'][0]['email'], "to@example.com")
self.assertEqual(data['message']['subaccount'], settings.MANDRILL_SUB_ACCOUNT)