2020-07-12 08:11:33 +00:00
|
|
|
from django.contrib.auth.models import AbstractUser, UserManager as DjangoUserManager
|
2020-05-15 08:01:56 +00:00
|
|
|
from django.core import validators
|
|
|
|
from django.db import models
|
|
|
|
from django.utils.deconstruct import deconstructible
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
|
|
|
|
@deconstructible
|
|
|
|
class UnicodeUsernameValidator(validators.RegexValidator):
|
2020-07-16 07:40:30 +00:00
|
|
|
regex = r'^[\w.-]+\Z'
|
2020-05-15 08:01:56 +00:00
|
|
|
message = _(
|
|
|
|
'Enter a valid username. This value may contain only letters, '
|
2020-07-16 07:40:30 +00:00
|
|
|
'numbers, and ./-/_ characters.'
|
2020-05-15 08:01:56 +00:00
|
|
|
)
|
|
|
|
flags = 0
|
2020-05-13 13:01:49 +00:00
|
|
|
|
|
|
|
|
2020-07-12 08:11:33 +00:00
|
|
|
class UserManager(DjangoUserManager):
|
|
|
|
def get_by_natural_key(self, username):
|
|
|
|
return self.get(**{self.model.USERNAME_FIELD + '__iexact': username})
|
|
|
|
|
|
|
|
|
2020-05-13 13:01:49 +00:00
|
|
|
class User(AbstractUser):
|
2020-05-15 08:01:56 +00:00
|
|
|
username_validator = UnicodeUsernameValidator()
|
|
|
|
|
2020-07-12 08:11:33 +00:00
|
|
|
objects = UserManager()
|
|
|
|
|
2020-05-15 08:01:56 +00:00
|
|
|
username = models.CharField(
|
|
|
|
_('username'),
|
|
|
|
max_length=150,
|
|
|
|
unique=True,
|
2020-07-16 07:40:30 +00:00
|
|
|
help_text=_('Required. 150 characters or fewer. Letters, digits and ./-/_ only.'),
|
2020-05-15 08:01:56 +00:00
|
|
|
validators=[username_validator],
|
|
|
|
error_messages={
|
|
|
|
'unique': _("A user with that username already exists."),
|
|
|
|
},
|
|
|
|
)
|
2020-07-12 08:11:33 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def normalize_username(cls, username):
|
|
|
|
return super().normalize_username(username).lower()
|