1 from datetime import date
3 from django import template
4 from django.db import models
5 from django.utils.translation import ugettext_lazy as _
7 from taggit.managers import TaggableManager
9 from combo.data.models import CellBase
10 from combo.data.library import register_cell_class
12 from emissions.models import Episode, NewsItem
15 class SoundCell(CellBase):
16 soundfile = models.ForeignKey('emissions.SoundFile', null=True)
19 verbose_name = _('Sound')
21 def render(self, context):
22 tmpl = template.loader.get_template('panikombo/audio.html')
23 context['soundfile'] = self.soundfile
24 return tmpl.render(context)
26 def get_default_form_class(self):
27 from .forms import SoundCellForm
30 def get_additional_label(self):
32 if self.soundfile.fragment:
33 return u'%s - %s - %s' % (
34 self.soundfile.episode.emission.title,
35 self.soundfile.episode.title,
39 self.soundfile.episode.emission.title,
40 self.soundfile.episode.title)
46 class EpisodeCell(CellBase):
47 episode = models.ForeignKey('emissions.Episode', null=True)
50 verbose_name = _('Episode')
52 def render(self, context):
53 tmpl = template.loader.get_template('panikombo/episode.html')
54 context['episode'] = self.episode
55 context['soundfile'] = self.episode.main_sound
56 return tmpl.render(context)
58 def get_default_form_class(self):
59 from .forms import EpisodeCellForm
60 return EpisodeCellForm
62 def get_additional_label(self):
65 self.episode.emission.title,
71 class EpisodeAutoSelectionCell(CellBase):
72 title = models.CharField(_('Title'), max_length=50, blank=True)
73 tags = TaggableManager(_('Tags'), blank=True)
74 category = models.ForeignKey('emissions.Category', null=True, blank=True)
77 verbose_name = _('Automatic Episode Selection')
79 def render(self, context):
80 tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
81 context['title'] = self.title
83 episodes_queryset = Episode.objects.select_related()
85 episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id])
87 episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all())
89 episodes_queryset = episodes_queryset.extra(select={
90 'first_diffusion': 'emissions_diffusion.datetime',
92 select_params=(False, True),
93 where=['''datetime = (SELECT MIN(datetime)
94 FROM emissions_diffusion
95 WHERE episode_id = emissions_episode.id)'''],
96 tables=['emissions_diffusion'],
97 ).order_by('-first_diffusion').distinct()
98 context['episodes'] = episodes_queryset
99 return tmpl.render(context)
101 def get_default_form_class(self):
102 from .forms import EpisodeAutoSelectionCellForm
103 return EpisodeAutoSelectionCellForm
107 class NewsItemAutoSelectionCell(CellBase):
108 title = models.CharField(_('Title'), max_length=50, blank=True)
109 tags = TaggableManager(_('Tags'), blank=True)
110 future = models.BooleanField(_('Future Events Only'), default=True)
113 verbose_name = _('Automatic Newsitem Selection')
115 def render(self, context):
116 tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html')
117 context['title'] = self.title
119 newsitems_queryset = NewsItem.objects.select_related()
120 if self.tags.count():
121 newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all())
123 newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today())
125 context['newsitems'] = newsitems_queryset
126 return tmpl.render(context)
128 def get_default_form_class(self):
129 from .forms import NewsItemAutoSelectionCellForm
130 return NewsItemAutoSelectionCellForm