You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
4 years ago
|
import typing as t
|
||
|
|
||
4 years ago
|
from django.contrib.auth.models import AbstractUser, UserManager as DjangoUserManager
|
||
5 years ago
|
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):
|
||
4 years ago
|
regex = r"^[\w.-]+\Z"
|
||
|
message = _("Enter a valid username. This value may contain only letters, " "numbers, and ./-/_ characters.")
|
||
5 years ago
|
flags = 0
|
||
5 years ago
|
|
||
|
|
||
4 years ago
|
class UserManager(DjangoUserManager):
|
||
4 years ago
|
def get_by_natural_key(self, username: str):
|
||
4 years ago
|
return self.get(**{self.model.USERNAME_FIELD + "__iexact": username})
|
||
4 years ago
|
|
||
|
|
||
5 years ago
|
class User(AbstractUser):
|
||
4 years ago
|
id: int
|
||
5 years ago
|
username_validator = UnicodeUsernameValidator()
|
||
|
|
||
4 years ago
|
objects: UserManager = UserManager()
|
||
4 years ago
|
|
||
5 years ago
|
username = models.CharField(
|
||
4 years ago
|
_("username"),
|
||
5 years ago
|
max_length=150,
|
||
|
unique=True,
|
||
4 years ago
|
help_text=_("Required. 150 characters or fewer. Letters, digits and ./-/_ only."),
|
||
5 years ago
|
validators=[username_validator],
|
||
4 years ago
|
error_messages={
|
||
|
"unique": _("A user with that username already exists."),
|
||
|
},
|
||
5 years ago
|
)
|
||
4 years ago
|
|
||
|
@classmethod
|
||
4 years ago
|
def normalize_username(cls, username: str):
|
||
4 years ago
|
return super().normalize_username(username).lower()
|
||
4 years ago
|
|
||
|
|
||
4 years ago
|
UserType = User
|
||
4 years ago
|
|
||
|
|
||
|
def get_typed_user_model() -> UserType:
|
||
|
from django.contrib.auth import get_user_model
|
||
|
|
||
|
ret: t.Any = get_user_model()
|
||
|
return ret
|