1 from django import template
2 from django.db import models
3 from django.utils.translation import ugettext_lazy as _
5 from taggit.managers import TaggableManager
7 from combo.data.models import CellBase
8 from combo.data.library import register_cell_class
10 from emissions.models import Episode
13 class SoundCell(CellBase):
14 soundfile = models.ForeignKey('emissions.SoundFile', null=True)
17 verbose_name = _('Sound')
19 def render(self, context):
20 tmpl = template.loader.get_template('panikombo/audio.html')
21 context['soundfile'] = self.soundfile
22 return tmpl.render(context)
24 def get_default_form_class(self):
25 from .forms import SoundCellForm
28 def get_additional_label(self):
30 if self.soundfile.fragment:
31 return u'%s - %s - %s' % (
32 self.soundfile.episode.emission.title,
33 self.soundfile.episode.title,
37 self.soundfile.episode.emission.title,
38 self.soundfile.episode.title)
44 class EpisodeCell(CellBase):
45 episode = models.ForeignKey('emissions.Episode', null=True)
48 verbose_name = _('Episode')
50 def render(self, context):
51 tmpl = template.loader.get_template('panikombo/episode.html')
52 context['episode'] = self.episode
53 context['soundfile'] = self.episode.main_sound
54 return tmpl.render(context)
56 def get_default_form_class(self):
57 from .forms import EpisodeCellForm
58 return EpisodeCellForm
60 def get_additional_label(self):
63 self.episode.emission.title,
69 class EpisodeAutoSelectionCell(CellBase):
70 title = models.CharField(_('Title'), max_length=50, blank=True)
71 tags = TaggableManager(_('Tags'), blank=True)
72 category = models.ForeignKey('emissions.Category', null=True, blank=True)
75 verbose_name = _('Automatic Episode Selection')
77 def render(self, context):
78 tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
79 context['title'] = self.title
81 episodes_queryset = Episode.objects.select_related()
83 episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id])
85 episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all())
87 episodes_queryset = episodes_queryset.extra(select={
88 'first_diffusion': 'emissions_diffusion.datetime',
90 select_params=(False, True),
91 where=['''datetime = (SELECT MIN(datetime)
92 FROM emissions_diffusion
93 WHERE episode_id = emissions_episode.id)'''],
94 tables=['emissions_diffusion'],
95 ).order_by('-first_diffusion').distinct()
96 context['episodes'] = episodes_queryset
97 return tmpl.render(context)
99 def get_default_form_class(self):
100 from .forms import EpisodeAutoSelectionCellForm
101 return EpisodeAutoSelectionCellForm