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
|
class Category(models.Model):
|
9
|
name = models.CharField(max_length=64, blank=False, null=False)
|
10
|
ctime = models.DateTimeField(auto_now_add=True)
|
11
|
|
12
|
def __unicode__(self):
|
13
|
return self.name
|
14
|
|
15
|
class Announce(models.Model):
|
16
|
title = models.CharField(_('title'), max_length=256,
|
17
|
help_text=_('maximum 256 characters'))
|
18
|
text = RichTextField(_('Content'))
|
19
|
publication_time = models.DateTimeField(_('publication time'), blank=True,
|
20
|
null=True, default=timezone.now)
|
21
|
expiration_time = models.DateTimeField(_('expiration time'), blank=True,
|
22
|
null=True)
|
23
|
ctime = models.DateTimeField(_('creation time'), auto_now_add=True)
|
24
|
mtime = models.DateTimeField(_('modification time'), auto_now=True)
|
25
|
category = models.ForeignKey('Category', verbose_name=_('category'))
|
26
|
|
27
|
def __unicode__(self):
|
28
|
return u'{title} ({id}) at {mtime}'.format(title=self.title,
|
29
|
id=self.id, mtime=self.mtime)
|
30
|
|
31
|
def is_published(self):
|
32
|
if self.publication_time:
|
33
|
if self.expiration_time:
|
34
|
return self.publication_time <= timezone.now() < self.expiration_time
|
35
|
return self.publication_time <= timezone.now()
|
36
|
|
37
|
def is_expired(self):
|
38
|
if self.expiration_time:
|
39
|
return self.expiration_time > timezone.now()
|
40
|
|
41
|
def is_not_published(self):
|
42
|
return not self.publication_time
|
43
|
|
44
|
class Meta:
|
45
|
verbose_name = _('announce')
|
46
|
ordering = ('-mtime',)
|
47
|
|
48
|
class SubscriptionType(models.Model):
|
49
|
subscription = models.ForeignKey('Subscription')
|
50
|
identifier = models.CharField(_('identifier'), max_length=128, blank=True,
|
51
|
help_text=_('ex.: email, mobile phone number, jabber id'))
|
52
|
|
53
|
class Subscription(models.Model):
|
54
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'),
|
55
|
blank=True, null=True)
|
56
|
category = models.ForeignKey('Category', verbose_name=_('category'))
|
57
|
class Meta:
|
58
|
unique_together = ('user', 'category')
|