]> git.0d.be Git - django-panik-combo.git/blobdiff - panikombo/models.py
don't try to get main sound if episode is not defined
[django-panik-combo.git] / panikombo / models.py
index fd717090120082638d05a1ac878690a9272ea7d2..5c294bc667bbdbd85ec8f47010ed8aeaac11d08c 100644 (file)
@@ -1,9 +1,11 @@
 from datetime import date
+import os
 
 from django import template
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
 
+from ckeditor.fields import RichTextField
 from taggit.managers import TaggableManager
 
 from combo.data.models import CellBase
@@ -27,6 +29,11 @@ class SoundCell(CellBase):
         from .forms import SoundCellForm
         return SoundCellForm
 
+    def get_included_items(self):
+        if not self.soundfile:
+            return []
+        return [self.soundfile.episode]
+
     def get_additional_label(self):
         if self.soundfile:
             if self.soundfile.fragment:
@@ -52,9 +59,15 @@ class EpisodeCell(CellBase):
     def render(self, context):
         tmpl = template.loader.get_template('panikombo/episode.html')
         context['episode'] = self.episode
-        context['soundfile'] = self.episode.main_sound
+        if self.episode:
+            context['soundfile'] = self.episode.main_sound
         return tmpl.render(context)
 
+    def get_included_items(self):
+        if not self.episode:
+            return []
+        return [self.episode]
+
     def get_default_form_class(self):
         from .forms import EpisodeCellForm
         return EpisodeCellForm
@@ -76,16 +89,19 @@ class EpisodeAutoSelectionCell(CellBase):
     class Meta:
         verbose_name = _('Automatic Episode Selection')
 
-    def render(self, context):
-        tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
-        context['title'] = self.title
-
+    def get_included_items(self):
         episodes_queryset = Episode.objects.select_related()
         if self.category:
             episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id])
         if self.tags.count():
             episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all())
+        return episodes_queryset
+
+    def render(self, context):
+        tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
+        context['title'] = self.title
 
+        episodes_queryset = self.get_included_items()
         episodes_queryset = episodes_queryset.extra(select={
                         'first_diffusion': 'emissions_diffusion.datetime',
                         },
@@ -102,29 +118,88 @@ class EpisodeAutoSelectionCell(CellBase):
         from .forms import EpisodeAutoSelectionCellForm
         return EpisodeAutoSelectionCellForm
 
+    def get_additional_label(self):
+        if self.title:
+            return self.title
+        return ''
 
 @register_cell_class
 class NewsItemAutoSelectionCell(CellBase):
     title = models.CharField(_('Title'), max_length=50, blank=True)
     tags = TaggableManager(_('Tags'), blank=True)
     future = models.BooleanField(_('Future Events Only'), default=True)
+    category = models.ForeignKey('emissions.NewsCategory',
+            verbose_name=_('Category'), null=True, blank=True)
 
     class Meta:
         verbose_name = _('Automatic Newsitem Selection')
 
-    def render(self, context):
-        tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html')
-        context['title'] = self.title
-
+    def get_included_items(self):
         newsitems_queryset = NewsItem.objects.select_related()
         if self.tags.count():
             newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all())
         if self.future:
             newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today())
+        if self.category:
+            newsitems_queryset = newsitems_queryset.filter(category=self.category)
+        newsitems_queryset = newsitems_queryset.order_by('-event_date', '-creation_timestamp')
+        return newsitems_queryset
 
-        context['newsitems'] = newsitems_queryset
+    def render(self, context):
+        tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html')
+        context['title'] = self.title
+        context['newsitems'] = self.get_included_items()
         return tmpl.render(context)
 
     def get_default_form_class(self):
         from .forms import NewsItemAutoSelectionCellForm
         return NewsItemAutoSelectionCellForm
+
+    def get_additional_label(self):
+        if self.title:
+            return self.title
+        return ''
+
+
+def get_topik_image_path(instance, filename):
+    return os.path.join('images', 'topik', instance.page.slug,
+            os.path.basename(filename))
+
+class Topik(models.Model):
+    page = models.ForeignKey('data.Page')
+    image = models.ImageField(_('Image'),
+            upload_to=get_topik_image_path, max_length=250, null=True, blank=True)
+
+    # denormalized from Focus
+    got_focus = models.DateTimeField(default=None, null=True, blank=True)
+    has_focus = models.BooleanField(default=False)
+
+    def __unicode__(self):
+        if not self.page:
+            return super(Topik, self).__unicode__()
+        return unicode(self.page)
+
+
+class ItemTopik(models.Model):
+    newsitem = models.ForeignKey('emissions.NewsItem', verbose_name=_('News Item'),
+            null=True, blank=True)
+    episode = models.ForeignKey('emissions.Episode', verbose_name=_('Episode'),
+            null=True, blank=True)
+    topik = models.ForeignKey('Topik', verbose_name='Topik',
+            null=True, blank=True)
+
+
+@register_cell_class
+class TopikCell(CellBase):
+    topik = models.ForeignKey(Topik, null=True)
+    text = RichTextField(_('Text'), blank=True, null=True)
+
+    template_name = 'panikombo/topik-cell.html'
+
+    class Meta:
+        verbose_name = _('Topik')
+
+    def get_additional_label(self):
+        if not self.topik:
+            return ''
+        return self.topik.page.title