diff options
Diffstat (limited to 'nop')
-rw-r--r-- | nop/models.py | 5 | ||||
-rw-r--r-- | nop/urls.py | 5 | ||||
-rw-r--r-- | nop/views.py | 111 |
3 files changed, 67 insertions, 54 deletions
diff --git a/nop/models.py b/nop/models.py index 8bb87af..6aeb794 100644 --- a/nop/models.py +++ b/nop/models.py @@ -1,5 +1,6 @@ from django.db import models + class Master(models.Model): timestamp = models.BigIntegerField(primary_key=True) cart = models.IntegerField() @@ -9,6 +10,7 @@ class Master(models.Model): artist = models.CharField(max_length=765, blank=True) album = models.CharField(max_length=765, blank=True) ismusic = models.IntegerField(null=True, blank=True) + class Meta: db_table = u'master' ordering = ['-timestamp'] @@ -23,6 +25,7 @@ class Standby(models.Model): artist = models.CharField(max_length=765, blank=True) album = models.CharField(max_length=765, blank=True) ismusic = models.IntegerField(null=True, blank=True) + class Meta: db_table = u'standby' ordering = ['-timestamp'] @@ -30,7 +33,7 @@ class Standby(models.Model): class State(models.Model): timestamp = models.BigIntegerField(primary_key=True) state = models.CharField(max_length=96, blank=True) + class Meta: db_table = u'state' ordering = ['-timestamp'] - diff --git a/nop/urls.py b/nop/urls.py index 63c183c..c4012a6 100644 --- a/nop/urls.py +++ b/nop/urls.py @@ -2,10 +2,11 @@ from django.conf.urls.defaults import * from views import get, get_current, nop_form import os -NOP_STATIC_DIR = os.path.join(os.path.dirname(__file__), 'site_media') +NOP_STATIC_DIR = os.path.join(os.path.dirname(__file__), 'site_media') -urlpatterns = patterns('', +urlpatterns = patterns( + '', url(r'^/get_current/?$', get_current), url(r'^/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<hour>\d{1,2})/(?P<minute>\d{1,2})/?$', get), url(r'^/?$', nop_form), diff --git a/nop/views.py b/nop/views.py index 9e76d2f..9d71379 100644 --- a/nop/views.py +++ b/nop/views.py @@ -1,48 +1,50 @@ -# -*- coding: utf-8 -*- +# coding=utf-8 -from django.core.context_processors import csrf +from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned from django.shortcuts import render_to_response from django.http import HttpResponse from django import forms from models import Master, Standby, State -from program.models import TimeSlot +from program.models import TimeSlot, Note import json import time from datetime import datetime DB = 'nop' -MUSIKPROG_IDS = (1,5,17,34,60,81,89) - # 1 - unmodieriertes musikprogramm - # 5 - Abunda Lingva - ## 12 - aus den freien radios - # 17 - bumbumtschak - # 34 - fruehstück A - # 60 - musikprogramm bunt gemischt - # 81 - selchfleisch - # 89 - styrian underground + +MUSIKPROG_IDS = ( + 1, # unmodieriertes musikprogramm + 17, # bumbumtschak + 203, # Hotel Passage + 204, # Radyo Mezopotamya + 206, # Abunda Lingva + 290 # styrian underground +) + +SPECIAL_PROGRAM_IDS = ( + 66, # Probebühne + 374 # musikprogramm bunt gemischt +) + class NopForm(forms.Form): date = forms.DateField( - required=True, - #initial=datetime.date(datetime.now()), ## static initial specifies - ## any time but not the - ## current one - widget=forms.DateInput( - format='%Y-%m-%d', - attrs={'id':'nop_date', 'class':'date'}) - ) + required=True, + widget=forms.DateInput( + format='%Y-%m-%d', + attrs={'id': 'nop_date', 'class': 'date'})) time = forms.TimeField( - required=True, - #initial=datetime.time(datetime.now()), - widget=forms.TimeInput( - format='%H:%M', - attrs={'id':'nop_time', 'class':'date'}) - ) + required=True, + widget=forms.TimeInput( + format='%H:%M', + attrs={'id': 'nop_time', 'class': 'date'})) + def _dtstring(dt): return time.strftime('%Y-%m-%d %H:%M', dt) + def _which(timestamp=None): if timestamp: res = State.objects.using(DB).filter(timestamp__lt=timestamp)[0] @@ -53,42 +55,49 @@ def _which(timestamp=None): else: return Standby -def _get_show(datetime = None): + +def _get_show(datetime=None): try: if datetime: timeslot = TimeSlot.objects.get(start__lte=datetime, end__gt=datetime) else: timeslot = TimeSlot.objects.get_or_create_current() + except (ObjectDoesNotExist, MultipleObjectsReturned): + return {'start': None, 'id': None, 'name': None} + else: + try: + note = timeslot.note + except ObjectDoesNotExist: + note = None + return {'start': _dtstring(timeslot.start.timetuple()), 'id': timeslot.show.id, - 'name': timeslot.show.name} - except: # e.g. DoesNotExist - return {'start': None, 'id': None, 'name': None} + 'name': timeslot.show.name, + 'note': note} def _current(): - #current = int(time.time())*1000000 - #time.gmtime(_which().objects.using(DB).all()[6000].timestamp//1000000) - # select all where timestamp < givenTS, get most recent one -> order DESC - artist = None title = None album = None show = _get_show() - if show['id'] in MUSIKPROG_IDS: - # reverse sorted. get the first object = last played + + if show['id'] in MUSIKPROG_IDS or (show['id'] in SPECIAL_PROGRAM_IDS and not show['note']): result = _which().objects.using(DB).all()[0] artist = result.artist title = result.title album = result.album + return {'show': show['name'], 'start': show['start'], 'artist': artist, 'title': title, 'album': album} + def _bydate(year=None, month=None, day=None, hour=None, minute=None): show = _get_show(datetime(year, month, day, hour, minute)) + if show['id'] and show['id'] not in MUSIKPROG_IDS: return [{'show': show['name'], 'start': show['start'], @@ -96,13 +105,7 @@ def _bydate(year=None, month=None, day=None, hour=None, minute=None): 'title': None, 'album': None}] else: - # tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst - ts = int(time.mktime(( - int(year), - int(month), - int(day), - int(hour), - int(minute),0,0,0,-1))) * 1000000 + ts = int(time.mktime((int(year), int(month), int(day), int(hour), int(minute), 0, 0, 0, -1))) * 1000000 result = _which(ts).objects.using(DB).filter(timestamp__lt=ts)[:5] return [{'show': show['name'], 'start': _dtstring(time.localtime(item.timestamp//1000000)), @@ -110,31 +113,37 @@ def _bydate(year=None, month=None, day=None, hour=None, minute=None): 'title': item.title, 'album': item.album} for item in result] + def get_current(request): response = json.dumps(_current()) return HttpResponse(response, mimetype='application/json') + def get(request, year=None, month=None, day=None, hour=None, minute=None): response = json.dumps(_bydate(year, month, day, hour, minute)) return HttpResponse(response, mimetype='application/json') + def nop_form(request): context = {} - ## currently no csrf security for nicier forms - #context.update(csrf(request)) # in django template: {% csrf_token %} date = None time = None - if request.method == 'GET' and\ - ('date' in request.GET or 'time' in request.GET): + + if request.method == 'GET' and ('date' in request.GET or 'time' in request.GET): form = NopForm(request.GET) + if form.is_valid(): date = form.cleaned_data['date'] time = form.cleaned_data['time'] else: - form = NopForm(initial={'date':datetime.date(datetime.now()), - 'time':datetime.time(datetime.now())}) - if not date: date = datetime.date(datetime.now()) - if not time: time = datetime.time(datetime.now()) + form = NopForm(initial={'date': datetime.date(datetime.now()), 'time': datetime.time(datetime.now())}) + + if not date: + date = datetime.date(datetime.now()) + + if not time: + time = datetime.time(datetime.now()) + result = _bydate(date.year, date.month, date.day, time.hour, time.minute) context['nowplaying'] = result context['form'] = form |