From 1bd297374424e8864e3ccd9ffd01e14c076b4185 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 23 Feb 2021 17:24:40 +0100 Subject: [PATCH] manager: show missing role recap in csv import (#50166) --- src/authentic2/csv_import.py | 3 +++ .../authentic2/manager/user_import_report.html | 3 +++ tests/test_user_manager.py | 11 +++++++++++ 3 files changed, 17 insertions(+) diff --git a/src/authentic2/csv_import.py b/src/authentic2/csv_import.py index d97e568d..5eae6970 100644 --- a/src/authentic2/csv_import.py +++ b/src/authentic2/csv_import.py @@ -373,6 +373,7 @@ class UserCsvImporter(object): updated = 0 created = 0 rows_with_errors = 0 + missing_roles = None def add_error(self, line_error): if not hasattr(line_error, 'line'): @@ -382,6 +383,7 @@ class UserCsvImporter(object): def run(self, fd_or_str, encoding, ou=None, simulate=False): self.ou = ou or get_default_ou() self.errors = [] + self.missing_roles = set() self.csv_importer = CsvImporter() self.max_user_id = User.objects.aggregate(max=models.Max('id'))['max'] or -1 @@ -757,6 +759,7 @@ class UserCsvImporter(object): elif cell.header.name == ROLE_SLUG: role = Role.objects.get(slug=cell.value, ou=self.ou) except Role.DoesNotExist: + self.missing_roles.add(cell.value) cell.errors.append( Error('role-not-found', _('Role "%s" does not exist') % cell.value)) diff --git a/src/authentic2/manager/templates/authentic2/manager/user_import_report.html b/src/authentic2/manager/templates/authentic2/manager/user_import_report.html index 9f2f0656..4adf7a9b 100644 --- a/src/authentic2/manager/templates/authentic2/manager/user_import_report.html +++ b/src/authentic2/manager/templates/authentic2/manager/user_import_report.html @@ -90,6 +90,9 @@
  • {% blocktrans count created=importer.created %}{{ created }} user created{% plural %}{{ created }} users created{% endblocktrans %}
  • {% blocktrans count updated=importer.updated %}{{ updated }} user updated{% plural %}{{ updated }} users updated{% endblocktrans %}
  • {% blocktrans count error_rows=importer.rows_with_errors %}{{ error_rows }} row has error{% plural %}{{ error_rows }} rows have errors{% endblocktrans %}
  • + {% if importer.missing_roles %} +
  • {% trans "The following roles were missing:" %} {{ importer.missing_roles|join:", " }}
  • + {% endif %}
  • {% blocktrans with duration=report.duration %}import took {{ duration }}{% endblocktrans %}
  • {% trans "Details" %}

    diff --git a/tests/test_user_manager.py b/tests/test_user_manager.py index 30c0ce97..f87e7a31 100644 --- a/tests/test_user_manager.py +++ b/tests/test_user_manager.py @@ -681,6 +681,17 @@ Elliott,3''' assert 'matches too many user' in response.pyquery('tr.row-errors').text() +def test_user_import_missing_roles_recap(transactional_db, app, admin): + content = '''first_name key,last_name,_role_name +Elliott,Doe,test1 +Jane,Doe,test1 +John,Doe,test2''' + login(app, admin, '/manage/users/') + response = import_csv(content, app) + + assert 'The following roles were missing: test1, test2' in response.text + + def test_manager_create_user_next(superuser_or_admin, app, ou1): login(app, superuser_or_admin, '/manage/') -- 2.20.1