From a53294a17b610fa38589d1ebe223ddad42d07f11 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sun, 15 Oct 2017 10:32:08 +0200 Subject: [PATCH] add additional "and_tags" field to auto selection, for ANDing criterias --- panikombo/forms.py | 4 ++-- .../migrations/0012_auto_20171015_1027.py | 24 +++++++++++++++++++ panikombo/models.py | 15 ++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 panikombo/migrations/0012_auto_20171015_1027.py diff --git a/panikombo/forms.py b/panikombo/forms.py index 6e499e0..8cb67c5 100644 --- a/panikombo/forms.py +++ b/panikombo/forms.py @@ -53,14 +53,14 @@ class EpisodeCellForm(forms.ModelForm): class EpisodeAutoSelectionCellForm(forms.ModelForm): class Meta: model = EpisodeAutoSelectionCell - fields = ('title', 'tags', 'category', 'period') + fields = ('title', 'tags', 'and_tags', 'category', 'period') widgets = {'tags': TagWidget()} class NewsItemAutoSelectionCellForm(forms.ModelForm): class Meta: model = NewsItemAutoSelectionCell - fields = ('title', 'tags', 'category', 'future') + fields = ('title', 'tags', 'and_tags', 'category', 'future') widgets = {'tags': TagWidget()} diff --git a/panikombo/migrations/0012_auto_20171015_1027.py b/panikombo/migrations/0012_auto_20171015_1027.py new file mode 100644 index 0000000..73beee0 --- /dev/null +++ b/panikombo/migrations/0012_auto_20171015_1027.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('panikombo', '0011_auto_20170418_1154'), + ] + + operations = [ + migrations.AddField( + model_name='episodeautoselectioncell', + name='and_tags', + field=models.CharField(max_length=100, verbose_name='And Tags', blank=True), + ), + migrations.AddField( + model_name='newsitemautoselectioncell', + name='and_tags', + field=models.CharField(max_length=100, verbose_name='And Tags', blank=True), + ), + ] diff --git a/panikombo/models.py b/panikombo/models.py index 5588962..350f77e 100644 --- a/panikombo/models.py +++ b/panikombo/models.py @@ -6,7 +6,9 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ from ckeditor.fields import RichTextField +from taggit.models import Tag from taggit.managers import TaggableManager +from taggit.utils import parse_tags from combo.data.models import CellBase from combo.data.library import register_cell_class @@ -80,10 +82,16 @@ class EpisodeCell(CellBase): return '' +def get_parsed_tags(tagstring): + tags = parse_tags(tagstring) + return [x for x in Tag.objects.filter(name__in=tags)] + + @register_cell_class class EpisodeAutoSelectionCell(CellBase): title = models.CharField(_('Title'), max_length=50, blank=True) tags = TaggableManager(_('Tags'), blank=True) + and_tags = models.CharField(_('And Tags'), max_length=100, blank=True) category = models.ForeignKey('emissions.Category', null=True, blank=True) period = models.PositiveSmallIntegerField( _('Period'), default=0, @@ -101,6 +109,9 @@ class EpisodeAutoSelectionCell(CellBase): 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()) + if self.and_tags: + and_tags = get_parsed_tags(self.and_tags) + episodes_queryset = episodes_queryset.filter(tags__in=and_tags) if self.period == 0: episodes_queryset = episodes_queryset.extra( @@ -153,6 +164,7 @@ class EpisodeAutoSelectionCell(CellBase): class NewsItemAutoSelectionCell(CellBase): title = models.CharField(_('Title'), max_length=50, blank=True) tags = TaggableManager(_('Tags'), blank=True) + and_tags = models.CharField(_('And Tags'), max_length=100, blank=True) future = models.BooleanField(_('Future Events Only'), default=True) category = models.ForeignKey('emissions.NewsCategory', verbose_name=_('Category'), null=True, blank=True) @@ -164,6 +176,9 @@ class NewsItemAutoSelectionCell(CellBase): newsitems_queryset = NewsItem.objects.select_related() if self.tags.count(): newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all()) + if self.and_tags: + and_tags = get_parsed_tags(self.and_tags) + newsitems_queryset = newsitems_queryset.filter(tags__in=and_tags) if self.future: newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today()) if self.category: -- 2.39.2