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 ugettext_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, ) class Setting(models.Model): """ A model for keeping track of dynamic settings while the site is online and the radio is running. """ INTEGER = 0 FLOAT = 1 STRING = 2 BOOL = 3 TYPE_CHOICES = ( (INTEGER, 'Integer'), (FLOAT, 'Float'), (STRING, 'String'), (BOOL, 'Bool'), ) name = models.CharField(_('name'), max_length=64, unique=True) description = models.TextField(_('description'), blank=True) setting_type = models.PositiveIntegerField(_('variable type'), choices=TYPE_CHOICES, default=INTEGER) data = models.TextField(_('data')) def get(self): if self.setting_type == self.INTEGER: return int(self.data) elif self.setting_type == self.FLOAT: return float(self.data) elif self.setting_type == self.BOOL: return self.data == 'True' else: return self.data def __str__(self): return '{}: {}'.format(self.name, self.data)