Projet

Général

Profil

0001-csv_import-ignore-empty-role-values-50158.patch

Valentin Deniaud, 25 février 2021 09:11

Télécharger (2,36 ko)

Voir les différences:

Subject: [PATCH] csv_import: ignore empty role values (#50158)

 src/authentic2/csv_import.py | 6 ++++--
 tests/test_csv_import.py     | 6 ++++++
 2 files changed, 10 insertions(+), 2 deletions(-)
src/authentic2/csv_import.py
589 589
            if header.name == SOURCE_ID:
590 590
                unique_key = (SOURCE_ID, row[SOURCE_NAME].value, cell.value)
591 591
            elif header.key or header.globally_unique or header.unique:
592
                if not cell.value:
592
                if not cell.value.strip():
593 593
                    # empty values are not checked
594 594
                    continue
595 595
                unique_key = (header.name, cell.value)
......
611 611
        for cell in row:
612 612
            if (not cell.header.globally_unique and not cell.header.unique) or (user and not cell.header.update):
613 613
                continue
614
            if not cell.value:
614
            if not cell.value.strip():
615 615
                continue
616 616
            qs = ou_users
617 617
            if cell.header.globally_unique:
......
744 744
        return success
745 745

  
746 746
    def add_role(self, cell, user, do_clear=False):
747
        if not cell.value.strip():
748
            return True
747 749
        try:
748 750
            if cell.header.name == ROLE_NAME:
749 751
                role = Role.objects.get(name=cell.value, ou=self.ou)
tests/test_csv_import.py
496 496
    assert importer.has_errors
497 497
    assert importer.errors[0].code == 'invalid-role-column'
498 498

  
499
    # empty role name doesn't raise error
500
    content_name_error = '\n'.join((base_header + '_role_name', base_user + ''))
501
    importer = user_csv_importer_factory(content_name_error)
502
    assert importer.run()
503
    assert not importer.has_errors
504

  
499 505

  
500 506
def test_csv_registration_options(profile, user_csv_importer_factory):
501 507
    content = '''email key,first_name,last_name,@registration
502
-