From 1a9ac1978c2a3dcf5ec34df46ef29ccb9e2db8e0 Mon Sep 17 00:00:00 2001
From: ingo <ingo.leindecker@fro.at>
Date: Sat, 10 Aug 2019 17:53:28 +0200
Subject: [PATCH] Show Model: - Field "FundingCategory" is not mandatory
 anymore - Added new field "is_active": Active shows will be determined either
 by existing timeslots in the future (if show is active). If show is not
 active it will always be considered as inactive, even if there are upcoming
 timeslots.

---
 program/admin.py       |  2 +-
 program/models.py      |  3 ++-
 program/serializers.py |  3 ++-
 program/views.py       | 14 ++++++++++----
 4 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/program/admin.py b/program/admin.py
index 57597c60..9db5ccc8 100644
--- a/program/admin.py
+++ b/program/admin.py
@@ -231,7 +231,7 @@ class ShowAdmin(admin.ModelAdmin):
     fields = (
         'predecessor', 'type', 'name', 'slug', 'image', 'logo', 'short_description', 'description',
         'email', 'website', 'hosts', 'owners', 'language', 'category', 'fundingcategory', 'topic',
-        'musicfocus', 'fallback_id', 'cba_series_id',
+        'musicfocus', 'fallback_id', 'cba_series_id', 'is_active'
     )
 
 
diff --git a/program/models.py b/program/models.py
index c6c70659..def5d0b6 100644
--- a/program/models.py
+++ b/program/models.py
@@ -307,7 +307,7 @@ class Show(models.Model):
     language = models.ManyToManyField(Language, blank=True, related_name='language', verbose_name=_("Language"))
     type = models.ForeignKey(Type, related_name='shows', verbose_name=_("Type"))
     category = models.ManyToManyField(Category, blank=True, related_name='shows', verbose_name=_("Category"))
-    fundingcategory = models.ForeignKey(FundingCategory, related_name='shows', verbose_name=_("Funding Category"))
+    fundingcategory = models.ForeignKey(FundingCategory, null=True, blank=True, related_name='shows', verbose_name=_("Funding Category"))
     topic = models.ManyToManyField(Topic, blank=True, related_name='shows', verbose_name=_("Topic"))
     musicfocus = models.ManyToManyField(MusicFocus, blank=True, related_name='shows', verbose_name=_("Music focus"))
     name = models.CharField(_("Name"), max_length=255, help_text=_("The show's name. Avoid a subtitle."))
@@ -325,6 +325,7 @@ class Show(models.Model):
     fallback_id = models.IntegerField(_("Fallback ID"), blank=True, null=True, help_text=_("If a timeslot of your show is empty, this playlist will be aired as a backup."))
     created = models.DateTimeField(auto_now_add=True, editable=False)
     last_updated = models.DateTimeField(auto_now=True, editable=False)
+    is_active = models.BooleanField(_("Is active?"), default=True)
 
     class Meta:
         ordering = ('slug',)
diff --git a/program/serializers.py b/program/serializers.py
index b2ec91a6..779316f4 100644
--- a/program/serializers.py
+++ b/program/serializers.py
@@ -256,7 +256,7 @@ class ShowSerializer(serializers.HyperlinkedModelSerializer):
         fields = ('id', 'name', 'slug', 'image', 'ppoi', 'logo', 'short_description', 'description',
                   'email', 'website', 'created', 'last_updated', 'type', 'fundingcategory',
                   'predecessor', 'cba_series_id', 'fallback_id', 'category', 'hosts',
-                  'owners', 'language', 'topic', 'musicfocus', 'thumbnails')
+                  'owners', 'language', 'topic', 'musicfocus', 'thumbnails', 'is_active')
 
 
     def create(self, validated_data):
@@ -315,6 +315,7 @@ class ShowSerializer(serializers.HyperlinkedModelSerializer):
             instance.type = validated_data.get('type', instance.type)
             instance.fundingcategory = validated_data.get('fundingcategory', instance.fundingcategory)
             instance.predecessor = validated_data.get('predecessor', instance.predecessor)
+            instance.is_active = validated_data.get('is_active', instance.is_active)
 
         instance.save()
         return instance
diff --git a/program/views.py b/program/views.py
index d99e828d..bace06fe 100644
--- a/program/views.py
+++ b/program/views.py
@@ -280,7 +280,11 @@ def json_playout(request):
         topics = ', '.join(ts.show.topic.values_list('topic', flat=True))
         musicfocus = ', '.join(ts.show.musicfocus.values_list('focus', flat=True))
         languages = ', '.join(ts.show.language.values_list('name', flat=True))
-        fundingcategory = FundingCategory.objects.get(pk=ts.show.fundingcategory_id)
+        fdcategory = None
+        if ts.show.fundingcategory_id is not None:
+            fundingcategory = FundingCategory.objects.get(pk=ts.show.fundingcategory_id)
+            fdcategory = fundingcategory.fundingcategory
+
         type = Type.objects.get(pk=ts.show.type_id)
 
         classname = 'default'
@@ -307,7 +311,7 @@ def json_playout(request):
             'show_topics': topics,
             'show_musicfocus': musicfocus,
             'show_languages': languages,
-            'show_fundingcategory': fundingcategory.fundingcategory,
+            'show_fundingcategory': fdcategory,
             'station_fallback_id': STATION_FALLBACK_ID, # TODO: Find a better way than getting it from the settings
             'memo': ts.memo,
             'className': classname,
@@ -477,11 +481,13 @@ class APIShowViewSet(viewsets.ModelViewSet):
                                                  Q(rrule_id=1,dstart__gte=date.today())
                                                ).distinct().values_list('show_id', flat=True)
 
-            shows = Show.objects.filter(id__in=show_ids)
+            # Filter active shows based on timeslots as well as on the is_active flag
+            # Even if there are future timeslots but is_active=True the show will be considered as inactive
+            shows = Show.objects.filter(id__in=show_ids,is_active=True)
 
         if self.request.GET.get('active') == 'false':
             '''Return all shows except those which are running'''
-            shows = Show.objects.exclude(id__in=show_ids)
+            shows = Show.objects.exclude(id__in=show_ids,is_active=True)
 
         if self.request.GET.get('owner') != None:
             '''Filter shows by owner'''
-- 
GitLab