class RegistrationForm(UserCreationForm): """ User input for sign-up As per registration docs, this should be a minimal subclass of registration.forms.RegistrationFormUniqueEmail. However that wasn't working, so this instead is an equivalent, based on some of that code. """ # FIXME: Is this an artifact of django-registration 2.4.1 that is no longer necessary due to using 3.0? class Meta: model = User fields = ['email', 'first_name', 'last_name', 'password1', 'password2'] email = forms.EmailField(label='Email address:', required=True, validators=[ validate_duplicate_email, validators.validate_confusables_email, ]) first_name = forms.CharField(label='First name:', required='True', validators=[ validators.ReservedNameValidator(), validators.validate_confusables, ]) last_name = forms.CharField(label='Last name:', required='True', validators=[ validators.ReservedNameValidator(), validators.validate_confusables, ])
def test_reserved_name_validator_eq(self): """ Test ReservedNameValidator __eq__ method. __eq__ is necessary for serializing custom user models that use the validator. """ validator = validators.ReservedNameValidator() validator_same = validators.ReservedNameValidator() self.assertTrue(validator.__eq__(validator_same)) validator_different = validators.ReservedNameValidator( reserved_names=[]) self.assertFalse(validator.__eq__(validator_different))
def clean_username(self): value = self.cleaned_data.get('username') if value != self.request.user.username: error_message = 'Another user has this username.' validators.validate_unique(get_user_model(), 'username', error_message)(value) validators.ReservedNameValidator()(value) return value
def test_reserved_name_non_string(self): """ GitHub issue #82: reserved-name validator should not attempt to validate a non-string 'username'. """ validator = validators.ReservedNameValidator() for value in (123456, 1.7, uuid.uuid4()): self.assertTrue(validator(value) is None)
def __init__(self): self.validators = [ validators.CaseInsensitiveUnique( get_user_model(), 'username', 'Invalid username: Already taken'), validators.ReservedNameValidator(), validators.validate_confusables, UnicodeUsernameValidator() ]