Coverage for claims\forms.py: 95%

21 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-14 18:28 -0400

1from django import forms 

2from django.contrib.auth.models import User 

3 

4 

5class RegistrationForm(forms.ModelForm): 

6 """Simple registration form. Skips Django's password validators.""" 

7 

8 password1 = forms.CharField( 

9 label="Password", 

10 widget=forms.PasswordInput(attrs={"autocomplete": "new-password"}), 

11 strip=False, 

12 help_text="", 

13 ) 

14 password2 = forms.CharField( 

15 label="Confirm Password", 

16 widget=forms.PasswordInput(attrs={"autocomplete": "new-password"}), 

17 strip=False, 

18 help_text="", 

19 ) 

20 

21 class Meta: 

22 model = User 

23 fields = ("username",) 

24 widgets = { 

25 "username": forms.TextInput(attrs={ 

26 "autofocus": True, 

27 "autocomplete": "username", 

28 }) 

29 } 

30 

31 def clean_password2(self): 

32 password1 = self.cleaned_data.get("password1") 

33 password2 = self.cleaned_data.get("password2") 

34 if password1 != password2: 

35 raise forms.ValidationError("Passwords do not match.") 

36 # intentionally NOT call Django's validate_password here. 

37 # should not be like this in actual 

38 return password2 

39 

40 def save(self, commit=True): 

41 user = super().save(commit=False) 

42 user.set_password(self.cleaned_data["password1"]) # hashes without validation 

43 if commit: 

44 user.save() 

45 return user 

46