diff options
author | Johannes Raggam <raggam-nl@adm.at> | 2011-05-25 17:14:37 (GMT) |
---|---|---|
committer | Johannes Raggam <raggam-nl@adm.at> | 2011-05-25 17:14:37 (GMT) |
commit | ca24a30ec6940052a259db5b38e85731ee7a3d27 (patch) | |
tree | ade0a2201c14ea57dd12d10593f47d8e9d2e0122 | |
parent | 40c628c4b3e836357fa18b8c3c8fcdb741340f49 (diff) |
finalize form with date/time submission functionality
-rw-r--r-- | nop/templates/nop/nop_form.html | 25 | ||||
-rw-r--r-- | nop/views.py | 72 |
2 files changed, 74 insertions, 23 deletions
diff --git a/nop/templates/nop/nop_form.html b/nop/templates/nop/nop_form.html index 71e16a2..e4efcd9 100644 --- a/nop/templates/nop/nop_form.html +++ b/nop/templates/nop/nop_form.html @@ -10,11 +10,11 @@ <script type="text/javascript"><!-- $(document).ready(function() { - $(":date").dateinput({ + $("#nop_date").dateinput({ format: 'yyyy-mm-dd', firstDay: 1 }); - $("#time").AnyTime_picker({ + $("#nop_time").AnyTime_picker({ format: "%H:%i", labelTitle: "Zeit", labelHour: "Stunde", @@ -27,10 +27,25 @@ <body> <div id="content-main" class="show-detail"> - <input type="date" id="date" class="date" value="" /> - <input type="text" id="time" class="date" value="" /> + <form action="" method="post">{% csrf_token %} + {{ form.as_table }} + <input type="submit" value="senden" /> + </form> + + {% if nowplaying %} + <ul> + {% for track in nowplaying %} + <li> + <small>{{track.datetime}}:</small> + <strong>{{track.artist}}</strong> - + {{track.title}} - + {{track.album}} + in der Sendung <em>{{track.showtitle}}</em> + </li> + {% endfor %} + </ul> + {% endif %} </div> </body> </html> - diff --git a/nop/views.py b/nop/views.py index b633422..610330f 100644 --- a/nop/views.py +++ b/nop/views.py @@ -1,24 +1,39 @@ -from django.views.generic import simple +from django.core.context_processors import csrf +from django.shortcuts import render_to_response from django.http import HttpResponse +from django import forms from models import Master import json import time +from datetime import datetime + +class NopForm(forms.Form): + date = forms.DateField( + required=True, + initial=datetime.date(datetime.now()), + 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'}) + ) -def get_current(request): +def _current(): #current = int(time.time())*1000000 #time.gmtime(Master.objects.using('nop').all()[6000].timestamp//1000000) # select all where timestamp < givenTS, get most recent one -> order DESC # reverse sorted. get the first object = last played result = Master.objects.using('nop').all()[0] - response = json.dumps({'artist': result.artist, 'title': result.title}) - - #return HttpResponse(response, mimetype='application/json') - return HttpResponse(response, mimetype='text/plain') - -def get(request, year=None, month=None, day=None, hour=None, minute=None): + return {'artist': result.artist, 'title': result.title} +def _bydate(year=None, month=None, day=None, hour=None, minute=None): try: # tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst ts = int(time.mktime(( @@ -29,19 +44,40 @@ def get(request, year=None, month=None, day=None, hour=None, minute=None): int(minute),0,0,0,-1))) * 1000000 result = Master.objects.using('nop').filter(timestamp__lt=ts)[:5] - response = json.dumps( - [{'artist': item.artist, 'title': item.title, 'album': item.album, - 'datetime': time.strftime('%Y-%m-%d %H:%M', - time.localtime(item.timestamp//1000000)), - 'showtitle': item.showtitle} for item in result]) + return [{'artist': item.artist, 'title': item.title, 'album': item.album, + 'datetime': time.strftime('%Y-%m-%d %H:%M', + time.localtime(item.timestamp//1000000)), + 'showtitle': item.showtitle} for item in result] except: # all errors - response = '' + return None + + +def get_current(request): + response = json.dumps(_current()) + + #return HttpResponse(response, mimetype='application/json') + return HttpResponse(response, mimetype='text/plain') + +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') return HttpResponse(response, mimetype='text/plain') + def nop_form(request): - extra_context = {'year':None, 'month':None, 'day':None, 'hour':None, 'minute': None} - if 'year' in request.GET: - pass - return simple.direct_to_template(request, extra_context=extra_context, template='nop/nop_form.html') + context = {} + context.update(csrf(request)) + if request.method == 'POST': + form = NopForm(request.POST) + if form.is_valid(): + date = form.cleaned_data['date'] + time = form.cleaned_data['time'] + else: + form = NopForm() + date = datetime.date(datetime.now()) + time = datetime.time(datetime.now()) + result = _bydate(date.year, date.month, date.day, time.hour, time.minute) + context['nowplaying'] = result + context['form'] = form + return render_to_response('nop/nop_form.html', context) |