Skip to content
This repository has been archived by the owner on Jul 20, 2020. It is now read-only.

Commit

Permalink
Allow global_merge_vars to be merged in with the per message dict, wi…
Browse files Browse the repository at this point in the history
…th keys in the latter taking precedent.

Update the docs accordingly.
  • Loading branch information
Wrhector committed Jul 14, 2015
1 parent 883b233 commit 7179734
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Expand Up @@ -17,3 +17,4 @@ Sameer Al-Sakran
Kyle Gibson
Wes Winham
nikolay-saskovets
William Hector
11 changes: 8 additions & 3 deletions djrill/mail/backends/djrill.py
Expand Up @@ -285,12 +285,17 @@ def _add_mandrill_options(self, message, msg_dict):

# Allow simple python dicts in place of Mandrill
# [{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:
msg_dict['global_merge_vars'] = self._expand_merge_vars(
self.global_settings['global_merge_vars'])
global_merge_vars.update(self.global_settings['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'] = \
self._expand_merge_vars(message.global_merge_vars)
self._expand_merge_vars(global_merge_vars)

if hasattr(message, 'merge_vars'):
# For testing reproducibility, we sort the recipients
msg_dict['merge_vars'] = [
Expand Down
12 changes: 11 additions & 1 deletion djrill/tests/test_mandrill_send.py
Expand Up @@ -650,11 +650,21 @@ def test_global_options_override_tracking(self):
self.assertEqual(data['message']['url_strip_qs'], False)

def test_global_merge(self):
# Test that global settings merge in
self.message.global_merge_vars = {'GREETING': "Hello"}
self.message.send()
data = self.get_api_call_data()
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")
Expand Down
8 changes: 8 additions & 0 deletions docs/usage/sending_mail.rst
Expand Up @@ -118,6 +118,10 @@ using :setting:`MANDRILL_SETTINGS`. For Example::
'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...
.. attribute:: important
Expand Down Expand Up @@ -211,6 +215,10 @@ using :setting:`MANDRILL_SETTINGS`. For Example::
Merge data must be strings or other JSON-serializable types.
(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

``dict``: per-recipient merge variables (most useful with :ref:`mandrill-templates`). The keys
Expand Down

0 comments on commit 7179734

Please sign in to comment.