4a6ab480d0fa9658b2656f21000278700b7b2b5c
[django-panik-combo.git] / panikombo / models.py
1 from datetime import date
2
3 from django import template
4 from django.db import models
5 from django.utils.translation import ugettext_lazy as _
6
7 from taggit.managers import TaggableManager
8
9 from combo.data.models import CellBase
10 from combo.data.library import register_cell_class
11
12 from emissions.models import Episode, NewsItem
13
14 @register_cell_class
15 class SoundCell(CellBase):
16     soundfile = models.ForeignKey('emissions.SoundFile', null=True)
17
18     class Meta:
19         verbose_name = _('Sound')
20
21     def render(self, context):
22         tmpl = template.loader.get_template('panikombo/audio.html')
23         context['soundfile'] = self.soundfile
24         return tmpl.render(context)
25
26     def get_default_form_class(self):
27         from .forms import SoundCellForm
28         return SoundCellForm
29
30     def get_additional_label(self):
31         if self.soundfile:
32             if self.soundfile.fragment:
33                 return u'%s - %s - %s' % (
34                         self.soundfile.episode.emission.title,
35                         self.soundfile.episode.title,
36                         self.soundfile.title)
37             else:
38                 return u'%s - %s' % (
39                         self.soundfile.episode.emission.title,
40                         self.soundfile.episode.title)
41
42         return ''
43
44
45 @register_cell_class
46 class EpisodeCell(CellBase):
47     episode = models.ForeignKey('emissions.Episode', null=True)
48
49     class Meta:
50         verbose_name = _('Episode')
51
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)
57
58     def get_default_form_class(self):
59         from .forms import EpisodeCellForm
60         return EpisodeCellForm
61
62     def get_additional_label(self):
63         if self.episode:
64             return u'%s - %s' % (
65                         self.episode.emission.title,
66                         self.episode.title)
67         return ''
68
69
70 @register_cell_class
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)
75
76     class Meta:
77         verbose_name = _('Automatic Episode Selection')
78
79     def render(self, context):
80         tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
81         context['title'] = self.title
82
83         episodes_queryset = Episode.objects.select_related()
84         if self.category:
85             episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id])
86         if self.tags.count():
87             episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all())
88
89         episodes_queryset = episodes_queryset.extra(select={
90                         'first_diffusion': 'emissions_diffusion.datetime',
91                         },
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)
100
101     def get_default_form_class(self):
102         from .forms import EpisodeAutoSelectionCellForm
103         return EpisodeAutoSelectionCellForm
104
105     def get_additional_label(self):
106         if self.title:
107             return self.title
108         return ''
109
110 @register_cell_class
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)
115
116     class Meta:
117         verbose_name = _('Automatic Newsitem Selection')
118
119     def render(self, context):
120         tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html')
121         context['title'] = self.title
122
123         newsitems_queryset = NewsItem.objects.select_related()
124         if self.tags.count():
125             newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all())
126         if self.future:
127             newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today())
128
129         context['newsitems'] = newsitems_queryset
130         return tmpl.render(context)
131
132     def get_default_form_class(self):
133         from .forms import NewsItemAutoSelectionCellForm
134         return NewsItemAutoSelectionCellForm
135
136     def get_additional_label(self):
137         if self.title:
138             return self.title
139         return ''