announce broadcasting channels added
This commit is contained in:
parent
59404bbf39
commit
46bbe5fe03
|
@ -0,0 +1,36 @@
|
|||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
def get_channel_choices(include=[], exclude=[]):
|
||||
for channel in HomepageChannel, SMSChannel, EmailChannel:
|
||||
if include and channel.identifier not in include:
|
||||
continue
|
||||
if exclude and channel.identifier in exclude:
|
||||
continue
|
||||
for identifier, display_name in channel.get_choices():
|
||||
yield (identifier, display_name)
|
||||
|
||||
class HomepageChannel(object):
|
||||
identifier = 'homepage'
|
||||
|
||||
@classmethod
|
||||
def get_choices(self):
|
||||
return (('homepage', _('Homepage')),)
|
||||
|
||||
class SMSChannel(object):
|
||||
|
||||
@classmethod
|
||||
def get_choices(self):
|
||||
return (('sms', _('SMS')),)
|
||||
|
||||
def send(self, announce):
|
||||
pass
|
||||
|
||||
class EmailChannel(object):
|
||||
identifier = 'email'
|
||||
|
||||
@classmethod
|
||||
def get_choices(self):
|
||||
return (('email', _('Email')),)
|
||||
|
||||
def send(self, announce):
|
||||
pass
|
|
@ -1,8 +1,13 @@
|
|||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .models import Announce, Category
|
||||
from .models import Announce, Category, Broadcast
|
||||
from .channels import get_channel_choices
|
||||
|
||||
class AnnounceForm(forms.ModelForm):
|
||||
transport_channel = forms.MultipleChoiceField(required=False,
|
||||
choices=get_channel_choices(),
|
||||
widget=forms.CheckboxSelectMultiple())
|
||||
|
||||
class Meta:
|
||||
model = Announce
|
||||
|
@ -13,6 +18,22 @@ class AnnounceForm(forms.ModelForm):
|
|||
'size': 8})
|
||||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(AnnounceForm, self).__init__(*args, **kwargs)
|
||||
self.fields['transport_channel'].initial = [b.channel for \
|
||||
b in self.instance.broadcast_set.all()]
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
instance = super(AnnounceForm, self).save(*args, **kwargs)
|
||||
if instance:
|
||||
channels = self.cleaned_data['transport_channel']
|
||||
for channel in channels:
|
||||
Broadcast.objects.get_or_create(announce=instance,
|
||||
channel=channel)
|
||||
self.instance.broadcast_set.exclude(announce=instance,
|
||||
channel__in=channels).delete()
|
||||
return instance
|
||||
|
||||
class CategoryForm(forms.ModelForm):
|
||||
class Meta:
|
||||
fields = ('name', )
|
||||
|
|
|
@ -5,6 +5,8 @@ from django.utils.translation import ugettext_lazy as _
|
|||
|
||||
from ckeditor.fields import RichTextField
|
||||
|
||||
import channels
|
||||
|
||||
class Category(models.Model):
|
||||
name = models.CharField(max_length=64, blank=False, null=False)
|
||||
ctime = models.DateTimeField(auto_now_add=True)
|
||||
|
@ -13,16 +15,16 @@ class Category(models.Model):
|
|||
return self.name
|
||||
|
||||
class Announce(models.Model):
|
||||
category = models.ForeignKey('Category', verbose_name=_('category'))
|
||||
title = models.CharField(_('title'), max_length=256,
|
||||
help_text=_('maximum 256 characters'))
|
||||
text = RichTextField(_('Content'))
|
||||
text = models.TextField(_('Content'))
|
||||
publication_time = models.DateTimeField(_('publication time'), blank=True,
|
||||
null=True)
|
||||
expiration_time = models.DateTimeField(_('Expires on'), blank=True,
|
||||
null=True)
|
||||
ctime = models.DateTimeField(_('creation time'), auto_now_add=True)
|
||||
mtime = models.DateTimeField(_('modification time'), auto_now=True)
|
||||
category = models.ForeignKey('Category', verbose_name=_('category'))
|
||||
|
||||
def __unicode__(self):
|
||||
return u'{title} ({id}) at {mtime}'.format(title=self.title,
|
||||
|
@ -44,6 +46,22 @@ class Announce(models.Model):
|
|||
verbose_name = _('announce')
|
||||
ordering = ('-mtime',)
|
||||
|
||||
class Broadcast(models.Model):
|
||||
announce = models.ForeignKey(Announce, verbose_name=_('announce'))
|
||||
channel = models.CharField(_('channel'), max_length=32,
|
||||
choices=channels.get_channel_choices(), blank=False)
|
||||
time = models.DateTimeField(_('sent time'), auto_now_add=True)
|
||||
result = models.TextField(_('result'), blank=True)
|
||||
|
||||
def __unicode__(self):
|
||||
return u'announce {id} sent via {channel} at {time}'.format(
|
||||
id=self.announce.id, channel=self.channel, time=self.time)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('sent')
|
||||
ordering = ('-time',)
|
||||
unique_together = ('announce', 'channel')
|
||||
|
||||
class SubscriptionType(models.Model):
|
||||
subscription = models.ForeignKey('Subscription')
|
||||
identifier = models.CharField(_('identifier'), max_length=128, blank=True,
|
||||
|
|
|
@ -277,3 +277,7 @@ li.datetime:after {
|
|||
form ul li label {
|
||||
display: block;
|
||||
}
|
||||
|
||||
#id_transport_channel li, #id_transport_channel label {
|
||||
display: inline;
|
||||
}
|
Reference in New Issue