Project

General

Profile

Download (2.8 KB) Statistics
| Branch: | Tag: | Revision:

root / corbo / models.py @ b51a06ed

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
import channels
9

    
10
class Category(models.Model):
11
    name = models.CharField(max_length=64, blank=False, null=False)
12
    ctime = models.DateTimeField(auto_now_add=True)
13

    
14
    def __unicode__(self):
15
        return self.name
16

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

    
29
    def __unicode__(self):
30
        return u'{title} ({id}) at {mtime}'.format(title=self.title,
31
                                    id=self.id, mtime=self.mtime)
32

    
33
    def is_expired(self):
34
        if self.expiration_time:
35
            return self.expiration_time < timezone.now()
36
        return False
37

    
38
    def is_published(self):
39
        if self.is_expired():
40
            return False
41
        if self.publication_time:
42
            return self.publication_time <= timezone.now()
43
        return False
44

    
45
    class Meta:
46
        verbose_name = _('announce')
47
        ordering = ('-mtime',)
48

    
49
class Broadcast(models.Model):
50
    announce = models.ForeignKey(Announce, verbose_name=_('announce'))
51
    channel = models.CharField(_('channel'), max_length=32,
52
            choices=channels.get_channel_choices(), blank=False)
53
    time = models.DateTimeField(_('sent time'), auto_now_add=True)
54
    result = models.TextField(_('result'), blank=True)
55

    
56
    def __unicode__(self):
57
        return u'announce {id} sent via {channel} at {time}'.format(
58
                id=self.announce.id, channel=self.channel, time=self.time)
59

    
60
    class Meta:
61
        verbose_name = _('sent')
62
        ordering = ('-time',)
63
        unique_together = ('announce', 'channel')
64

    
65
class SubscriptionType(models.Model):
66
    subscription = models.ForeignKey('Subscription')
67
    identifier = models.CharField(_('identifier'), max_length=128, blank=True,
68
            help_text=_('ex.: email, mobile phone number, jabber id'))
69

    
70
class Subscription(models.Model):
71
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'),
72
                             blank=True, null=True)
73
    category = models.ForeignKey('Category', verbose_name=_('category'))
74
    class Meta:
75
        unique_together = ('user', 'category')
(5-5/11)