]> git.0d.be Git - django-panik-combo.git/blobdiff - panikombo/models.py
remove topik from itemtopik model
[django-panik-combo.git] / panikombo / models.py
index 558896239c9b903d1f8f8216eb3417c554eb9e8e..2907b86cb22fdf868a7efde55422654cbcd39e10 100644 (file)
@@ -3,10 +3,14 @@ import os
 
 from django import template
 from django.db import models
+from django.db.models.functions import Lower
+from django.utils.encoding import force_text, python_2_unicode_compatible
 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 +84,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,
@@ -91,6 +101,8 @@ class EpisodeAutoSelectionCell(CellBase):
                      (1, _('Future')),
                      (2, _('Past'))))
 
+    template_name = 'panikombo/episode_auto_selection.html'
+
 
     class Meta:
         verbose_name = _('Automatic Episode Selection')
@@ -101,6 +113,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(
@@ -109,6 +124,7 @@ class EpisodeAutoSelectionCell(CellBase):
                     where=['''datetime = (SELECT MIN(datetime) FROM emissions_diffusion
                                            WHERE episode_id = emissions_episode.id)'''],
                     tables=['emissions_diffusion'])
+            episodes_queryset = episodes_queryset.order_by('first_diffusion').distinct()
         elif self.period == 1:
             episodes_queryset = episodes_queryset.extra(
                     select={ 'first_diffusion': 'emissions_diffusion.datetime', },
@@ -117,6 +133,7 @@ class EpisodeAutoSelectionCell(CellBase):
                                            WHERE episode_id = emissions_episode.id) AND
                                                  datetime >= CURRENT_TIMESTAMP'''],
                     tables=['emissions_diffusion'])
+            episodes_queryset = episodes_queryset.order_by('-first_diffusion').distinct()
         elif self.period == 2:
             episodes_queryset = episodes_queryset.extra(
                     select={ 'first_diffusion': 'emissions_diffusion.datetime', },
@@ -125,20 +142,20 @@ class EpisodeAutoSelectionCell(CellBase):
                                            WHERE episode_id = emissions_episode.id) AND
                                                  datetime < CURRENT_TIMESTAMP'''],
                     tables=['emissions_diffusion'])
+            episodes_queryset = episodes_queryset.order_by('-first_diffusion').distinct()
 
         return episodes_queryset
 
-    def render(self, context):
-        tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
-        context['title'] = self.title
+    def get_cell_extra_context(self, context):
+        ctx = super(EpisodeAutoSelectionCell, self).get_cell_extra_context(context)
+        ctx['title'] = self.title
 
         if (self.category or self.period or self.tags.count()):
-            episodes_queryset = self.get_included_items()
-            episodes_queryset = episodes_queryset.order_by('-first_diffusion').distinct()
-            context['episodes'] = episodes_queryset
+            ctx['episodes'] = self.get_included_items()
         else:
-            context['episodes'] = []
-        return tmpl.render(context)
+            ctx['episodes'] = []
+
+        return ctx
 
     def get_default_form_class(self):
         from .forms import EpisodeAutoSelectionCellForm
@@ -153,10 +170,13 @@ 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)
 
+    template_name = 'panikombo/newsitem_auto_selection.html'
+
     class Meta:
         verbose_name = _('Automatic Newsitem Selection')
 
@@ -164,6 +184,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:
@@ -171,14 +194,16 @@ class NewsItemAutoSelectionCell(CellBase):
         newsitems_queryset = newsitems_queryset.order_by('-event_date', '-creation_timestamp')
         return newsitems_queryset
 
-    def render(self, context):
-        tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html')
-        context['title'] = self.title
+    def get_cell_extra_context(self, context):
+        ctx = super(NewsItemAutoSelectionCell, self).get_cell_extra_context(context)
+        ctx['title'] = self.title
+
         if self.tags.count() or self.future or self.category:
-            context['newsitems'] = self.get_included_items()
+            ctx['newsitems'] = self.get_included_items()
         else:
-            context['newsitems'] = []
-        return tmpl.render(context)
+            ctx['newsitems'] = []
+
+        return ctx
 
     def get_default_form_class(self):
         from .forms import NewsItemAutoSelectionCellForm
@@ -194,6 +219,7 @@ def get_topik_image_path(instance, filename):
     return os.path.join('images', 'topik', instance.page.slug,
             os.path.basename(filename))
 
+@python_2_unicode_compatible
 class Topik(models.Model):
     page = models.ForeignKey('data.Page')
     image = models.ImageField(_('Image'),
@@ -203,10 +229,10 @@ class Topik(models.Model):
     got_focus = models.DateTimeField(default=None, null=True, blank=True)
     has_focus = models.BooleanField(default=False)
 
-    def __unicode__(self):
+    def __str__(self):
         if not self.page:
-            return super(Topik, self).__unicode__()
-        return unicode(self.page)
+            return super(Topik, self).__str__()
+        return force_text(self.page)
 
 
 class ItemTopik(models.Model):
@@ -214,8 +240,7 @@ class ItemTopik(models.Model):
             null=True, blank=True)
     episode = models.ForeignKey('emissions.Episode', verbose_name=_('Episode'),
             null=True, blank=True)
-    topik = models.ForeignKey('Topik', verbose_name='Topik',
-            null=True, blank=True)
+    page = models.ForeignKey('data.Page', null=True, blank=True)
 
 
 @register_cell_class