Allow global_merge_vars to be merged in with the per message dict, with keys in the latter taking precedent.

Update the docs accordingly.
This commit is contained in:
William Hector
2015-07-14 05:57:12 +01:00
parent 883b23362c
commit 7179734a08
4 changed files with 28 additions and 4 deletions

View File

@@ -17,3 +17,4 @@ Sameer Al-Sakran
Kyle Gibson Kyle Gibson
Wes Winham Wes Winham
nikolay-saskovets nikolay-saskovets
William Hector

View File

@@ -285,12 +285,17 @@ class DjrillBackend(BaseEmailBackend):
# Allow simple python dicts in place of Mandrill # Allow simple python dicts in place of Mandrill
# [{name:name, value:value},...] arrays... # [{name:name, value:value},...] arrays...
# Allow merge of global and per message global_merge_var, the former taking precedent
global_merge_vars = {}
if 'global_merge_vars' in self.global_settings: if 'global_merge_vars' in self.global_settings:
msg_dict['global_merge_vars'] = self._expand_merge_vars( global_merge_vars.update(self.global_settings['global_merge_vars'])
self.global_settings['global_merge_vars'])
if hasattr(message, 'global_merge_vars'): if hasattr(message, 'global_merge_vars'):
global_merge_vars.update(message.global_merge_vars)
if global_merge_vars:
msg_dict['global_merge_vars'] = \ msg_dict['global_merge_vars'] = \
self._expand_merge_vars(message.global_merge_vars) self._expand_merge_vars(global_merge_vars)
if hasattr(message, 'merge_vars'): if hasattr(message, 'merge_vars'):
# For testing reproducibility, we sort the recipients # For testing reproducibility, we sort the recipients
msg_dict['merge_vars'] = [ msg_dict['merge_vars'] = [

View File

@@ -650,11 +650,21 @@ class DjrillMandrillGlobalFeatureTests(DjrillBackendMockAPITestCase):
self.assertEqual(data['message']['url_strip_qs'], False) self.assertEqual(data['message']['url_strip_qs'], False)
def test_global_merge(self): def test_global_merge(self):
# Test that global settings merge in
self.message.global_merge_vars = {'GREETING': "Hello"} self.message.global_merge_vars = {'GREETING': "Hello"}
self.message.send() self.message.send()
data = self.get_api_call_data() data = self.get_api_call_data()
self.assertEqual(data['message']['global_merge_vars'], self.assertEqual(data['message']['global_merge_vars'],
[{'name': "GREETING", 'content': "Hello"}]) [{'name': "GREETING", 'content': "Hello"},
{'name': 'TEST', 'content': 'djrill'}])
def test_global_merge_overwrite(self):
# Test that global merge settings are overwritten
self.message.global_merge_vars = {'TEST': "Hello"}
self.message.send()
data = self.get_api_call_data()
self.assertEqual(data['message']['global_merge_vars'],
[{'name': 'TEST', 'content': 'Hello'}])
@override_settings(EMAIL_BACKEND="djrill.mail.backends.djrill.DjrillBackend") @override_settings(EMAIL_BACKEND="djrill.mail.backends.djrill.DjrillBackend")

View File

@@ -118,6 +118,10 @@ using :setting:`MANDRILL_SETTINGS`. For Example::
'track_opens': True, 'track_opens': True,
} }
.. note::
``merge_vars`` and ``recipient_metadata`` cannot be set globally. ``global_merge_vars`` is merged
(see :attribute:`global_merge_vars`)
.. These attributes are in the same order as they appear in the Mandrill API docs... .. These attributes are in the same order as they appear in the Mandrill API docs...
.. attribute:: important .. attribute:: important
@@ -211,6 +215,10 @@ using :setting:`MANDRILL_SETTINGS`. For Example::
Merge data must be strings or other JSON-serializable types. Merge data must be strings or other JSON-serializable types.
(See :ref:`formatting-merge-data` for details.) (See :ref:`formatting-merge-data` for details.)
.. note::
If using :setting:`MANDRILL_SETTINGS` then the message ``dict`` will be merged and overwrite any duplicates.
.. attribute:: merge_vars .. attribute:: merge_vars
``dict``: per-recipient merge variables (most useful with :ref:`mandrill-templates`). The keys ``dict``: per-recipient merge variables (most useful with :ref:`mandrill-templates`). The keys