models.py 1.76 KB
Newer Older
fred's avatar
fred committed
1 2 3 4 5
import re

from django.contrib.auth.models import AbstractUser
from django.core import validators
from django.db import models
6
from django.utils.translation import ugettext_lazy as _
fred's avatar
fred committed
7

8
from emissions.models import Emission, Episode, NewsItem, NewsCategory, SoundFile
fred's avatar
fred committed
9 10

class User(AbstractUser):
fred's avatar
fred committed
11
    emissions = models.ManyToManyField(Emission, null=True, blank=True)
fred's avatar
fred committed
12
    news_categories = models.ManyToManyField(NewsCategory, null=True, blank=True)
fred's avatar
fred committed
13

14 15 16 17 18 19
    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']

fred's avatar
fred committed
20 21 22 23
    def can_manage(self, object):
        if self.is_staff:
            return True
        if isinstance(object, Emission):
24
            return self.has_perm('emissions.change_emission') or object in self.emissions.all()
fred's avatar
fred committed
25
        if isinstance(object, Episode):
26
            return self.has_perm('emissions.change_episode') or object.emission in self.emissions.all()
27
        if isinstance(object, SoundFile):
28
            return self.has_perm('emissions.change_soundfile') or object.episode.emission in self.emissions.all()
fred's avatar
fred committed
29
        if isinstance(object, NewsItem):
30
            return self.has_perm('emissions.change_newsitem') or object.emission in self.emissions.all()
fred's avatar
fred committed
31
        return False
32 33 34

    def __unicode__(self):
        s = super(User, self).__unicode__()
fred's avatar
fred committed
35 36 37 38 39 40 41
        parts = []
        if self.first_name:
            parts.append(self.first_name)
        if self.last_name:
            parts.append(self.last_name)
        if parts:
            s = ' '.join(parts)
42 43 44 45 46
        if self.mobile:
            return '%s (%s)' % (s, self.mobile)
        if self.phone:
            return '%s (%s)' % (s, self.phone)
        return s