diff --git a/stregsystem/admin.py b/stregsystem/admin.py index 74a5ab7d..80f4f4dd 100644 --- a/stregsystem/admin.py +++ b/stregsystem/admin.py @@ -202,7 +202,7 @@ class Meta: def clean_username(self): username = self.cleaned_data['username'] if self.instance is None or self.instance.pk is None: - if Member.objects.filter(username=username).exists(): + if Member.objects.filter(username__iexact=username).exists(): raise forms.ValidationError("Brugernavnet er allerede taget") return username @@ -234,7 +234,7 @@ class MemberAdmin(admin.ModelAdmin): def save_model(self, request, obj, form, change): if 'username' in form.changed_data and change: - if Member.objects.filter(username=obj.username).exclude(pk=obj.pk).exists(): + if Member.objects.filter(username__iexact=obj.username).exclude(pk=obj.pk).exists(): messages.add_message(request, messages.WARNING, 'Det brugernavn var allerede optaget') super().save_model(request, obj, form, change) diff --git a/stregsystem/tests.py b/stregsystem/tests.py index b2db8a45..fd64bcb5 100644 --- a/stregsystem/tests.py +++ b/stregsystem/tests.py @@ -964,6 +964,11 @@ def test_cant_create_duplicate_username(self): form = MemberForm(model_to_dict(jeff)) self.assertFalse(form.is_valid()) + def test_cant_create_duplicate_username_in_other_case(self): + jeff = Member(username="JeFf", firstname="jeffrey", lastname="jefferson", gender="M") + form = MemberForm(model_to_dict(jeff)) + self.assertFalse(form.is_valid()) + def test_can_create_non_duplicate_username(self): not_jeff = Member(username="not_jeff", firstname="jeff", lastname="jefferson", gender="M") form = MemberForm(model_to_dict(not_jeff))