Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.contrib.auth.forms import UserCreationForm
- from django.core.exceptions import ValidationError
- from django.utils.translation import gettext_lazy as _
- from django.contrib.auth import get_user_model
- UserModel = get_user_model()
- class MyHolidayUserCreationForm(UserCreationForm):
- user = None
- def clean_password1(self):
- password1 = self.cleaned_data.get("password1")
- self.validate_password(password1)
- return password1
- def validate_password(self, password):
- if len(password) < 6:
- raise ValidationError(_("Password must be at least 6 characters long."))
- has_letter = False
- has_digit = False
- has_underscore = False
- for char in password:
- if char.isalpha():
- has_letter = True
- elif char.isdigit():
- has_digit = True
- elif char == '_':
- has_underscore = True
- if not has_letter:
- raise ValidationError(_("Password must contain at least one letter."))
- if not has_digit:
- raise ValidationError(_("Password must contain at least one digit."))
- if not has_underscore:
- raise ValidationError(_("Password must contain at least one underscore."))
- class Meta(UserCreationForm.Meta):
- model = UserModel
- fields = ('email',)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement