Projet

Général

Profil

0001-utils-handle-per-ou-template-in-send_templated_mail-.patch

Valentin Deniaud, 07 novembre 2019 14:52

Télécharger (6,97 ko)

Voir les différences:

Subject: [PATCH 1/2] utils: handle per ou template in send_templated_mail
 (#35774)

 src/authentic2/utils/__init__.py | 23 ++++++++------
 tests/test_utils.py              | 51 +++++++++++++++++++++++++++++++-
 2 files changed, 64 insertions(+), 10 deletions(-)
src/authentic2/utils/__init__.py
613 613

  
614 614
def send_templated_mail(user_or_email, template_names, context=None, with_html=True,
615 615
                        from_email=None, request=None, legacy_subject_templates=None,
616
                        legacy_body_templates=None, legacy_html_body_templates=None, **kwargs):
616
                        legacy_body_templates=None, legacy_html_body_templates=None,
617
                        per_ou_templates=False, **kwargs):
617 618
    '''Send mail to an user by using templates:
618 619
       - <template_name>_subject.txt for the subject
619 620
       - <template_name>_body.txt for the plain text body
......
622 623
    from .. import middleware
623 624
    if isinstance(template_names, six.string_types):
624 625
        template_names = [template_names]
626
    if per_ou_templates and getattr(user_or_email, 'ou', None):
627
        new_template_names = []
628
        for template in template_names:
629
            new_template_names.append('_'.join((template, user_or_email.ou.slug)))
630
            new_template_names.append(template)
631
        template_names = new_template_names
625 632
    if hasattr(user_or_email, 'email'):
626 633
        user_or_email = user_or_email.email
627 634
    if not request:
......
645 652
            html_body = render_to_string(html_body_template_names, ctx, request=request)
646 653
        except TemplateDoesNotExist:
647 654
            html_body = None
655

  
648 656
    send_mail(subject, body, from_email or settings.DEFAULT_FROM_EMAIL, [user_or_email],
649 657
              html_message=html_body, **kwargs)
650 658

  
......
751 759
        'deletion_url': deletion_url}
752 760
    template_names = [
753 761
        'authentic2/account_deletion_code']
754
    if user.ou:
755
        template_names.insert(0, 'authentic2/account_deletion_code_%s' % user.ou.slug)
756
    send_templated_mail(user.email, template_names, context, request=request)
762
    send_templated_mail(user, template_names, context, request=request, per_ou_templates=True)
757 763
    logger.info(u'account deletion code sent to %s', user.email)
758 764

  
759 765

  
......
769 775
        'site': request.get_host()}
770 776
    template_names = [
771 777
        'authentic2/account_delete_notification']
772
    if user.ou:
773
        template_names.insert(0, 'authentic2/account_delete_notification_%s' % user.ou.slug)
774
    send_templated_mail(user.email, template_names, context, request=request)
778
    send_templated_mail(user, template_names, context, request=request, per_ou_templates=True)
775 779
    logger.info(u'account deletion mail sent to %s', user.email)
776 780

  
777 781

  
......
826 830
                                                       set_random_password=set_random_password,
827 831
                                                       sign_next_url=sign_next_url)
828 832

  
829
    send_templated_mail(user.email, template_names, ctx, request=request,
833
    send_templated_mail(user, template_names, ctx, request=request,
830 834
                        legacy_subject_templates=legacy_subject_templates,
831
                        legacy_body_templates=legacy_body_templates, **kwargs)
835
                        legacy_body_templates=legacy_body_templates,
836
                        per_ou_templates=True, **kwargs)
832 837
    logger.info(u'password reset request for user %s, email sent to %s '
833 838
                'with token %s', user, user.email, token[:9])
834 839

  
tests/test_utils.py
17 17

  
18 18
from django.contrib.auth.middleware import AuthenticationMiddleware
19 19
from django.contrib.sessions.middleware import SessionMiddleware
20
from django.core import mail
20 21

  
21 22
from authentic2.utils import (good_next_url, same_origin, select_next_url,
22 23
                              user_can_change_password, login,
23
                              get_authentication_events, authenticate)
24
                              get_authentication_events, authenticate,
25
                              send_templated_mail)
24 26

  
25 27

  
26 28
def test_good_next_url(db, rf, settings):
......
116 118
    request.COOKIES['preferrence'] = '1 2 3 4 5'
117 119
    prepend_remember_cookie(request, response, 'preferrence', 7)
118 120
    assert response.cookies['preferrence'].value == '7 1 2 3 4'
121

  
122

  
123
def test_send_templated_mail_template_selection(db, tmp_path, settings, simple_user):
124
    settings.TEMPLATES[0]['DIRS'].append(str(tmp_path))
125
    templates = {}
126
    settings.TEMPLATES[0]['APP_DIRS'] = False
127
    settings.TEMPLATES[0]['OPTIONS']['loaders'] = [
128
            ('django.template.loaders.locmem.Loader', templates)
129
    ]
130
    default_template = 'default_template'
131
    specific_template = 'specific_template'
132
    default_template_ou = '_'.join((default_template, simple_user.ou.slug))
133
    specific_template_ou = '_'.join((specific_template, simple_user.ou.slug))
134
    template_names = [specific_template, default_template]
135

  
136
    def add_template(template):
137
        extensions = ['_body.html', '_body.txt', '_subject.txt']
138
        for ext in extensions:
139
            templates[template + ext] = template
140

  
141
    add_template(default_template)
142
    send_templated_mail(simple_user, template_names, per_ou_templates=True)
143
    assert len(mail.outbox) == 1
144
    sent_mail = mail.outbox.pop()
145
    assert sent_mail.subject == default_template
146
    assert sent_mail.body == default_template
147

  
148
    add_template(default_template_ou)
149
    send_templated_mail(simple_user, template_names, per_ou_templates=True)
150
    assert len(mail.outbox) == 1
151
    sent_mail = mail.outbox.pop()
152
    assert sent_mail.subject == default_template_ou
153
    assert sent_mail.body == default_template_ou
154

  
155
    add_template(specific_template)
156
    send_templated_mail(simple_user, template_names, per_ou_templates=True)
157
    assert len(mail.outbox) == 1
158
    sent_mail = mail.outbox.pop()
159
    assert sent_mail.subject == specific_template
160
    assert sent_mail.body == specific_template
161

  
162
    add_template(specific_template_ou)
163
    send_templated_mail(simple_user, template_names, per_ou_templates=True)
164
    assert len(mail.outbox) == 1
165
    sent_mail = mail.outbox.pop()
166
    assert sent_mail.subject == specific_template_ou
167
    assert sent_mail.body == specific_template_ou
119
-