add additional "and_tags" field to auto selection, for ANDing criterias
[django-panik-combo.git] / panikombo / models.py
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 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: