Skip to content
Snippets Groups Projects
Commit 57cbdfa4 authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

updated ids with playlist instead of (live) moderation, added special show handling. formatting.

parent c5bcf2d0
No related branches found
No related tags found
No related merge requests found
from django.db import models from django.db import models
class Master(models.Model): class Master(models.Model):
timestamp = models.BigIntegerField(primary_key=True) timestamp = models.BigIntegerField(primary_key=True)
cart = models.IntegerField() cart = models.IntegerField()
...@@ -9,6 +10,7 @@ class Master(models.Model): ...@@ -9,6 +10,7 @@ class Master(models.Model):
artist = models.CharField(max_length=765, blank=True) artist = models.CharField(max_length=765, blank=True)
album = models.CharField(max_length=765, blank=True) album = models.CharField(max_length=765, blank=True)
ismusic = models.IntegerField(null=True, blank=True) ismusic = models.IntegerField(null=True, blank=True)
class Meta: class Meta:
db_table = u'master' db_table = u'master'
ordering = ['-timestamp'] ordering = ['-timestamp']
...@@ -23,6 +25,7 @@ class Standby(models.Model): ...@@ -23,6 +25,7 @@ class Standby(models.Model):
artist = models.CharField(max_length=765, blank=True) artist = models.CharField(max_length=765, blank=True)
album = models.CharField(max_length=765, blank=True) album = models.CharField(max_length=765, blank=True)
ismusic = models.IntegerField(null=True, blank=True) ismusic = models.IntegerField(null=True, blank=True)
class Meta: class Meta:
db_table = u'standby' db_table = u'standby'
ordering = ['-timestamp'] ordering = ['-timestamp']
...@@ -30,7 +33,7 @@ class Standby(models.Model): ...@@ -30,7 +33,7 @@ class Standby(models.Model):
class State(models.Model): class State(models.Model):
timestamp = models.BigIntegerField(primary_key=True) timestamp = models.BigIntegerField(primary_key=True)
state = models.CharField(max_length=96, blank=True) state = models.CharField(max_length=96, blank=True)
class Meta: class Meta:
db_table = u'state' db_table = u'state'
ordering = ['-timestamp'] ordering = ['-timestamp']
...@@ -2,10 +2,11 @@ from django.conf.urls.defaults import * ...@@ -2,10 +2,11 @@ from django.conf.urls.defaults import *
from views import get, get_current, nop_form from views import get, get_current, nop_form
import os 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'^/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'^/(?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), url(r'^/?$', nop_form),
......
# -*- 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.shortcuts import render_to_response
from django.http import HttpResponse from django.http import HttpResponse
from django import forms from django import forms
...@@ -12,38 +12,39 @@ import time ...@@ -12,38 +12,39 @@ import time
from datetime import datetime from datetime import datetime
DB = 'nop' DB = 'nop'
MUSIKPROG_IDS = ( MUSIKPROG_IDS = (
1, # unmodieriertes musikprogramm 1, # unmodieriertes musikprogramm
206, # Abunda Lingva 17, # bumbumtschak
17, # bumbumtschak 203, # Hotel Passage
34, # fruehstück A 204, # Radyo Mezopotamya
374, # musikprogramm bunt gemischt 206, # Abunda Lingva
81, # selchfleisch 290 # styrian underground
290, # styrian underground )
203 # Hotel Passage
SPECIAL_PROGRAM_IDS = (
66, # Probebühne
374 # musikprogramm bunt gemischt
) )
class NopForm(forms.Form): class NopForm(forms.Form):
date = forms.DateField( date = forms.DateField(
required=True, required=True,
#initial=datetime.date(datetime.now()), ## static initial specifies widget=forms.DateInput(
## any time but not the format='%Y-%m-%d',
## current one attrs={'id': 'nop_date', 'class': 'date'}))
widget=forms.DateInput(
format='%Y-%m-%d',
attrs={'id':'nop_date', 'class':'date'})
)
time = forms.TimeField( time = forms.TimeField(
required=True, required=True,
#initial=datetime.time(datetime.now()), widget=forms.TimeInput(
widget=forms.TimeInput( format='%H:%M',
format='%H:%M', attrs={'id': 'nop_time', 'class': 'date'}))
attrs={'id':'nop_time', 'class':'date'})
)
def _dtstring(dt): def _dtstring(dt):
return time.strftime('%Y-%m-%d %H:%M', dt) return time.strftime('%Y-%m-%d %H:%M', dt)
def _which(timestamp=None): def _which(timestamp=None):
if timestamp: if timestamp:
res = State.objects.using(DB).filter(timestamp__lt=timestamp)[0] res = State.objects.using(DB).filter(timestamp__lt=timestamp)[0]
...@@ -54,7 +55,8 @@ def _which(timestamp=None): ...@@ -54,7 +55,8 @@ def _which(timestamp=None):
else: else:
return Standby return Standby
def _get_show(datetime = None):
def _get_show(datetime=None):
try: try:
if datetime: if datetime:
timeslot = TimeSlot.objects.get(start__lte=datetime, end__gt=datetime) timeslot = TimeSlot.objects.get(start__lte=datetime, end__gt=datetime)
...@@ -62,34 +64,34 @@ def _get_show(datetime = None): ...@@ -62,34 +64,34 @@ def _get_show(datetime = None):
timeslot = TimeSlot.objects.get_or_create_current() timeslot = TimeSlot.objects.get_or_create_current()
return {'start': _dtstring(timeslot.start.timetuple()), return {'start': _dtstring(timeslot.start.timetuple()),
'id': timeslot.show.id, 'id': timeslot.show.id,
'name': timeslot.show.name} 'name': timeslot.show.name,
except: # e.g. DoesNotExist 'note': timeslot.note}
except (ObjectDoesNotExist, MultipleObjectsReturned):
return {'start': None, 'id': None, 'name': None} return {'start': None, 'id': None, 'name': None}
def _current(): 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 artist = None
title = None title = None
album = None album = None
show = _get_show() 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] result = _which().objects.using(DB).all()[0]
artist = result.artist artist = result.artist
title = result.title title = result.title
album = result.album album = result.album
return {'show': show['name'], return {'show': show['name'],
'start': show['start'], 'start': show['start'],
'artist': artist, 'artist': artist,
'title': title, 'title': title,
'album': album} 'album': album}
def _bydate(year=None, month=None, day=None, hour=None, minute=None): def _bydate(year=None, month=None, day=None, hour=None, minute=None):
show = _get_show(datetime(year, month, day, hour, minute)) show = _get_show(datetime(year, month, day, hour, minute))
if show['id'] and show['id'] not in MUSIKPROG_IDS: if show['id'] and show['id'] not in MUSIKPROG_IDS:
return [{'show': show['name'], return [{'show': show['name'],
'start': show['start'], 'start': show['start'],
...@@ -97,13 +99,7 @@ def _bydate(year=None, month=None, day=None, hour=None, minute=None): ...@@ -97,13 +99,7 @@ def _bydate(year=None, month=None, day=None, hour=None, minute=None):
'title': None, 'title': None,
'album': None}] 'album': None}]
else: 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] result = _which(ts).objects.using(DB).filter(timestamp__lt=ts)[:5]
return [{'show': show['name'], return [{'show': show['name'],
'start': _dtstring(time.localtime(item.timestamp//1000000)), 'start': _dtstring(time.localtime(item.timestamp//1000000)),
...@@ -111,31 +107,37 @@ def _bydate(year=None, month=None, day=None, hour=None, minute=None): ...@@ -111,31 +107,37 @@ def _bydate(year=None, month=None, day=None, hour=None, minute=None):
'title': item.title, 'title': item.title,
'album': item.album} for item in result] 'album': item.album} for item in result]
def get_current(request): def get_current(request):
response = json.dumps(_current()) response = json.dumps(_current())
return HttpResponse(response, mimetype='application/json') return HttpResponse(response, mimetype='application/json')
def get(request, year=None, month=None, day=None, hour=None, minute=None): def get(request, year=None, month=None, day=None, hour=None, minute=None):
response = json.dumps(_bydate(year, month, day, hour, minute)) response = json.dumps(_bydate(year, month, day, hour, minute))
return HttpResponse(response, mimetype='application/json') return HttpResponse(response, mimetype='application/json')
def nop_form(request): def nop_form(request):
context = {} context = {}
## currently no csrf security for nicier forms
#context.update(csrf(request)) # in django template: {% csrf_token %}
date = None date = None
time = 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) form = NopForm(request.GET)
if form.is_valid(): if form.is_valid():
date = form.cleaned_data['date'] date = form.cleaned_data['date']
time = form.cleaned_data['time'] time = form.cleaned_data['time']
else: else:
form = NopForm(initial={'date':datetime.date(datetime.now()), form = NopForm(initial={'date': datetime.date(datetime.now()), 'time': datetime.time(datetime.now())})
'time':datetime.time(datetime.now())})
if not date: date = datetime.date(datetime.now()) if not date:
if not time: time = datetime.time(datetime.now()) 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) result = _bydate(date.year, date.month, date.day, time.hour, time.minute)
context['nowplaying'] = result context['nowplaying'] = result
context['form'] = form context['form'] = form
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment