membersystem/users/models.py

61 lines
1.8 KiB
Python
Raw Permalink 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
user.save(update_fields=['is_staff', 'is_superuser'])
return user
class User(PermissionsMixin, AbstractBaseUser):
EMAIL_FIELD = 'email'
USERNAME_FIELD = 'email'
objects = UserManager()
nick = models.CharField(max_length=60, null=True, blank=True)
email = models.EmailField(
unique=True,
verbose_name=_('E-Mail'),
help_text=_(
'Your email address will be used for password resets and notification about your event/submissions.'
),
)
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:
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")