From b86385019bcfcf889461fcb2ac4120cb3daccd6b Mon Sep 17 00:00:00 2001
From: ingo <ingo.leindecker@fro.at>
Date: Wed, 13 Dec 2017 16:39:38 +0100
Subject: [PATCH] Only retrieve active schedules for the given date. ...but
 include upcoming single timeslots.

Fixed another bug in case there's no note.

See #24
---
 frapp/views.py | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/frapp/views.py b/frapp/views.py
index 9f2c649f..48026687 100644
--- a/frapp/views.py
+++ b/frapp/views.py
@@ -1,6 +1,7 @@
 import json
 from datetime import date, datetime, time
 
+from django.db.models import Q
 from django.core.exceptions import ObjectDoesNotExist
 from django.http import HttpResponse
 from django.contrib.sites.shortcuts import get_current_site
@@ -104,16 +105,30 @@ def json_frapp(request):
         image = '' if s.image.name == None else str(get_current_site(request)) + MEDIA_URL + s.image.name
         url = '' if s.website == None else s.website
 
-        schedules = Schedule.objects.filter(show=s.id,is_repetition=False)
-        schedules_repetition = Schedule.objects.filter(show=s.id,is_repetition=True)
+        # Get active schedules for the given date
+        # But include upcoming single timeslots (with rrule_id=1)
+        schedules = Schedule.objects.filter( Q(show=s.id,is_repetition=False) &
+                                             (
+                                               Q(rrule_id__gt=1,dstart__lte=start,until__gte=start) |
+                                               Q(rrule_id=1,dstart__gte=start)
+                                             )
+                                           )
+
+        schedules_repetition = Schedule.objects.filter( Q(show=s.id,is_repetition=True) &
+                                             (
+                                               Q(rrule_id__gt=1,dstart__lte=start,until__gte=start) |
+                                               Q(rrule_id=1,dstart__gte=start)
+                                             )
+                                           )
+
         broadcastinfos = ''
 
-        if schedules.exists():
-            for schedule in schedules:
-                broadcastinfos = broadcastinfos + generate_frapp_broadcastinfos(schedule)
-        else:
+        if not schedules.exists():
             continue
 
+        for schedule in schedules:
+            broadcastinfos = broadcastinfos + generate_frapp_broadcastinfos(schedule)
+
         if schedules_repetition.exists():
             broadcastinfos = broadcastinfos + 'Wiederholung jeweils:'
             for schedule in schedules_repetition:
@@ -152,7 +167,7 @@ def json_frapp(request):
             namedisplay = note.title + is_repetition
             description = note.content
             url = str(get_current_site(request)) + '/notes/' + note.slug
-
+            urlmp3 = note.audio_url
         except ObjectDoesNotExist:
             pass
 
@@ -164,7 +179,7 @@ def json_frapp(request):
             'namedisplay': namedisplay,
             'description': description,
             'url': url,
-            'urlmp3': note.audio_url,
+            'urlmp3': urlmp3,
         }
 
         shows_output.append(ts_entry)
-- 
GitLab