Skip to content

Instantly share code, notes, and snippets.

@andytwoods
Last active November 28, 2020 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andytwoods/626a83906eb408feb367e2d3bc645455 to your computer and use it in GitHub Desktop.
Save andytwoods/626a83906eb408feb367e2d3bc645455 to your computer and use it in GitHub Desktop.
PlainASCIIUsernameValidator
@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."),
},
)
@andytwoods
Copy link
Author

good_username_regex = r'^[\w]+\Z'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment