Coverage for accounts/forms.py: 100.00%
20 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-11 15:56 -0700
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-11 15:56 -0700
1from django.contrib.auth import get_user_model 1a
2from django.contrib.auth.forms import UserChangeForm, UserCreationForm 1a
3from django.core.exceptions import ValidationError 1a
5User = get_user_model() 1a
8class CustomUserChangeForm(UserChangeForm): 1a
9 class Meta: 1a
10 model = User 1a
11 fields = ( 1a
12 "email",
13 "username",
14 )
17class CustomUserCreationForm(UserCreationForm): 1a
18 error_message = UserCreationForm.error_messages.update( 1a
19 {"duplicate_username": "This username has already been taken."} # fmt: skip
20 )
22 class Meta: 1a
23 model = User 1a
24 fields = ( 1a
25 "email",
26 "username",
27 )
29 def clean_username(self): 1a
30 username = self.cleaned_data["username"] 1b
32 try: 1b
33 User.objects.get(username=username) 1b
34 except User.DoesNotExist: 1b
35 return username 1b
37 raise ValidationError(self.error_messages["duplicate_username"]) 1b