Projet

Général

Profil

Télécharger (2,86 ko) Statistiques
| Branche: | Tag: | Révision:

root / corbo / models.py @ e93ea142

1
from django.utils import timezone
2
from django.conf import settings
3
from django.db import models
4
from django.utils.translation import ugettext_lazy as _
5

    
6
from ckeditor.fields import RichTextField
7

    
8
channel_choices = (
9
    ('mailto', _('Email')),
10
    ('homepage', _('Homepage'))
11
)
12

    
13
class Category(models.Model):
14
    name = models.CharField(max_length=64, blank=False, null=False)
15
    ctime = models.DateTimeField(auto_now_add=True)
16

    
17
    def __unicode__(self):
18
        return self.name
19

    
20

    
21
class Announce(models.Model):
22
    category  = models.ForeignKey('Category', verbose_name=_('category'))
23
    title = models.CharField(_('title'), max_length=256,
24
                             help_text=_('maximum 256 characters'))
25
    text = RichTextField(_('Content'))
26
    publication_time = models.DateTimeField(_('publication time'), blank=True,
27
                                            null=True)
28
    expiration_time = models.DateTimeField(_('Expires on'), blank=True,
29
                                           null=True)
30
    ctime = models.DateTimeField(_('creation time'), auto_now_add=True)
31
    mtime = models.DateTimeField(_('modification time'), auto_now=True)
32

    
33
    def __unicode__(self):
34
        return u'{title} ({id}) at {mtime}'.format(title=self.title,
35
                                    id=self.id, mtime=self.mtime)
36

    
37
    def is_expired(self):
38
        if self.expiration_time:
39
            return self.expiration_time < timezone.now()
40
        return False
41

    
42
    def is_published(self):
43
        if self.is_expired():
44
            return False
45
        if self.publication_time:
46
            return self.publication_time <= timezone.now()
47
        return False
48

    
49
    class Meta:
50
        verbose_name = _('announce')
51
        ordering = ('-mtime',)
52

    
53

    
54
class Broadcast(models.Model):
55
    announce = models.ForeignKey(Announce, verbose_name=_('announce'))
56
    channel = models.CharField(_('channel'), max_length=32,
57
            choices=channel_choices, blank=False)
58
    time = models.DateTimeField(_('sent time'), auto_now_add=True)
59
    result = models.TextField(_('result'), blank=True)
60

    
61
    def __unicode__(self):
62
        return u'announce {id} sent via {channel} at {time}'.format(
63
                id=self.announce.id, channel=self.channel, time=self.time)
64

    
65
    class Meta:
66
        verbose_name = _('sent')
67
        ordering = ('-time',)
68
        unique_together = ('announce', 'channel')
69

    
70

    
71
class SubscriptionType(models.Model):
72
    subscription = models.ForeignKey('Subscription')
73
    identifier = models.CharField(_('identifier'), max_length=128, blank=True,
74
            help_text=_('ex.: email, mobile phone number, jabber id'))
75

    
76

    
77
class Subscription(models.Model):
78
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'),
79
                             blank=True, null=True)
80
    category = models.ForeignKey('Category', verbose_name=_('category'))
81
    class Meta:
82
        unique_together = ('user', 'category')
(6-6/11)