add episode auto selection cell
[django-panik-combo.git] / panikombo / models.py
1 from django import template
2 from django.db import models
3 from django.utils.translation import ugettext_lazy as _
4
5 from taggit.managers import TaggableManager
6
7 from combo.data.models import CellBase
8 from combo.data.library import register_cell_class
9
10 from emissions.models import Episode
11
12 @register_cell_class
13 class SoundCell(CellBase):
14     soundfile = models.ForeignKey('emissions.SoundFile', null=True)
15
16     class Meta:
17         verbose_name = _('Sound')
18
19     def render(self, context):
20         tmpl = template.loader.get_template('panikombo/audio.html')
21         context['soundfile'] = self.soundfile
22         return tmpl.render(context)
23
24     def get_default_form_class(self):
25         from .forms import SoundCellForm
26         return SoundCellForm
27
28     def get_additional_label(self):
29         if self.soundfile:
30             if self.soundfile.fragment:
31                 return u'%s - %s - %s' % (
32                         self.soundfile.episode.emission.title,
33                         self.soundfile.episode.title,
34                         self.soundfile.title)
35             else:
36                 return u'%s - %s' % (
37                         self.soundfile.episode.emission.title,
38                         self.soundfile.episode.title)
39
40         return ''
41
42
43 @register_cell_class
44 class EpisodeCell(CellBase):
45     episode = models.ForeignKey('emissions.Episode', null=True)
46
47     class Meta:
48         verbose_name = _('Episode')
49
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)
55
56     def get_default_form_class(self):
57         from .forms import EpisodeCellForm
58         return EpisodeCellForm
59
60     def get_additional_label(self):
61         if self.episode:
62             return u'%s - %s' % (
63                         self.episode.emission.title,
64                         self.episode.title)
65         return ''
66
67
68 @register_cell_class
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)
73
74     class Meta:
75         verbose_name = _('Automatic Episode Selection')
76
77     def render(self, context):
78         tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
79         context['title'] = self.title
80
81         episodes_queryset = Episode.objects.select_related()
82         if self.category:
83             episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id])
84         if self.tags:
85             episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all())
86
87         episodes_queryset = episodes_queryset.extra(select={
88                         'first_diffusion': 'emissions_diffusion.datetime',
89                         },
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)
98
99     def get_default_form_class(self):
100         from .forms import EpisodeAutoSelectionCellForm
101         return EpisodeAutoSelectionCellForm