2017-12-27 21:11:20 +00:00
|
|
|
from django.db import models
|
2020-01-21 17:20:40 +00:00
|
|
|
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
|
|
|
|
from django.core.mail import send_mail
|
2020-01-21 18:20:07 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-01-21 17:20:40 +00:00
|
|
|
from django.utils import timezone
|
2017-12-27 21:11:20 +00:00
|
|
|
|
2020-01-21 17:20:40 +00:00
|
|
|
from .managers import RadioUserManager
|
2017-12-27 21:11:20 +00:00
|
|
|
|
|
|
|
|
2020-01-21 17:20:40 +00:00
|
|
|
class RadioUser(AbstractBaseUser, PermissionsMixin):
|
2017-12-27 21:11:20 +00:00
|
|
|
"""
|
|
|
|
Custom user model which uses email as the main identifier and adds a flag
|
|
|
|
for whether the user is the radio DJ.
|
2020-01-21 17:20:40 +00:00
|
|
|
|
|
|
|
Reworked from the django-authtools module.
|
2017-12-27 21:11:20 +00:00
|
|
|
"""
|
2020-01-21 17:20:40 +00:00
|
|
|
email = models.EmailField(_('email address'), max_length=255, unique=True)
|
|
|
|
name = models.CharField(_('name'), max_length=255)
|
|
|
|
|
|
|
|
is_dj = models.BooleanField(_('dj status'), default=False,
|
|
|
|
help_text=_('Designates whether the user is the automated dj account '
|
|
|
|
'or is a real person account.')
|
|
|
|
)
|
|
|
|
is_staff = models.BooleanField(_('staff status'), default=False,
|
|
|
|
help_text=_('Designates whether the user can log into the admin site.')
|
|
|
|
)
|
|
|
|
is_active = models.BooleanField(_('active'), default=True,
|
|
|
|
help_text=_('Designates whether the user should be treated as active. '
|
|
|
|
'Unselect this instead of deleting accounts.')
|
|
|
|
)
|
|
|
|
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
|
|
|
|
|
|
|
|
objects = RadioUserManager()
|
|
|
|
|
|
|
|
USERNAME_FIELD = 'email'
|
|
|
|
REQUIRED_FIELDS = ['name']
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['name', 'email']
|
|
|
|
|
|
|
|
def get_full_name(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def get_short_name(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def email_user(self, subject, message, from_email=None, **kwargs):
|
|
|
|
'''
|
|
|
|
Sends an email to this user.
|
|
|
|
'''
|
|
|
|
|
|
|
|
send_mail(subject, message, from_email, [self.email], **kwargs)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{name} <{email}>'.format(
|
|
|
|
name=self.name,
|
|
|
|
email=self.email,
|
|
|
|
)
|