From e89b4c50f48d5acd1b81c3825515b1a34a1d2089 Mon Sep 17 00:00:00 2001 From: Ernesto Rico-Schmidt Date: Fri, 12 Apr 2013 19:37:47 +0200 Subject: added JSON export for automation. diff --git a/program/models.py b/program/models.py index 448b742..484c191 100644 --- a/program/models.py +++ b/program/models.py @@ -9,6 +9,8 @@ from datetime import date, datetime, time, timedelta from dateutil.relativedelta import relativedelta from dateutil.rrule import rrule +from utils import get_automation_id_choices + class BroadcastFormat(models.Model): format = models.CharField(_("Format"), max_length=32) slug = models.SlugField(_("Slug"), max_length=32, unique=True) @@ -227,7 +229,7 @@ class Show(models.Model): email = models.EmailField(_("E-Mail"), blank=True, null=True) website = models.URLField(_("Website"), blank=True, null=True) cba_series_id = models.IntegerField(_("CBA series ID"), blank=True, null=True) - automation_id = models.IntegerField(_("Automation ID"), blank=True, null=True) + automation_id = models.IntegerField(_("Automation ID"), blank=True, null=True, choices=get_automation_id_choices()) created = models.DateTimeField(auto_now_add=True, editable=False) last_updated = models.DateTimeField(auto_now=True, editable=False) @@ -294,7 +296,7 @@ class ProgramSlot(models.Model): tend = models.TimeField(_("End time")) until = models.DateField(_("Last date")) is_repetition = models.BooleanField(_("Is repetition"), default=False) - automation_id = models.IntegerField(_("Automation ID"), blank=True, null=True) + automation_id = models.IntegerField(_("Automation ID"), blank=True, null=True, choices=get_automation_id_choices()) created = models.DateTimeField(auto_now_add=True, editable=False) last_updated = models.DateTimeField(auto_now=True, editable=False) diff --git a/program/utils.py b/program/utils.py new file mode 100644 index 0000000..5798c11 --- /dev/null +++ b/program/utils.py @@ -0,0 +1,9 @@ +from django.conf import settings + +import json +import urllib + +def get_automation_id_choices(): + shows_list = json.load(urllib.urlopen(settings.AUTOMATION_BASE_URL))['shows'] + shows = [(s['id'], s['title']) for s in shows_list] + return shows \ No newline at end of file diff --git a/program/views.py b/program/views.py index ec795cf..d824df8 100644 --- a/program/views.py +++ b/program/views.py @@ -1,11 +1,14 @@ from django.views.generic import list_detail, simple from django.shortcuts import get_object_or_404 from django.db.models import Q +from django.http import HttpResponse from models import BroadcastFormat, MusicFocus, Note, Show, ShowInformation, ShowTopic, TimeSlot from datetime import date, datetime, time, timedelta +import json + def show_list(request): queryset = Show.objects.filter(programslots__until__gt=date.today()).exclude(id=1).distinct() @@ -127,3 +130,21 @@ def styles(request): extra_context['showinformation'] = ShowInformation.objects.all() extra_context['showtopic'] = ShowTopic.objects.all() return simple.direct_to_template(request, template='program/styles.css', mimetype='text/css', extra_context=extra_context) + +def json_day_schedule(request, year=None, month=None, day=None): + if year is None and month is None and day is None: + today = datetime.combine(date.today(), time(6, 0)) + else: + today = datetime.strptime('%s__%s__%s__06__00' % (year, month, day), '%Y__%m__%d__%H__%M') + + timeslots = TimeSlot.objects.get_day_timeslots(today) + schedule = [] + for ts in timeslots: + if ts.programslot.automation_id: + schedule.append((ts.start.strftime('%H:%M:%S'), ts.programslot.automation_id)) + elif ts.programslot.show.automation_id: + schedule.append((ts.start.strftime('%H:%M:%S'), ts.programslot.show.automation_id)) + else: + schedule.append((ts.start.strftime('%H:%M:%S'), -1)) + + return HttpResponse(json.dumps(schedule), content_type="application/json") diff --git a/urls.py b/urls.py index e77334c..e55dddc 100644 --- a/urls.py +++ b/urls.py @@ -4,12 +4,15 @@ from django.contrib import admin admin.autodiscover() +from program.views import json_day_schedule urlpatterns = patterns('', (r'^admin/', include(admin.site.urls)), (r'^program/', include('program.urls')), (r'^nop', include('nop.urls')), (r'^tinymce/', include('tinymce.urls')), + url(r'^export/day_schedule/(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/$', json_day_schedule), ) + if settings.DEBUG: urlpatterns += patterns('', (r'^site_media/(?P.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}) -- cgit v0.10.2