membersystem/parked_apps/users/models.py

61 lines
1.8 KiB
Python
Raw Normal View History

2019-08-31 18:46:49 +00:00
import uuid
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
2018-06-23 11:00:26 +00:00
from django.db import models
2019-08-31 18:46:49 +00:00
from django.utils.translation import gettext_lazy as _
class UserManager(BaseUserManager):
"""The user manager class."""
def create_user(self, password: str = None, **kwargs):
user = self.model(**kwargs)
user.set_password(password)
user.save()
return user
def create_superuser(self, password: str, **kwargs):
user = self.create_user(password=password, **kwargs)
user.is_staff = True
user.is_superuser = True
2019-08-31 22:27:36 +00:00
user.save(update_fields=["is_staff", "is_superuser"])
2019-08-31 18:46:49 +00:00
return user
class User(PermissionsMixin, AbstractBaseUser):
2019-08-31 22:27:36 +00:00
EMAIL_FIELD = "email"
USERNAME_FIELD = "email"
2019-08-31 18:46:49 +00:00
objects = UserManager()
nick = models.CharField(max_length=60, null=True, blank=True)
email = models.EmailField(
unique=True,
2019-08-31 22:27:36 +00:00
verbose_name=_("E-Mail"),
2019-08-31 18:46:49 +00:00
help_text=_(
2019-08-31 22:27:36 +00:00
"Your email address will be used for password resets and notification about your event/submissions."
2019-08-31 18:46:49 +00:00
),
)
is_active = models.BooleanField(default=True)
# For the Django admin...
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
# Used for confirmations and password reminders to NOT disclose email in URL
token_uuid = models.UUIDField(default=uuid.uuid4, editable=False)
def __str__(self) -> str:
"""Use a useful string representation."""
return self.get_display_name()
2018-06-23 11:00:26 +00:00
2019-08-31 18:46:49 +00:00
def get_display_name(self) -> str:
2019-08-31 22:27:36 +00:00
return self.nick if self.nick else str(_("Unnamed user"))
2018-06-23 11:00:26 +00:00
2019-08-31 18:46:49 +00:00
class Meta:
verbose_name = _("User")