]> git.0d.be Git - django-panik-combo.git/blobdiff - panikombo/models.py
add newsitem auto selection
[django-panik-combo.git] / panikombo / models.py
index 0c63576a74ea833dee46f3b9164ade3266c49e44..fd717090120082638d05a1ac878690a9272ea7d2 100644 (file)
@@ -1,3 +1,5 @@
+from datetime import date
+
 from django import template
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
@@ -7,7 +9,7 @@ from taggit.managers import TaggableManager
 from combo.data.models import CellBase
 from combo.data.library import register_cell_class
 
-from emissions.models import Episode
+from emissions.models import Episode, NewsItem
 
 @register_cell_class
 class SoundCell(CellBase):
@@ -81,7 +83,7 @@ class EpisodeAutoSelectionCell(CellBase):
         episodes_queryset = Episode.objects.select_related()
         if self.category:
             episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id])
-        if self.tags:
+        if self.tags.count():
             episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all())
 
         episodes_queryset = episodes_queryset.extra(select={
@@ -99,3 +101,30 @@ class EpisodeAutoSelectionCell(CellBase):
     def get_default_form_class(self):
         from .forms import EpisodeAutoSelectionCellForm
         return EpisodeAutoSelectionCellForm
+
+
+@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)
+
+    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
+
+        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())
+
+        context['newsitems'] = newsitems_queryset
+        return tmpl.render(context)
+
+    def get_default_form_class(self):
+        from .forms import NewsItemAutoSelectionCellForm
+        return NewsItemAutoSelectionCellForm