add possibility to filter episodes by time
authorFrédéric Péters <fpeters@0d.be>
Wed, 18 Nov 2015 19:54:26 +0000 (20:54 +0100)
committerFrédéric Péters <fpeters@0d.be>
Wed, 18 Nov 2015 19:54:26 +0000 (20:54 +0100)
panikombo/forms.py
panikombo/migrations/0009_episodeautoselectioncell_period.py [new file with mode: 0644]
panikombo/models.py

index eae28b4..01fb013 100644 (file)
@@ -51,7 +51,7 @@ class EpisodeCellForm(forms.ModelForm):
 class EpisodeAutoSelectionCellForm(forms.ModelForm):
     class Meta:
         model = EpisodeAutoSelectionCell
-        fields = ('title', 'tags', 'category')
+        fields = ('title', 'tags', 'category', 'period')
         widgets = {'tags': TagWidget()}
 
 
diff --git a/panikombo/migrations/0009_episodeautoselectioncell_period.py b/panikombo/migrations/0009_episodeautoselectioncell_period.py
new file mode 100644 (file)
index 0000000..fdd0c87
--- /dev/null
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('panikombo', '0008_topikcell'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='episodeautoselectioncell',
+            name='period',
+            field=models.PositiveSmallIntegerField(default=0, verbose_name='Period', choices=[(0, 'All'), (1, 'Future'), (2, 'Past')]),
+            preserve_default=True,
+        ),
+    ]
index 5c294bc..38da84b 100644 (file)
@@ -85,6 +85,12 @@ class EpisodeAutoSelectionCell(CellBase):
     title = models.CharField(_('Title'), max_length=50, blank=True)
     tags = TaggableManager(_('Tags'), blank=True)
     category = models.ForeignKey('emissions.Category', null=True, blank=True)
+    period = models.PositiveSmallIntegerField(
+            _('Period'), default=0,
+            choices=((0, _('All')),
+                     (1, _('Future')),
+                     (2, _('Past'))))
+
 
     class Meta:
         verbose_name = _('Automatic Episode Selection')
@@ -95,6 +101,31 @@ 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.period == 0:
+            episodes_queryset = episodes_queryset.extra(
+                    select={ 'first_diffusion': 'emissions_diffusion.datetime', },
+                    select_params=(False, True),
+                    where=['''datetime = (SELECT MIN(datetime) FROM emissions_diffusion
+                                           WHERE episode_id = emissions_episode.id)'''],
+                    tables=['emissions_diffusion'])
+        elif self.period == 1:
+            episodes_queryset = episodes_queryset.extra(
+                    select={ 'first_diffusion': 'emissions_diffusion.datetime', },
+                    select_params=(False, True),
+                    where=['''datetime = (SELECT MIN(datetime) FROM emissions_diffusion
+                                           WHERE episode_id = emissions_episode.id AND
+                                                 emissions_diffusion.datetime >= CURRENT_TIMESTAMP)'''],
+                    tables=['emissions_diffusion'])
+        elif self.period == 2:
+            episodes_queryset = episodes_queryset.extra(
+                    select={ 'first_diffusion': 'emissions_diffusion.datetime', },
+                    select_params=(False, True),
+                    where=['''datetime = (SELECT MIN(datetime) FROM emissions_diffusion
+                                           WHERE episode_id = emissions_episode.id AND
+                                                 emissions_diffusion.datetime < CURRENT_TIMESTAMP)'''],
+                    tables=['emissions_diffusion'])
+
         return episodes_queryset
 
     def render(self, context):
@@ -102,15 +133,7 @@ class EpisodeAutoSelectionCell(CellBase):
         context['title'] = self.title
 
         episodes_queryset = self.get_included_items()
-        episodes_queryset = episodes_queryset.extra(select={
-                        'first_diffusion': 'emissions_diffusion.datetime',
-                        },
-                        select_params=(False, True),
-                        where=['''datetime = (SELECT MIN(datetime)
-                                                FROM emissions_diffusion
-                                               WHERE episode_id = emissions_episode.id)'''],
-                        tables=['emissions_diffusion'],
-                    ).order_by('-first_diffusion').distinct()
+        episodes_queryset = episodes_queryset.order_by('-first_diffusion').distinct()
         context['episodes'] = episodes_queryset
         return tmpl.render(context)