spradio-server-django/savepointradio/core/models.py

58 lines
1.9 KiB
Python

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.core.mail import send_mail
from django.utils.translation import gettext_lazy as _
from django.utils import timezone
from .managers import RadioUserManager
class RadioUser(AbstractBaseUser, PermissionsMixin):
"""
Custom user model which uses email as the main identifier and adds a flag
for whether the user is the radio DJ.
Reworked from the django-authtools module.
"""
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,
)