import re from django.contrib.auth.models import AbstractUser from django.core import validators from django.db import models from django.utils.translation import ugettext_lazy as _ from emissions.models import Emission, Episode, NewsItem, NewsCategory, SoundFile class User(AbstractUser): emissions = models.ManyToManyField(Emission, null=True, blank=True) news_categories = models.ManyToManyField(NewsCategory, null=True, blank=True) phone = models.CharField(_('Phone'), max_length=20, null=True, blank=True) mobile = models.CharField(_('Mobile'), max_length=20, null=True, blank=True) class Meta: ordering = ['first_name', 'last_name'] def can_manage(self, object): if self.is_staff: return True if isinstance(object, Emission): return self.has_perm('emissions.change_emission') or object in self.emissions.all() if isinstance(object, Episode): return self.has_perm('emissions.change_episode') or object.emission in self.emissions.all() if isinstance(object, SoundFile): return self.has_perm('emissions.change_soundfile') or object.episode.emission in self.emissions.all() if isinstance(object, NewsItem): return self.has_perm('emissions.change_newsitem') or object.emission in self.emissions.all() return False def __unicode__(self): s = super(User, self).__unicode__() if self.mobile: return '%s (%s)' % (s, self.mobile) if self.phone: return '%s (%s)' % (s, self.phone) return s