Project

General

Profile

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

root / corbo / models.py @ ef0dcf1e

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

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

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

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

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

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

    
50

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

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

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

    
67

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

    
73

    
74
class Subscription(models.Model):
75
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'),
76
                             blank=True, null=True)
77
    category = models.ForeignKey('Category', verbose_name=_('category'))
78
    class Meta:
79
        unique_together = ('user', 'category')
(5-5/11)