add newsitem auto selection
authorFrédéric Péters <fpeters@0d.be>
Mon, 25 May 2015 12:40:55 +0000 (14:40 +0200)
committerFrédéric Péters <fpeters@0d.be>
Mon, 25 May 2015 12:40:55 +0000 (14:40 +0200)
panikombo/forms.py
panikombo/migrations/0004_newsitemautoselectioncell.py [new file with mode: 0644]
panikombo/models.py

index d9f60ec..b4b4ef7 100644 (file)
@@ -4,7 +4,8 @@ from taggit.forms import TagWidget
 
 from emissions.models import SoundFile, Episode
 
-from .models import SoundCell, EpisodeCell, EpisodeAutoSelectionCell
+from .models import (SoundCell, EpisodeCell, EpisodeAutoSelectionCell,
+        NewsItemAutoSelectionCell)
 from .views import soundfiles, episodes
 
 class SoundFileWidget(HeavySelect2Widget):
@@ -52,3 +53,10 @@ class EpisodeAutoSelectionCellForm(forms.ModelForm):
         model = EpisodeAutoSelectionCell
         fields = ('title', 'tags', 'category')
         widgets = {'tags': TagWidget()}
+
+
+class NewsItemAutoSelectionCellForm(forms.ModelForm):
+    class Meta:
+        model = NewsItemAutoSelectionCell
+        fields = ('title', 'tags', 'future')
+        widgets = {'tags': TagWidget()}
diff --git a/panikombo/migrations/0004_newsitemautoselectioncell.py b/panikombo/migrations/0004_newsitemautoselectioncell.py
new file mode 100644 (file)
index 0000000..f8efc3b
--- /dev/null
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+import taggit.managers
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('auth', '0001_initial'),
+        ('data', '0005_auto_20150226_0903'),
+        ('taggit', '0001_initial'),
+        ('panikombo', '0003_episodeautoselectioncell'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='NewsItemAutoSelectionCell',
+            fields=[
+                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+                ('placeholder', models.CharField(max_length=20)),
+                ('order', models.PositiveIntegerField()),
+                ('slug', models.SlugField(verbose_name='Slug', blank=True)),
+                ('public', models.BooleanField(default=True, verbose_name='Public')),
+                ('title', models.CharField(max_length=50, verbose_name='Title', blank=True)),
+                ('future', models.BooleanField(default=True, verbose_name='Future Events Only')),
+                ('groups', models.ManyToManyField(to='auth.Group', verbose_name='Groups', blank=True)),
+                ('page', models.ForeignKey(to='data.Page')),
+                ('tags', taggit.managers.TaggableManager(to='taggit.Tag', through='taggit.TaggedItem', blank=True, help_text='A comma-separated list of tags.', verbose_name='Tags')),
+            ],
+            options={
+                'verbose_name': 'Automatic Newsitem Selection',
+            },
+            bases=(models.Model,),
+        ),
+    ]
index 0c63576..fd71709 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