order episodes alphabetically when period is set to all
authorFrédéric Péters <fpeters@0d.be>
Sun, 15 Oct 2017 09:14:23 +0000 (11:14 +0200)
committerFrédéric Péters <fpeters@0d.be>
Sun, 15 Oct 2017 09:14:23 +0000 (11:14 +0200)
panikombo/models.py

index bdf0704..d3b8c78 100644 (file)
@@ -3,6 +3,7 @@ import os
 
 from django import template
 from django.db import models
+from django.db.models.functions import Lower
 from django.utils.translation import ugettext_lazy as _
 
 from ckeditor.fields import RichTextField
@@ -122,6 +123,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(Lower('title')).distinct()
         elif self.period == 1:
             episodes_queryset = episodes_queryset.extra(
                     select={ 'first_diffusion': 'emissions_diffusion.datetime', },
@@ -130,6 +132,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', },
@@ -138,6 +141,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()
 
         return episodes_queryset
 
@@ -146,9 +150,7 @@ class EpisodeAutoSelectionCell(CellBase):
         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()
-            ctx['episodes'] = episodes_queryset
+            ctx['episodes'] = self.get_included_items()
         else:
             ctx['episodes'] = []