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
105 def get_additional_label(self):
111 class NewsItemAutoSelectionCell(CellBase):
112 title = models.CharField(_('Title'), max_length=50, blank=True)
113 tags = TaggableManager(_('Tags'), blank=True)
114 future = models.BooleanField(_('Future Events Only'), default=True)
117 verbose_name = _('Automatic Newsitem Selection')
119 def render(self, context):
120 tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html')
121 context['title'] = self.title
123 newsitems_queryset = NewsItem.objects.select_related()
124 if self.tags.count():
125 newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all())
127 newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today())
129 context['newsitems'] = newsitems_queryset
130 return tmpl.render(context)
132 def get_default_form_class(self):
133 from .forms import NewsItemAutoSelectionCellForm
134 return NewsItemAutoSelectionCellForm
136 def get_additional_label(self):