1 from datetime import date
4 from django import template
5 from django.db import models
6 from django.utils.translation import ugettext_lazy as _
8 from ckeditor.fields import RichTextField
9 from taggit.managers import TaggableManager
11 from combo.data.models import CellBase
12 from combo.data.library import register_cell_class
14 from emissions.models import Episode, NewsItem
17 class SoundCell(CellBase):
18 soundfile = models.ForeignKey('emissions.SoundFile', null=True)
21 verbose_name = _('Sound')
23 def render(self, context):
24 tmpl = template.loader.get_template('panikombo/audio.html')
25 context['soundfile'] = self.soundfile
26 return tmpl.render(context)
28 def get_default_form_class(self):
29 from .forms import SoundCellForm
32 def get_included_items(self):
33 if not self.soundfile:
35 return [self.soundfile.episode]
37 def get_additional_label(self):
39 if self.soundfile.fragment:
40 return u'%s - %s - %s' % (
41 self.soundfile.episode.emission.title,
42 self.soundfile.episode.title,
46 self.soundfile.episode.emission.title,
47 self.soundfile.episode.title)
53 class EpisodeCell(CellBase):
54 episode = models.ForeignKey('emissions.Episode', null=True)
57 verbose_name = _('Episode')
59 def render(self, context):
60 tmpl = template.loader.get_template('panikombo/episode.html')
61 context['episode'] = self.episode
63 context['soundfile'] = self.episode.main_sound
64 return tmpl.render(context)
66 def get_included_items(self):
71 def get_default_form_class(self):
72 from .forms import EpisodeCellForm
73 return EpisodeCellForm
75 def get_additional_label(self):
78 self.episode.emission.title,
84 class EpisodeAutoSelectionCell(CellBase):
85 title = models.CharField(_('Title'), max_length=50, blank=True)
86 tags = TaggableManager(_('Tags'), blank=True)
87 category = models.ForeignKey('emissions.Category', null=True, blank=True)
88 period = models.PositiveSmallIntegerField(
89 _('Period'), default=0,
90 choices=((0, _('All')),
96 verbose_name = _('Automatic Episode Selection')
98 def get_included_items(self):
99 episodes_queryset = Episode.objects.select_related()
101 episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id])
102 if self.tags.count():
103 episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all())
106 episodes_queryset = episodes_queryset.extra(
107 select={ 'first_diffusion': 'emissions_diffusion.datetime', },
108 select_params=(False, True),
109 where=['''datetime = (SELECT MIN(datetime) FROM emissions_diffusion
110 WHERE episode_id = emissions_episode.id)'''],
111 tables=['emissions_diffusion'])
112 elif self.period == 1:
113 episodes_queryset = episodes_queryset.extra(
114 select={ 'first_diffusion': 'emissions_diffusion.datetime', },
115 select_params=(False, True),
116 where=['''datetime = (SELECT MIN(datetime) FROM emissions_diffusion
117 WHERE episode_id = emissions_episode.id AND
118 emissions_diffusion.datetime >= CURRENT_TIMESTAMP)'''],
119 tables=['emissions_diffusion'])
120 elif self.period == 2:
121 episodes_queryset = episodes_queryset.extra(
122 select={ 'first_diffusion': 'emissions_diffusion.datetime', },
123 select_params=(False, True),
124 where=['''datetime = (SELECT MIN(datetime) FROM emissions_diffusion
125 WHERE episode_id = emissions_episode.id AND
126 emissions_diffusion.datetime < CURRENT_TIMESTAMP)'''],
127 tables=['emissions_diffusion'])
129 return episodes_queryset
131 def render(self, context):
132 tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
133 context['title'] = self.title
135 episodes_queryset = self.get_included_items()
136 episodes_queryset = episodes_queryset.order_by('-first_diffusion').distinct()
137 context['episodes'] = episodes_queryset
138 return tmpl.render(context)
140 def get_default_form_class(self):
141 from .forms import EpisodeAutoSelectionCellForm
142 return EpisodeAutoSelectionCellForm
144 def get_additional_label(self):
150 class NewsItemAutoSelectionCell(CellBase):
151 title = models.CharField(_('Title'), max_length=50, blank=True)
152 tags = TaggableManager(_('Tags'), blank=True)
153 future = models.BooleanField(_('Future Events Only'), default=True)
154 category = models.ForeignKey('emissions.NewsCategory',
155 verbose_name=_('Category'), null=True, blank=True)
158 verbose_name = _('Automatic Newsitem Selection')
160 def get_included_items(self):
161 newsitems_queryset = NewsItem.objects.select_related()
162 if self.tags.count():
163 newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all())
165 newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today())
167 newsitems_queryset = newsitems_queryset.filter(category=self.category)
168 newsitems_queryset = newsitems_queryset.order_by('-event_date', '-creation_timestamp')
169 return newsitems_queryset
171 def render(self, context):
172 tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html')
173 context['title'] = self.title
174 context['newsitems'] = self.get_included_items()
175 return tmpl.render(context)
177 def get_default_form_class(self):
178 from .forms import NewsItemAutoSelectionCellForm
179 return NewsItemAutoSelectionCellForm
181 def get_additional_label(self):
187 def get_topik_image_path(instance, filename):
188 return os.path.join('images', 'topik', instance.page.slug,
189 os.path.basename(filename))
191 class Topik(models.Model):
192 page = models.ForeignKey('data.Page')
193 image = models.ImageField(_('Image'),
194 upload_to=get_topik_image_path, max_length=250, null=True, blank=True)
196 # denormalized from Focus
197 got_focus = models.DateTimeField(default=None, null=True, blank=True)
198 has_focus = models.BooleanField(default=False)
200 def __unicode__(self):
202 return super(Topik, self).__unicode__()
203 return unicode(self.page)
206 class ItemTopik(models.Model):
207 newsitem = models.ForeignKey('emissions.NewsItem', verbose_name=_('News Item'),
208 null=True, blank=True)
209 episode = models.ForeignKey('emissions.Episode', verbose_name=_('Episode'),
210 null=True, blank=True)
211 topik = models.ForeignKey('Topik', verbose_name='Topik',
212 null=True, blank=True)
216 class TopikCell(CellBase):
217 topik = models.ForeignKey(Topik, null=True)
218 text = RichTextField(_('Text'), blank=True, null=True)
220 template_name = 'panikombo/topik-cell.html'
223 verbose_name = _('Topik')
225 def get_additional_label(self):
228 return self.topik.page.title