Last active
November 28, 2020 20:47
-
-
Save andytwoods/626a83906eb408feb367e2d3bc645455 to your computer and use it in GitHub Desktop.
PlainASCIIUsernameValidator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@deconstructible | |
class PlainASCIIUsernameValidator(validators.RegexValidator): | |
regex = good_username_regex | |
message = _( | |
'Enter a valid username. This value may contain only English letters and numbers' | |
) | |
flags = re.ASCII | |
def random_username(): | |
return uuid.uuid4().hex[:username_max_length] | |
class User(AbstractUser, Demographics, HomeStudyInfoMultipleItemsThru, Ram, Ethics): | |
username_validator = PlainASCIIUsernameValidator() | |
username = models.CharField( | |
'username', | |
default=random_username, | |
max_length=username_max_length, | |
unique=True, | |
help_text=_('Required. 20 characters or fewer. English letters and digits only.'), | |
validators=[username_validator], | |
error_messages={ | |
'unique': _("A user with that username already exists."), | |
}, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good_username_regex = r'^[\w]+\Z'