1 from datetime import date
4 from django import template
5 from django.db import models
6 from django.utils.translation import ugettext_lazy as _
8 from taggit.managers import TaggableManager
10 from combo.data.models import CellBase
11 from combo.data.library import register_cell_class
13 from emissions.models import Episode, NewsItem
16 class SoundCell(CellBase):
17 soundfile = models.ForeignKey('emissions.SoundFile', null=True)
20 verbose_name = _('Sound')
22 def render(self, context):
23 tmpl = template.loader.get_template('panikombo/audio.html')
24 context['soundfile'] = self.soundfile
25 return tmpl.render(context)
27 def get_default_form_class(self):
28 from .forms import SoundCellForm
31 def get_additional_label(self):
33 if self.soundfile.fragment:
34 return u'%s - %s - %s' % (
35 self.soundfile.episode.emission.title,
36 self.soundfile.episode.title,
40 self.soundfile.episode.emission.title,
41 self.soundfile.episode.title)
47 class EpisodeCell(CellBase):
48 episode = models.ForeignKey('emissions.Episode', null=True)
51 verbose_name = _('Episode')
53 def render(self, context):
54 tmpl = template.loader.get_template('panikombo/episode.html')
55 context['episode'] = self.episode
56 context['soundfile'] = self.episode.main_sound
57 return tmpl.render(context)
59 def get_default_form_class(self):
60 from .forms import EpisodeCellForm
61 return EpisodeCellForm
63 def get_additional_label(self):
66 self.episode.emission.title,
72 class EpisodeAutoSelectionCell(CellBase):
73 title = models.CharField(_('Title'), max_length=50, blank=True)
74 tags = TaggableManager(_('Tags'), blank=True)
75 category = models.ForeignKey('emissions.Category', null=True, blank=True)
78 verbose_name = _('Automatic Episode Selection')
80 def render(self, context):
81 tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
82 context['title'] = self.title
84 episodes_queryset = Episode.objects.select_related()
86 episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id])
88 episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all())
90 episodes_queryset = episodes_queryset.extra(select={
91 'first_diffusion': 'emissions_diffusion.datetime',
93 select_params=(False, True),
94 where=['''datetime = (SELECT MIN(datetime)
95 FROM emissions_diffusion
96 WHERE episode_id = emissions_episode.id)'''],
97 tables=['emissions_diffusion'],
98 ).order_by('-first_diffusion').distinct()
99 context['episodes'] = episodes_queryset
100 return tmpl.render(context)
102 def get_default_form_class(self):
103 from .forms import EpisodeAutoSelectionCellForm
104 return EpisodeAutoSelectionCellForm
106 def get_additional_label(self):
112 class NewsItemAutoSelectionCell(CellBase):
113 title = models.CharField(_('Title'), max_length=50, blank=True)
114 tags = TaggableManager(_('Tags'), blank=True)
115 future = models.BooleanField(_('Future Events Only'), default=True)
118 verbose_name = _('Automatic Newsitem Selection')
120 def render(self, context):
121 tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html')
122 context['title'] = self.title
124 newsitems_queryset = NewsItem.objects.select_related()
125 if self.tags.count():
126 newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all())
128 newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today())
130 context['newsitems'] = newsitems_queryset
131 return tmpl.render(context)
133 def get_default_form_class(self):
134 from .forms import NewsItemAutoSelectionCellForm
135 return NewsItemAutoSelectionCellForm
137 def get_additional_label(self):
143 def get_topik_image_path(instance, filename):
144 return os.path.join('images', 'topik', instance.page.slug,
145 os.path.basename(filename))
147 class Topik(models.Model):
148 page = models.ForeignKey('data.Page')
149 image = models.ImageField(_('Image'),
150 upload_to=get_topik_image_path, max_length=250, null=True, blank=True)
152 # denormalized from Focus
153 got_focus = models.DateTimeField(default=None, null=True, blank=True)
154 has_focus = models.BooleanField(default=False)