1
0
mirror of https://github.com/etesync/server synced 2024-11-23 01:08:08 +00:00
etesync-server/etebase_server/myauth/models.py
Xiretza 9d6e0ae60a fix: move myauth module from toplevel to under etebase_server
This is in preparation for creating a python package, which should only
occupy the "etebase_server" name in the global module namespace.
2022-05-09 17:41:16 +02:00

52 lines
1.4 KiB
Python

import typing as t
from django.contrib.auth.models import AbstractUser, UserManager as DjangoUserManager
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):
regex = r"^[\w.-]+\Z"
message = _("Enter a valid username. This value may contain only letters, " "numbers, and ./-/_ characters.")
flags = 0
class UserManager(DjangoUserManager):
def get_by_natural_key(self, username: str):
return self.get(**{self.model.USERNAME_FIELD + "__iexact": username})
class User(AbstractUser):
id: int
username_validator = UnicodeUsernameValidator()
objects: UserManager = UserManager()
username = models.CharField(
_("username"),
max_length=150,
unique=True,
help_text=_("Required. 150 characters or fewer. Letters, digits and ./-/_ only."),
validators=[username_validator],
error_messages={
"unique": _("A user with that username already exists."),
},
)
@classmethod
def normalize_username(cls, username: str):
return super().normalize_username(username).lower()
UserType = User
def get_typed_user_model() -> UserType:
from django.contrib.auth import get_user_model
ret: t.Any = get_user_model()
return ret