2020-12-29 11:22:36 +00:00
|
|
|
import typing as t
|
|
|
|
|
2020-07-12 08:11:33 +00:00
|
|
|
from django.contrib.auth.models import AbstractUser, UserManager as DjangoUserManager
|
2024-06-08 21:56:36 +00:00
|
|
|
from django.contrib.auth.validators import UnicodeUsernameValidator
|
2020-05-15 08:01:56 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
|
2020-07-12 08:11:33 +00:00
|
|
|
class UserManager(DjangoUserManager):
|
2024-06-08 21:56:36 +00:00
|
|
|
def get_by_natural_key(self, username: t.Optional[str]):
|
2020-11-14 15:04:41 +00:00
|
|
|
return self.get(**{self.model.USERNAME_FIELD + "__iexact": username})
|
2020-07-12 08:11:33 +00:00
|
|
|
|
|
|
|
|
2020-05-13 13:01:49 +00:00
|
|
|
class User(AbstractUser):
|
2020-12-29 14:06:59 +00:00
|
|
|
id: int
|
2020-05-15 08:01:56 +00:00
|
|
|
username_validator = UnicodeUsernameValidator()
|
|
|
|
|
2020-12-29 14:06:59 +00:00
|
|
|
objects: UserManager = UserManager()
|
2020-07-12 08:11:33 +00:00
|
|
|
|
2020-05-15 08:01:56 +00:00
|
|
|
username = models.CharField(
|
2020-11-14 15:04:41 +00:00
|
|
|
_("username"),
|
2020-05-15 08:01:56 +00:00
|
|
|
max_length=150,
|
|
|
|
unique=True,
|
2020-11-14 15:04:41 +00:00
|
|
|
help_text=_("Required. 150 characters or fewer. Letters, digits and ./-/_ only."),
|
2020-05-15 08:01:56 +00:00
|
|
|
validators=[username_validator],
|
2020-12-29 11:22:36 +00:00
|
|
|
error_messages={
|
|
|
|
"unique": _("A user with that username already exists."),
|
|
|
|
},
|
2020-05-15 08:01:56 +00:00
|
|
|
)
|
2020-07-12 08:11:33 +00:00
|
|
|
|
|
|
|
@classmethod
|
2020-12-29 15:18:09 +00:00
|
|
|
def normalize_username(cls, username: str):
|
2020-07-12 08:11:33 +00:00
|
|
|
return super().normalize_username(username).lower()
|
2020-12-29 11:22:36 +00:00
|
|
|
|
|
|
|
|
2020-12-29 12:04:17 +00:00
|
|
|
UserType = User
|
2020-12-29 11:22:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_typed_user_model() -> UserType:
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
|
|
|
ret: t.Any = get_user_model()
|
|
|
|
return ret
|