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
62 context['soundfile'] = self.episode.main_sound
63 return tmpl.render(context)
65 def get_included_items(self):
70 def get_default_form_class(self):
71 from .forms import EpisodeCellForm
72 return EpisodeCellForm
74 def get_additional_label(self):
77 self.episode.emission.title,
83 class EpisodeAutoSelectionCell(CellBase):
84 title = models.CharField(_('Title'), max_length=50, blank=True)
85 tags = TaggableManager(_('Tags'), blank=True)
86 category = models.ForeignKey('emissions.Category', null=True, blank=True)
89 verbose_name = _('Automatic Episode Selection')
91 def get_included_items(self):
92 episodes_queryset = Episode.objects.select_related()
94 episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id])
96 episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all())
97 return episodes_queryset
99 def render(self, context):
100 tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
101 context['title'] = self.title
103 episodes_queryset = self.get_included_items()
104 episodes_queryset = episodes_queryset.extra(select={
105 'first_diffusion': 'emissions_diffusion.datetime',
107 select_params=(False, True),
108 where=['''datetime = (SELECT MIN(datetime)
109 FROM emissions_diffusion
110 WHERE episode_id = emissions_episode.id)'''],
111 tables=['emissions_diffusion'],
112 ).order_by('-first_diffusion').distinct()
113 context['episodes'] = episodes_queryset
114 return tmpl.render(context)
116 def get_default_form_class(self):
117 from .forms import EpisodeAutoSelectionCellForm
118 return EpisodeAutoSelectionCellForm
120 def get_additional_label(self):
126 class NewsItemAutoSelectionCell(CellBase):
127 title = models.CharField(_('Title'), max_length=50, blank=True)
128 tags = TaggableManager(_('Tags'), blank=True)
129 future = models.BooleanField(_('Future Events Only'), default=True)
130 category = models.ForeignKey('emissions.NewsCategory',
131 verbose_name=_('Category'), null=True, blank=True)
134 verbose_name = _('Automatic Newsitem Selection')
136 def get_included_items(self):
137 newsitems_queryset = NewsItem.objects.select_related()
138 if self.tags.count():
139 newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all())
141 newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today())
143 newsitems_queryset = newsitems_queryset.filter(category=self.category)
144 newsitems_queryset = newsitems_queryset.order_by('-event_date', '-creation_timestamp')
145 return newsitems_queryset
147 def render(self, context):
148 tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html')
149 context['title'] = self.title
150 context['newsitems'] = self.get_included_items()
151 return tmpl.render(context)
153 def get_default_form_class(self):
154 from .forms import NewsItemAutoSelectionCellForm
155 return NewsItemAutoSelectionCellForm
157 def get_additional_label(self):
163 def get_topik_image_path(instance, filename):
164 return os.path.join('images', 'topik', instance.page.slug,
165 os.path.basename(filename))
167 class Topik(models.Model):
168 page = models.ForeignKey('data.Page')
169 image = models.ImageField(_('Image'),
170 upload_to=get_topik_image_path, max_length=250, null=True, blank=True)
172 # denormalized from Focus
173 got_focus = models.DateTimeField(default=None, null=True, blank=True)
174 has_focus = models.BooleanField(default=False)
176 def __unicode__(self):
178 return super(Topik, self).__unicode__()
179 return unicode(self.page)
182 class ItemTopik(models.Model):
183 newsitem = models.ForeignKey('emissions.NewsItem', verbose_name=_('News Item'),
184 null=True, blank=True)
185 episode = models.ForeignKey('emissions.Episode', verbose_name=_('Episode'),
186 null=True, blank=True)
187 topik = models.ForeignKey('Topik', verbose_name='Topik',
188 null=True, blank=True)
192 class TopikCell(CellBase):
193 topik = models.ForeignKey(Topik, null=True)
194 text = RichTextField(_('Text'), blank=True, null=True)
196 template_name = 'panikombo/topik-cell.html'
199 verbose_name = _('Topik')
201 def get_additional_label(self):
204 return self.topik.page.title