class SignUpForm(UserCreationForm): email = forms.EmailField( max_length=200, widget=forms.TextInput(attrs={'size': 50}), help_text=_('Required. Enter an existing email address.')) password1 = PasswordField( label='Password', help_text=password_validation.password_validators_help_text_html(), widget=PasswordStrengthInput(attrs={ "class": "form-control", "placeholder": "Password" })) class Meta: model = User widgets = { 'username': forms.TextInput(attrs={'size': 50}), } fields = ('username', 'email', 'password1', 'password2') def clean_email(self): email = self.cleaned_data['email'] user = User.objects.filter(email__iexact=email).exists() if user: raise ValidationError(_('You can not use this email address.')) return email
class ChangePasswordForm(PasswordChangeForm): new_password1 = PasswordField( label='New password', help_text=password_validation.password_validators_help_text_html(), widget=PasswordStrengthInput(attrs={ "class": "form-control", "placeholder": "New Password" }))
class SetPasswordForm(auth_forms.SetPasswordForm): new_password1 = PasswordField( label=_("New password"), strip=False, ) new_password2 = PasswordConfirmationField( confirm_with='new_password1', label=_("New password confirmation"), strip=False, )
class RegisterForm(forms.Form): password1 = PasswordField(label='password here') password2 = PasswordConfirmationField(confirm_with='password1') def clean(self): password = self.cleaned_data.get('password1') passwordVerification = self.cleaned_data.get('password2') if password: score = zxcvbn(password, [passwordVerification])['score'] return self.cleaned_data
class LoginInfoForm(forms.ModelForm): error_messages = { 'pw_current_wrong': _("The current password you entered was not correct."), } old_password = forms.CharField(widget=forms.PasswordInput, label=_('Password (current)'), required=True) password = PasswordField( label=_('New password'), required=False, ) password_repeat = PasswordConfirmationField( label=_('New password (again)'), required=False, confirm_with='password', ) def clean_old_password(self): old_pw = self.cleaned_data.get('old_password') if not check_password(old_pw, self.user.password): raise forms.ValidationError( self.error_messages['pw_current_wrong'], code='pw_current_wrong', ) return old_pw def __init__(self, user, *args, **kwargs): self.user = user kwargs['instance'] = user super().__init__(*args, **kwargs) def save(self): password = self.cleaned_data.get('password') if not password == self.cleaned_data.get('password_repeat'): raise ValidationError( _('You entered two different passwords. Please input the same one twice!' )) super().save() if password: self.user.set_password(password) self.user.save() class Meta: model = User fields = ('email', )
class RecoverForm(forms.Form): password = PasswordField( label=_('New password'), required=False, ) password_repeat = PasswordConfirmationField( label=_('New password (again)'), required=False, confirm_with='password', ) def clean(self): data = super().clean() if data.get('password') != data.get('password_repeat'): raise ValidationError(_('You entered two different passwords. Please input the same one twice!')) return data
class CustomSetPasswordForm(forms.Form): password = PasswordField( max_length=32, required=True, label=_('Password'), widget=PasswordStrengthInput( attrs={'placeholder': _('Password'), "class": "form-control form-control-gold"} ), ) def __init__(self, user, *args, **kwargs): self.user = user super().__init__(*args, **kwargs) def save(self, commit=True): password = self.cleaned_data["password"] self.user.set_password(password) if commit: self.user.save() return self.user
class UserForm(forms.Form): login_username = forms.CharField(max_length=60, label=_('Username or email address'), required=False) login_password = forms.CharField(widget=forms.PasswordInput, label=_('Password'), required=False) register_username = forms.CharField(max_length=60, label=_('Username'), required=False) register_email = forms.EmailField(label=_('Email address'), required=False) register_password = PasswordField( label=_('Password'), required=False, ) register_password_repeat = PasswordConfirmationField( label=_('Password (again)'), required=False, confirm_with='register_password', ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['register_email'].widget.attrs = { 'placeholder': _('Email address') } def _clean_login(self, data): if '@' in data.get('login_username'): try: uname = User.objects.get(email=data.get('login_username')).nick except User.DoesNotExist: uname = 'user@invalid' else: uname = data.get('login_username') user = authenticate(username=uname, password=data.get('login_password')) if user is None: raise ValidationError( _('No user account matches the entered credentials. ' 'Are you sure that you typed your password correctly?')) if not user.is_active: raise ValidationError( _('Sorry, your account is currently disabled.')) data['user_id'] = user.pk def _clean_register(self, data): if data.get('register_password') != data.get( 'register_password_repeat'): raise ValidationError( _('You entered two different passwords. Please input the same one twice!' )) if User.objects.filter(nick=data.get('register_username')).exists(): raise ValidationError( _('We already have a user with that username. Did you already register before ' 'and just need to log in?')) if User.objects.filter(email=data.get('register_email')).exists(): raise ValidationError( _('We already have a user with that email address. Did you already register ' 'before and just need to log in?')) def clean(self): data = super().clean() if data.get('login_username') and data.get('login_password'): self._clean_login(data) elif data.get('register_username') and data.get( 'register_email') and data.get('register_password'): self._clean_register(data) else: raise ValidationError( _('You need to fill all fields of either the login or the registration form.' )) return data def save(self): data = self.cleaned_data if data.get('register_username') and data.get( 'register_email') and data.get('register_password'): user = User.objects.create_user( nick=data.get('register_username'), email=data.get('register_email'), password=data.get('register_password'), locale=translation.get_language(), timezone=timezone.get_current_timezone_name()) data['user_id'] = user.pk return data['user_id']
class CustomChangePasswordForm(PasswordChangeForm): """Extending Password Change Form Overriding help_text """ new_password1 = PasswordField() new_password2 = PasswordConfirmationField(confirm_with="new_password1") def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) help_text = "<ul>" \ "<li>Your password can't be too similar to " \ "your other personal information</li>" \ "<li>Your password must contain " \ "at least 14 characters</li>" \ "<li>Your password can't be a " \ "commonly used password</li>" \ "<li>Your password can't be " \ "entirely numeric</li>" \ "<li>Your password must not be " \ "the same as the current password</li>" \ "<li>Your password must use of " \ "both uppercase and lowercase letters</li>" \ "<li>Your password must include " \ "of one or more numerical digits</li>" \ "<li>Your password must include " \ "of special characters, such as @, #, $</li>" \ "</ul>" self.fields['new_password1'].help_text = help_text class Meta: fields = [ 'old_password', 'new_password1', 'new_password2', ] def clean(self): """Clean is first in order not to have not matched error if other validation fails """ cleaned_data = super().clean() new_password1 = cleaned_data.get('new_password1') new_password2 = cleaned_data.get('new_password2') if new_password1 != new_password2: raise forms.ValidationError('Your new password and ' 'confirmation do not match') def clean_new_password1(self): """Validating the Password1 Field""" new_password1 = self.cleaned_data.get('new_password1') new_password2 = self.cleaned_data.get('new_password2') if check_password(new_password1, self.user.password): raise forms.ValidationError('Your password must not be ' 'the same as the current password') characters = set(new_password1) lower = any(letter.islower() for letter in characters) upper = any(letter.isupper() for letter in characters) digit = any(letter.isdigit() for letter in characters) if not upper: raise forms.ValidationError('Your password must use of ' 'both uppercase and lowercase letters') if not lower: raise forms.ValidationError('Your password must use of ' 'both uppercase and lowercase letters') if not digit: raise forms.ValidationError('Your password must include ' 'of one or more numerical digits') special_characters = ["@", "#", "$"] check = False for character in special_characters: if character in new_password1: check = True if not check: raise forms.ValidationError('Your password must include of ' 'special characters, such as @, #, $') first_name = self.user.profile.first_name.lower() last_name = self.user.profile.last_name.lower() if first_name in new_password1.lower(): raise forms.ValidationError('Your password cannot be too ' 'similar to your other personal ' 'information') if last_name in new_password1.lower(): raise forms.ValidationError('Your password cannot be too ' 'similar to your other personal ' 'information') return new_password1
class CreateForm(forms.Form): def validate_username(value): try: u = User.objects.get(username=value) except User.DoesNotExist: return value raise ValidationError(_("A user with that username already exists.")) def validate_unique_email(value): try: u = User.objects.get(email=value) except User.DoesNotExist: return value raise ValidationError(_("A user with that e-mail already exists.")) def validate_referral_id(value): try: u = User.objects.get(referral_id=value) except User.DoesNotExist: raise ValidationError(_("There's no user with such referral ID")) return value last_name = forms.CharField( max_length=30, required=True, label=_('Last name'), widget=forms.TextInput( attrs={'placeholder': _('Last name'), "class": "form-control form-control-gold"} ) ) first_name = forms.CharField( max_length=30, required=True, label=_('First name'), widget=forms.TextInput( attrs={'placeholder': _('First name'), "class": "form-control form-control-gold"} ) ) middle_name = forms.CharField( max_length=30, required=True, label=_('Last name'), widget=forms.TextInput( attrs={'placeholder': _('Last name'), "class": "form-control form-control-gold"} ) ) username = forms.CharField( max_length=32, required=True, validators=[UnicodeUsernameValidator, validate_username,], label=_('Username'), widget=forms.TextInput( attrs={'placeholder': _('Username'), "class": "form-control form-control-gold"} ) ) email = forms.EmailField( required=True, validators=[validate_unique_email,], label=_('E-mail'), widget=forms.EmailInput( attrs={'placeholder': _('E-mail'), "class": "form-control form-control-gold"} ) ) password = PasswordField( max_length=32, required=True, label=_('Password'), widget=PasswordStrengthInput( attrs={'placeholder': _('Password'), "class": "form-control form-control-gold"} ) ) rules_accepted = forms.BooleanField(required=True) captcha = ReCaptchaField(widget=ReCaptchaV3())
class passreset(forms.Form): username = forms.CharField( required=True, widget=forms.HiddenInput(attrs={'autocomplete': 'off'})) newpassword = PasswordField(label='New Password', ) confirmpassword = PasswordConfirmationField(label='Confirm Password', confirm_with='newpassword')