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 ckeditor.fields import RichTextField
9 from taggit.managers import TaggableManager
11 from combo.data.models import CellBase
12 from combo.data.library import register_cell_class
14 from emissions.models import Episode, NewsItem
17 class SoundCell(CellBase):
18 soundfile = models.ForeignKey('emissions.SoundFile', null=True)
21 verbose_name = _('Sound')
23 def render(self, context):
24 tmpl = template.loader.get_template('panikombo/audio.html')
25 context['soundfile'] = self.soundfile
26 return tmpl.render(context)
28 def get_default_form_class(self):
29 from .forms import SoundCellForm
32 def get_included_items(self):
33 if not self.soundfile:
35 return [self.soundfile.episode]
37 def get_additional_label(self):
39 if self.soundfile.fragment:
40 return u'%s - %s - %s' % (
41 self.soundfile.episode.emission.title,
42 self.soundfile.episode.title,
46 self.soundfile.episode.emission.title,
47 self.soundfile.episode.title)
53 class EpisodeCell(CellBase):
54 episode = models.ForeignKey('emissions.Episode', null=True)
57 verbose_name = _('Episode')
59 def render(self, context):
60 tmpl = template.loader.get_template('panikombo/episode.html')
61 context['episode'] = self.episode
63 context['soundfile'] = self.episode.main_sound
64 return tmpl.render(context)
66 def get_included_items(self):
71 def get_default_form_class(self):
72 from .forms import EpisodeCellForm
73 return EpisodeCellForm
75 def get_additional_label(self):
78 self.episode.emission.title,
84 class EpisodeAutoSelectionCell(CellBase):
85 title = models.CharField(_('Title'), max_length=50, blank=True)
86 tags = TaggableManager(_('Tags'), blank=True)
87 category = models.ForeignKey('emissions.Category', null=True, blank=True)
90 verbose_name = _('Automatic Episode Selection')
92 def get_included_items(self):
93 episodes_queryset = Episode.objects.select_related()
95 episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id])
97 episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all())
98 return episodes_queryset
100 def render(self, context):
101 tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
102 context['title'] = self.title
104 episodes_queryset = self.get_included_items()
105 episodes_queryset = episodes_queryset.extra(select={
106 'first_diffusion': 'emissions_diffusion.datetime',
108 select_params=(False, True),
109 where=['''datetime = (SELECT MIN(datetime)
110 FROM emissions_diffusion
111 WHERE episode_id = emissions_episode.id)'''],
112 tables=['emissions_diffusion'],
113 ).order_by('-first_diffusion').distinct()
114 context['episodes'] = episodes_queryset
115 return tmpl.render(context)
117 def get_default_form_class(self):
118 from .forms import EpisodeAutoSelectionCellForm
119 return EpisodeAutoSelectionCellForm
121 def get_additional_label(self):
127 class NewsItemAutoSelectionCell(CellBase):
128 title = models.CharField(_('Title'), max_length=50, blank=True)
129 tags = TaggableManager(_('Tags'), blank=True)
130 future = models.BooleanField(_('Future Events Only'), default=True)
131 category = models.ForeignKey('emissions.NewsCategory',
132 verbose_name=_('Category'), null=True, blank=True)
135 verbose_name = _('Automatic Newsitem Selection')
137 def get_included_items(self):
138 newsitems_queryset = NewsItem.objects.select_related()
139 if self.tags.count():
140 newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all())
142 newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today())
144 newsitems_queryset = newsitems_queryset.filter(category=self.category)
145 newsitems_queryset = newsitems_queryset.order_by('-event_date', '-creation_timestamp')
146 return newsitems_queryset
148 def render(self, context):
149 tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html')
150 context['title'] = self.title
151 context['newsitems'] = self.get_included_items()
152 return tmpl.render(context)
154 def get_default_form_class(self):
155 from .forms import NewsItemAutoSelectionCellForm
156 return NewsItemAutoSelectionCellForm
158 def get_additional_label(self):
164 def get_topik_image_path(instance, filename):
165 return os.path.join('images', 'topik', instance.page.slug,
166 os.path.basename(filename))
168 class Topik(models.Model):
169 page = models.ForeignKey('data.Page')
170 image = models.ImageField(_('Image'),
171 upload_to=get_topik_image_path, max_length=250, null=True, blank=True)
173 # denormalized from Focus
174 got_focus = models.DateTimeField(default=None, null=True, blank=True)
175 has_focus = models.BooleanField(default=False)
177 def __unicode__(self):
179 return super(Topik, self).__unicode__()
180 return unicode(self.page)
183 class ItemTopik(models.Model):
184 newsitem = models.ForeignKey('emissions.NewsItem', verbose_name=_('News Item'),
185 null=True, blank=True)
186 episode = models.ForeignKey('emissions.Episode', verbose_name=_('Episode'),
187 null=True, blank=True)
188 topik = models.ForeignKey('Topik', verbose_name='Topik',
189 null=True, blank=True)
193 class TopikCell(CellBase):
194 topik = models.ForeignKey(Topik, null=True)
195 text = RichTextField(_('Text'), blank=True, null=True)
197 template_name = 'panikombo/topik-cell.html'
200 verbose_name = _('Topik')
202 def get_additional_label(self):
205 return self.topik.page.title