| 1 |
import datetime |
| 2 |
|
| 3 |
from core.mixins import CustomGroupPermissionsMixin |
| 4 |
from django.contrib.auth.models import AbstractBaseUser, Group, UserManager |
| 5 |
from django.contrib.auth.validators import UnicodeUsernameValidator |
| 6 |
from django.core.mail import send_mail |
| 7 |
from django.db import models |
| 8 |
from django.utils import timezone |
| 9 |
from django.utils.translation import gettext_lazy as _ |
| 10 |
|
| 11 |
from guardian.mixins import GuardianGroupMixin, GuardianUserMixin |
| 12 |
|
| 13 |
|
| 14 |
class CustomGroup(Group, GuardianGroupMixin): |
| 15 |
label = models.CharField(max_length=120) |
| 16 |
|
| 17 |
|
| 18 |
class CustomUser(AbstractBaseUser, CustomGroupPermissionsMixin, GuardianUserMixin): |
| 19 |
"""Copied from auth.models.AbstractUser and replaced PermissionsMixin with CustomGroupPermissionsMixin.""" |
| 20 |
|
| 21 |
username_validator = UnicodeUsernameValidator() |
| 22 |
|
| 23 |
username = models.CharField( |
| 24 |
_("username"), |
| 25 |
max_length=150, |
| 26 |
unique=True, |
| 27 |
help_text=_("Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only."), |
| 28 |
validators=[username_validator], |
| 29 |
error_messages={ |
| 30 |
"unique": _("A user with that username already exists."), |
| 31 |
}, |
| 32 |
) |
| 33 |
first_name = models.CharField(_("first name"), max_length=150, blank=True) |
| 34 |
last_name = models.CharField(_("last name"), max_length=150, blank=True) |
| 35 |
email = models.EmailField(_("email address"), blank=True) |
| 36 |
is_staff = models.BooleanField( |
| 37 |
_("staff status"), |
| 38 |
default=False, |
| 39 |
help_text=_("Designates whether the user can log into this admin site."), |
| 40 |
) |
| 41 |
is_active = models.BooleanField( |
| 42 |
_("active"), |
| 43 |
default=True, |
| 44 |
help_text=_( |
| 45 |
"Designates whether this user should be treated as active. Unselect this instead of deleting accounts." |
| 46 |
), |
| 47 |
) |
| 48 |
date_joined = models.DateTimeField(_("date joined"), default=timezone.now) |
| 49 |
|
| 50 |
birth_date = models.DateField(null=True, blank=True) |
| 51 |
|
| 52 |
objects = UserManager() |
| 53 |
|
| 54 |
EMAIL_FIELD = "email" |
| 55 |
USERNAME_FIELD = "username" |
| 56 |
REQUIRED_FIELDS = ["email"] |
| 57 |
|
| 58 |
def clean(self): |
| 59 |
super().clean() |
| 60 |
self.email = self.__class__.objects.normalize_email(self.email) |
| 61 |
|
| 62 |
def get_full_name(self): |
| 63 |
""" |
| 64 |
Return the first_name plus the last_name, with a space in between. |
| 65 |
""" |
| 66 |
full_name = f"{self.first_name} {self.last_name}" |
| 67 |
return full_name.strip() |
| 68 |
|
| 69 |
def get_short_name(self): |
| 70 |
"""Return the short name for the user.""" |
| 71 |
return self.first_name |
| 72 |
|
| 73 |
def email_user(self, subject, message, from_email=None, **kwargs): |
| 74 |
"""Send an email to this user.""" |
| 75 |
send_mail(subject, message, from_email, [self.email], **kwargs) |
| 76 |
|
| 77 |
|
| 78 |
def get_custom_anon_user(User): |
| 79 |
return User( |
| 80 |
username="AnonymousUser", |
| 81 |
birth_date=datetime.date(1410, 7, 15), |
| 82 |
) |