From e1ea1fd45cd3aaa9b89e107fa22dfd95c7a34f77 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Thu, 27 Apr 2017 18:12:11 +0200
Subject: [PATCH] added markdown exporter for yearly show logs improved some
 export queries

---
 program/management/commands/export_showlog.py | 44 +++++++++++++++++++
 program/views.py                              | 12 ++---
 2 files changed, 50 insertions(+), 6 deletions(-)
 create mode 100644 program/management/commands/export_showlog.py

diff --git a/program/management/commands/export_showlog.py b/program/management/commands/export_showlog.py
new file mode 100644
index 00000000..da78cd23
--- /dev/null
+++ b/program/management/commands/export_showlog.py
@@ -0,0 +1,44 @@
+# -*- coding: utf-8 -*-
+
+import codecs
+import sys
+from datetime import date, datetime, time, timedelta
+from django.core.management.base import BaseCommand, CommandError
+from program.models import TimeSlot
+
+
+class Command(BaseCommand):
+    help = 'export playlog for one year'
+    args = '<year>'
+
+    def handle(self, *args, **options):
+        UTF8Writer = codecs.getwriter('utf8')
+        sys.stdout = UTF8Writer(sys.stdout)
+
+        if len(args) == 1:
+            try:
+                year = int(args[0])
+            except ValueError:
+                raise CommandError("'%s' is not a valid year" % args[0])
+        else:
+            raise CommandError('you must provide the year')
+
+        print "# Radio Helsinki Sendungslog %d" % year
+
+        start = datetime.strptime('%d__01__01__00__00' % (year), '%Y__%m__%d__%H__%M')
+        end = datetime.strptime('%d__01__01__00__00' % (year+1), '%Y__%m__%d__%H__%M')
+
+        currentDate = None
+        for ts in TimeSlot.objects.filter(end__gt=start, start__lt=end).select_related('programslot').select_related('show'):
+            if currentDate == None or currentDate < ts.start.date():
+                if currentDate:
+                    print "\n"
+                currentDate = ts.start.date()
+                print currentDate.strftime("## %a %d.%m.%Y:\n")
+
+            title = ts.show.name
+            if ts.programslot.is_repetition:
+                title += " (WH)"
+
+            print " * **%s - %s**: %s" % (ts.start.strftime("%H:%M:%S"), ts.end.strftime("%H:%M:%S"), title)
+
diff --git a/program/views.py b/program/views.py
index 9886d08d..a9c83e45 100644
--- a/program/views.py
+++ b/program/views.py
@@ -193,14 +193,14 @@ def json_day_schedule(request, year=None, month=None, day=None):
     else:
         today = datetime.strptime('%s__%s__%s__00__00' % (year, month, day), '%Y__%m__%d__%H__%M')
 
-    timeslots = TimeSlot.objects.get_24h_timeslots(today).select_related('programslot')
+    timeslots = TimeSlot.objects.get_24h_timeslots(today).select_related('programslot').select_related('show')
     schedule = []
     for ts in timeslots:
         entry = {
             'start': ts.start.strftime('%Y-%m-%d_%H:%M:%S'),
             'end': ts.end.strftime('%Y-%m-%d_%H:%M:%S'),
-            'title': ts.programslot.show.name,
-            'id': ts.programslot.show.id,
+            'title': ts.show.name,
+            'id': ts.show.id,
             'automation-id': -1
         }
 
@@ -220,10 +220,10 @@ def json_timeslots_specials(request):
         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()):
+    for ts in TimeSlot.objects.filter(end__gt=datetime.now, programslot__automation_id__in=specials.iterkeys()).select_related('show'):
         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_id'] = int(ts.show.id)
+        specials[automation_id]['pv_name'] = ts.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')
 
-- 
GitLab