From 6eabfdd69ecf3b7615e9522ae3839554afc72864 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Fri, 7 Apr 2017 02:40:33 +0200
Subject: [PATCH] added sepcial show timeslot export

---
 program/utils.py | 10 ++++++++++
 program/views.py | 20 +++++++++++++++++++-
 pv/urls.py       |  5 +++--
 3 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/program/utils.py b/program/utils.py
index 3befeaf2..1a81766a 100644
--- a/program/utils.py
+++ b/program/utils.py
@@ -40,6 +40,16 @@ def get_automation_id_choices():
     return shows
 
 
+def get_cached_shows():
+    cache_dir = getattr(settings, 'AUTOMATION_CACHE_DIR', 'cache')
+    cached_shows = join(cache_dir, 'shows.json')
+    shows = {}
+    with open(cached_shows) as shows_json:
+        shows = json.loads(shows_json.read())
+
+    return shows
+
+
 def tofirstdayinisoweek(year, week):
     # http://stackoverflow.com/questions/5882405/get-date-from-iso-week-number-in-python
     ret = datetime.strptime('%04d-%02d-1' % (year, week), '%Y-%W-%w')
diff --git a/program/views.py b/program/views.py
index 53467cd3..9886d08d 100644
--- a/program/views.py
+++ b/program/views.py
@@ -10,7 +10,7 @@ from django.views.generic.list import ListView
 
 from models import BroadcastFormat, MusicFocus, Note, Show, ShowInformation, ShowTopic, TimeSlot, Host
 
-from program.utils import tofirstdayinisoweek
+from program.utils import tofirstdayinisoweek, get_cached_shows
 
 
 class HostListView(ListView):
@@ -211,3 +211,21 @@ def json_day_schedule(request, year=None, month=None, day=None):
 
     return HttpResponse(json.dumps(schedule, ensure_ascii=False, encoding='utf8').encode('utf8'),
                         content_type="application/json; charset=utf-8")
+
+def json_timeslots_specials(request):
+    specials = {}
+    shows = get_cached_shows()['shows']
+    for show in shows:
+        show['pv_id'] = -1
+        if show['type'] == 's':
+            specials[show['id']] = show
+
+    for ts in TimeSlot.objects.filter(end__gt=datetime.now).filter(programslot__automation_id__in=specials.iterkeys()):
+        automation_id = ts.programslot.automation_id
+        specials[automation_id]['pv_id'] = int(ts.programslot.show.id)
+        specials[automation_id]['pv_name'] = ts.programslot.show.name
+        specials[automation_id]['pv_start'] = ts.start.strftime('%Y-%m-%d_%H:%M:%S')
+        specials[automation_id]['pv_end'] = ts.end.strftime('%Y-%m-%d_%H:%M:%S')
+
+    return HttpResponse(json.dumps(specials, ensure_ascii=False, encoding='utf8').encode('utf8'),
+                        content_type="application/json; charset=utf-8")
diff --git a/pv/urls.py b/pv/urls.py
index 3c0ab4d2..9873723c 100644
--- a/pv/urls.py
+++ b/pv/urls.py
@@ -2,7 +2,7 @@ from django.conf import settings
 from django.conf.urls import patterns, url, include
 from django.contrib import admin
 
-from program.views import json_day_schedule
+from program.views import json_day_schedule, json_timeslots_specials
 
 admin.autodiscover()
 
@@ -11,7 +11,8 @@ urlpatterns = patterns('',
                        url(r'^program/', include('program.urls')),
                        url(r'^nop', include('nop.urls')),
                        url(r'^tinymce/', include('tinymce.urls')),
-                       url(r'^export/day_schedule/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$', json_day_schedule)
+                       url(r'^export/day_schedule/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$', json_day_schedule),
+                       url(r'^export/timeslots_specials/$', json_timeslots_specials)
                        )
 
 if settings.DEBUG:
-- 
GitLab