From f6c16b94d89f2c752657ee93936d4edc6b29128d Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 26 Jun 2020 13:01:51 +0200 Subject: [PATCH] misc: inject TEMPLATE_VARS in mail template context (#43469) --- src/authentic2/utils/__init__.py | 4 +++- tests/templates/template_vars_body.html | 1 + tests/templates/template_vars_body.txt | 1 + tests/templates/template_vars_subject.txt | 1 + tests/test_utils.py | 15 +++++++++++++++ 5 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/templates/template_vars_body.html create mode 100644 tests/templates/template_vars_body.txt create mode 100644 tests/templates/template_vars_subject.txt diff --git a/src/authentic2/utils/__init__.py b/src/authentic2/utils/__init__.py index 9a5f0236..5dcd92eb 100644 --- a/src/authentic2/utils/__init__.py +++ b/src/authentic2/utils/__init__.py @@ -634,7 +634,9 @@ def send_templated_mail(user_or_email, template_names, context=None, with_html=T if not request: request = middleware.StoreRequestMiddleware().get_request() - ctx = context or {} + ctx = copy.copy(app_settings.TEMPLATE_VARS) + if context: + ctx.update(context) subject_template_names = [template_name + '_subject.txt' for template_name in template_names] subject_template_names += legacy_subject_templates or [] diff --git a/tests/templates/template_vars_body.html b/tests/templates/template_vars_body.html new file mode 100644 index 00000000..10f68efa --- /dev/null +++ b/tests/templates/template_vars_body.html @@ -0,0 +1 @@ +{{ template_vars_body_html }} diff --git a/tests/templates/template_vars_body.txt b/tests/templates/template_vars_body.txt new file mode 100644 index 00000000..11934a48 --- /dev/null +++ b/tests/templates/template_vars_body.txt @@ -0,0 +1 @@ +{{ template_vars_body_txt }} diff --git a/tests/templates/template_vars_subject.txt b/tests/templates/template_vars_subject.txt new file mode 100644 index 00000000..5928667a --- /dev/null +++ b/tests/templates/template_vars_subject.txt @@ -0,0 +1 @@ +{{ template_vars_subject_txt }} diff --git a/tests/test_utils.py b/tests/test_utils.py index f9626bcd..0dd78930 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -161,6 +161,21 @@ def test_send_templated_mail_template_selection(simple_user): assert sent_mail.body == specific_template_ou +def test_send_templated_mail_template_vars(settings, simple_user): + settings.TEMPLATE_VARS = { + 'template_vars_subject_txt': 'here is the subject', + 'template_vars_body_txt': 'here is the text body', + 'template_vars_body_html': 'here is the html body', + } + send_templated_mail(simple_user, ['template_vars']) + + assert len(mail.outbox) == 1 + sent_mail = mail.outbox.pop() + assert sent_mail.subject == 'here is the subject' + assert sent_mail.body == 'here is the text body\n' + assert sent_mail.alternatives == [('here is the html body\n', 'text/html')] + + def test_lazy_join(): a = 'a' -- 2.26.2