Projet

Général

Profil

0002-csv_import-allow-setting-user-password-and-sending-m.patch

Valentin Deniaud, 25 novembre 2019 16:14

Télécharger (4,63 ko)

Voir les différences:

Subject: [PATCH 2/2] csv_import: allow setting user password and sending mail
 (#35774)

 src/authentic2/csv_import.py | 25 ++++++++++++++++++++++++-
 tests/test_csv_import.py     | 27 +++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 1 deletion(-)
src/authentic2/csv_import.py
37 37
from authentic2.custom_user.models import User
38 38
from authentic2.forms.profile import modelform_factory, BaseUserForm
39 39
from authentic2.models import Attribute, AttributeValue, UserExternalId
40
from authentic2.utils import send_password_reset_mail, generate_password
40 41

  
41 42
Role = get_role_model()
42 43

  
......
201 202
SOURCE_COLUMNS = set([SOURCE_NAME, SOURCE_ID])
202 203
ROLE_NAME = '_role_name'
203 204
ROLE_SLUG = '_role_slug'
204
SPECIAL_COLUMNS = SOURCE_COLUMNS | {ROLE_NAME, ROLE_SLUG}
205
REGISTRATION = '@registration'
206
REGISTRATION_RESET_EMAIL = 'send-email'
207
SPECIAL_COLUMNS = SOURCE_COLUMNS | {ROLE_NAME, ROLE_SLUG, REGISTRATION}
205 208

  
206 209

  
207 210
class ImportUserForm(BaseUserForm):
......
211 214
    locals()[ROLE_SLUG] = forms.CharField(
212 215
        label=_('Role slug'),
213 216
        required=False)
217
    choices = [
218
        (REGISTRATION_RESET_EMAIL, _('Email user so they can set a password')),
219
    ]
220
    locals()[REGISTRATION] = forms.ChoiceField(
221
        choices,
222
        label=_('Registration option'),
223
        required=False)
214 224

  
215 225
    def clean(self):
216 226
        super(BaseUserForm, self).clean()
......
597 607

  
598 608
        if not user:
599 609
            user = User(ou=self.ou)
610
            user.set_password(generate_password())
600 611

  
601 612
        for cell in row.cells:
602 613
            if not cell.header.field:
......
643 654
                continue
644 655
            if cell.header.name in {ROLE_NAME, ROLE_SLUG}:
645 656
                success &= self.add_role(cell, user, do_clear=True)
657
            elif cell.header.name == REGISTRATION and row.action == 'create':
658
                success &= self.registration_option(cell, user)
646 659

  
647 660
        setattr(self, row.action + 'd', getattr(self, row.action + 'd') + 1)
648 661
        return success
......
668 681
            user.roles.add(role)
669 682
        cell.action = 'updated'
670 683
        return True
684

  
685
    def registration_option(self, cell, user):
686
        if cell.value == REGISTRATION_RESET_EMAIL:
687
            send_password_reset_mail(
688
                user,
689
                template_names=['authentic2/manager/user_create_registration_email',
690
                                'authentic2/password_reset'],
691
                next_url='/accounts/',
692
                context={'user': user})
693
        return True
tests/test_csv_import.py
21 21

  
22 22
import io
23 23

  
24
from django.core import mail
25

  
24 26
from django_rbac.utils import get_role_model
25 27

  
26 28
from authentic2.custom_user.models import User
......
170 172
    assert thomas.last_name == 'Noël'
171 173
    assert thomas.attributes.last_name == 'Noël'
172 174
    assert thomas.attributes.phone == '1234'
175
    assert thomas.password
173 176

  
174 177
    fpeters = User.objects.get(email='fpeters@entrouvert.com')
175 178
    assert fpeters.ou == get_default_ou()
......
482 485
    assert not importer.run()
483 486
    assert importer.has_errors
484 487
    assert importer.errors[0].code == 'invalid-role-column'
488

  
489

  
490
def test_csv_registration_options(profile, user_csv_importer_factory):
491
    content = '''email key,first_name,last_name,@registration
492
tnoel@entrouvert.com,Thomas,Noël,'''
493

  
494
    assert not mail.outbox
495
    importer = user_csv_importer_factory(content + 'send-email')
496
    assert importer.run()
497
    thomas = User.objects.get(email='tnoel@entrouvert.com')
498
    assert len(mail.outbox) == 1
499

  
500
    password = thomas.password
501
    del mail.outbox[0]
502
    importer = user_csv_importer_factory(content + 'send-email')
503
    assert importer.run()
504
    thomas.refresh_from_db()
505
    assert thomas.password == password
506
    assert not mail.outbox
507

  
508
    importer = user_csv_importer_factory(content + 'invalid-option')
509
    assert importer.run()
510
    assert importer.has_errors
511
    assert importer.rows[0].cells[-1].errors[0].code == 'data-error'
485
-