class CustomSetPasswordForm(forms.Form): """ A form that lets a user change set their password without entering the old password """ new_password = forms.CharField( label=_("New password"), widget=forms.PasswordInput, strip=False, help_text=password_validation.password_validators_help_text_html(), ) def __init__(self, user, *args, **kwargs): self.user = user super().__init__(*args, **kwargs) def clean_new_password(self): password = self.cleaned_data.get('new_password') password_validation.validate_password(password, self.user) return password def save(self, commit=True): password = self.cleaned_data["new_password"] self.user.set_password(password) if commit: self.user.save() return self.user
class UserRegisterForm(UserCreationForm): error_messages = { 'password_mismatch': "The two password fields didn't match.", 'email_exists': 'email already existed.' } password1 = forms.CharField( label="Password", strip=False, widget=forms.PasswordInput, help_text=password_validation.password_validators_help_text_html(), ) password2 = forms.CharField( label="Password confirmation", widget=forms.PasswordInput, strip=False, help_text="Enter the same password as before, for verification.", ) class Meta: model = User fields = ("username", 'email') field_classes = {'username': UsernameField, 'email': EmailField} def clean_email(self): email = self.cleaned_data.get('email') if User.objects.filter(email=email).exists(): raise forms.ValidationError( self.error_messages['email_exists'], code='email_exists', ) return email
class UserCreationForm(forms.ModelForm): """ A form that creates a user, with no privileges, from the given username and password. """ error_messages = { 'password_mismatch': _("The two password fields didn't match."), } password1 = forms.CharField( label=_("Password"), strip=False, widget=forms.PasswordInput, help_text=password_validation.password_validators_help_text_html(), ) password2 = forms.CharField( label=_("Password confirmation"), widget=forms.PasswordInput, strip=False, help_text=_("Enter the same password as before, for verification."), ) class Meta: model = User fields = ("email", ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if self._meta.model.USERNAME_FIELD in self.fields: self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update( {'autofocus': True}) def clean_password2(self): password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: raise forms.ValidationError( self.error_messages['password_mismatch'], code='password_mismatch', ) return password2 def _post_clean(self): super()._post_clean() # Validate the password after self.instance is updated with form data # by super(). password = self.cleaned_data.get('password2') if password: try: password_validation.validate_password(password, self.instance) except forms.ValidationError as error: self.add_error('password2', error) def save(self, commit=True): user = super().save(commit=False) user.set_password(self.cleaned_data["password1"]) if commit: user.save() return user
class StyledUserCreationForm(UserCreationForm): """User creation form with bootstrap styles""" username = forms.CharField(widget=TextInput( attrs={'class': 'form-control'})) password1 = forms.CharField( label="Password", strip=False, widget=forms.PasswordInput(attrs={'class': 'form-control'}), help_text=password_validation.password_validators_help_text_html(), ) password2 = forms.CharField( label="Password confirmation", widget=forms.PasswordInput(attrs={'class': 'form-control'}), strip=False, help_text="Enter the same password as before, for verification.", )
class UserResetPasswordForm(SetPasswordForm): error_messages = { 'password_mismatch': "The two password fields didn't match.", } new_password1 = forms.CharField( label="New password", widget=forms.PasswordInput, strip=False, help_text=password_validation.password_validators_help_text_html(), ) new_password2 = forms.CharField( label="New password confirmation", strip=False, widget=forms.PasswordInput, )
class ChangeForgetPasswordForm(forms.Form): """ A form that lets a user change set their password without entering the old password """ error_messages = { 'password_mismatch': _('The two password fields didn’t match.'), } new_password1 = forms.CharField( label=_("New password"), widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}), strip=False, help_text=password_validation.password_validators_help_text_html(), ) new_password2 = forms.CharField( label=_("New password confirmation"), strip=False, widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}), ) def __init__(self, user, *args, **kwargs): self.user = user super().__init__(*args, **kwargs) def clean_new_password2(self): password1 = self.cleaned_data.get('new_password1') password2 = self.cleaned_data.get('new_password2') if password1 and password2: if password1 != password2: raise forms.ValidationError( self.error_messages['password_mismatch'], code='password_mismatch', ) password_validation.validate_password(password2, self.user) return password2 def save(self, commit=True): password = self.cleaned_data["new_password1"] self.user.set_password(password) if commit: self.user.save() return self.user
class UserCreationForm(DefaultUserCreationForm): class Meta: model = User fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2') error_messages = { 'password_mismatch': _("Пароли не совпадают."), } first_name = forms.CharField(label=_("Имя"), widget=forms.TextInput, strip=True) last_name = forms.CharField(label=_("Фамилия"), widget=forms.TextInput, strip=True) email = forms.EmailField(label=_("E-Mail"), widget=forms.EmailInput) password1 = forms.CharField( label=_("Пароль"), strip=False, widget=forms.PasswordInput, help_text=password_validation.password_validators_help_text_html(), ) password2 = forms.CharField( label=_("Подтверждение пароля"), widget=forms.PasswordInput, strip=False, help_text=_("Enter the same password as before, for verification."), ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def clean_email(self): email = self.cleaned_data['email'] if User.objects.filter(email=email).exists(): raise ValidationError( 'Пользователь с данным E-mail уже существует!') return email
class CustomPasswordChangeForm(PasswordChangeForm): """비밀번호 변경 폼 클래스""" old_password = forms.CharField( label="현재 비밀번호", strip=False, widget=forms.PasswordInput(attrs={'autocomplete': 'current-password', 'autofocus': True, "class": "form-control"}), ) new_password1 = forms.CharField( label="새 비밀번호", strip=False, widget=forms.PasswordInput(attrs={"class": "form-control"}), help_text=password_validation.password_validators_help_text_html(), ) new_password2 = forms.CharField( label="새 비밀번호 확인", strip=False, widget=forms.PasswordInput(attrs={"class": "form-control"}), ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)