source: django_apps/running/views.py@ 296

Last change on this file since 296 was 291, checked in by Rick van der Zwet, 14 years ago

Project import and intial application 'running' import, which combines django with protovis.

File size: 1.8 KB
Line 
1# Create your views here.
2
3from django.core import serializers
4from django.http import HttpResponse
5from django.shortcuts import render_to_response, get_object_or_404
6from running.models import Result, User
7import json
8
9def user_index(request, user_name):
10 user = get_object_or_404(User, name=user_name)
11 return render_to_response('user_index.html', { 'user' : user })
12
13def index(request):
14 user_list = User.objects.all()
15 return render_to_response('index.html', { 'user_list' : user_list })
16
17def zoom(request):
18 result_list = Result.objects.values_list('date', 'distance')
19 json_list = []
20 # Output date object in epoch milliseconds as JavaScript find this easy to use.
21 for (date,distance) in result_list:
22 json_list.append({'x': int(date.strftime("%s")) * 1000, 'y' : float(distance)})
23
24 data = json.dumps(json_list, ensure_ascii=False, indent=2)
25 return HttpResponse(data,mimetype="application/json")
26
27def json_user_column(request,user_id):
28 user = get_object_or_404(User, pk=user_id)
29 result_list = Result.objects.filter(user=user).values_list('date', 'distance')
30 json_object = { 'label' : [] , 'data' : [] }
31 for (date,distance) in result_list:
32 json_object['label'].append(str(date))
33 json_object['data'].append(float(distance))
34 data = json.dumps(json_object, ensure_ascii=False, indent=2)
35 return HttpResponse(data,mimetype="application/json")
36
37def json_user_total(request,user_id):
38 result_list = Result.objects.values_list('date', 'distance')
39 json_list = []
40 # Output date object in epoch milliseconds as JavaScript find this easy to use.
41 for (date,distance) in result_list:
42 json_list.append({'x': int(date.strftime("%s")) * 1000, 'y' : float(distance)})
43 data = json.dumps(json_list, ensure_ascii=False, indent=2)
44 return HttpResponse(data,mimetype="application/json")
Note: See TracBrowser for help on using the repository browser.