add additional "and_tags" field to auto selection, for ANDing criterias
authorFrédéric Péters <fpeters@0d.be>
Sun, 15 Oct 2017 08:32:08 +0000 (10:32 +0200)
committerFrédéric Péters <fpeters@0d.be>
Sun, 15 Oct 2017 08:32:08 +0000 (10:32 +0200)
panikombo/forms.py
panikombo/migrations/0012_auto_20171015_1027.py [new file with mode: 0644]
panikombo/models.py

index 6e499e0..8cb67c5 100644 (file)
@@ -53,14 +53,14 @@ class EpisodeCellForm(forms.ModelForm):
 class EpisodeAutoSelectionCellForm(forms.ModelForm):
     class Meta:
         model = EpisodeAutoSelectionCell
 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
         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()}
 
 
         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 (file)
index 0000000..73beee0
--- /dev/null
@@ -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),
+        ),
+    ]
index 5588962..350f77e 100644 (file)
@@ -6,7 +6,9 @@ from django.db import models
 from django.utils.translation import ugettext_lazy as _
 
 from ckeditor.fields import RichTextField
 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.managers import TaggableManager
+from taggit.utils import parse_tags
 
 from combo.data.models import CellBase
 from combo.data.library import register_cell_class
 
 from combo.data.models import CellBase
 from combo.data.library import register_cell_class
@@ -80,10 +82,16 @@ class EpisodeCell(CellBase):
         return ''
 
 
         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)
 @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,
     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())
             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(
 
         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)
 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)
     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())
         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:
         if self.future:
             newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today())
         if self.category: