From 0f802b0d22bd33177f09209f95ee1c947b402eea Mon Sep 17 00:00:00 2001 From: Nikhil anand <77477551+nick2432@users.noreply.github.com> Date: Sat, 24 Feb 2024 01:35:40 +0530 Subject: [PATCH] Properly handle case insensitive matches for existing usernames in bulkimport (#11779) --- .../management/commands/bulkimportusers.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/kolibri/core/auth/management/commands/bulkimportusers.py b/kolibri/core/auth/management/commands/bulkimportusers.py index 4a39845373a..7e945d54dca 100644 --- a/kolibri/core/auth/management/commands/bulkimportusers.py +++ b/kolibri/core/auth/management/commands/bulkimportusers.py @@ -534,7 +534,7 @@ def compare_fields(self, user_obj, values): setattr(user_obj, field, values[field]) return changed - def build_users_objects(self, users): + def build_users_objects(self, users): # noqa C901 new_users = [] update_users = [] keeping_users = [] @@ -564,7 +564,7 @@ def build_users_objects(self, users): if user_obj.username != user: # check for duplicated username in the facility existing_user = FacilityUser.objects.get( - username=user, facility=self.default_facility + username__iexact=user, facility=self.default_facility ) if existing_user: error = { @@ -579,6 +579,21 @@ def build_users_objects(self, users): if self.compare_fields(user_obj, values): update_users.append(user_obj) else: + # If UUID is not specified, check for a username clash + if values["uuid"] == "": + existing_user = FacilityUser.objects.filter( + username__iexact=user, facility=self.default_facility + ).first() + if existing_user: + error = { + "row": users[user]["position"], + "username": user, + "message": MESSAGES[DUPLICATED_USERNAME], + "field": "USERNAME", + "value": user, + } + per_line_errors.append(error) + continue if values["uuid"] != "": error = { "row": users[user]["position"],