From da4eae1f4ca9bb021c42c4d659bce604dbdf3f74 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 25 May 2015 14:40:55 +0200 Subject: [PATCH] add newsitem auto selection --- panikombo/forms.py | 10 ++++- .../0004_newsitemautoselectioncell.py | 37 +++++++++++++++++++ panikombo/models.py | 33 ++++++++++++++++- 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 panikombo/migrations/0004_newsitemautoselectioncell.py diff --git a/panikombo/forms.py b/panikombo/forms.py index d9f60ec..b4b4ef7 100644 --- a/panikombo/forms.py +++ b/panikombo/forms.py @@ -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 index 0000000..f8efc3b --- /dev/null +++ b/panikombo/migrations/0004_newsitemautoselectioncell.py @@ -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,), + ), + ] diff --git a/panikombo/models.py b/panikombo/models.py index 0c63576..fd71709 100644 --- a/panikombo/models.py +++ b/panikombo/models.py @@ -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 -- 2.39.2