1 | # Create your views here.
|
---|
2 |
|
---|
3 | from django.core import serializers
|
---|
4 | from django.http import HttpResponse
|
---|
5 | from django.shortcuts import render_to_response, get_object_or_404
|
---|
6 | from running.models import Result, User
|
---|
7 | import json
|
---|
8 |
|
---|
9 | def 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 |
|
---|
13 | def index(request):
|
---|
14 | user_list = User.objects.all()
|
---|
15 | return render_to_response('index.html', { 'user_list' : user_list })
|
---|
16 |
|
---|
17 | def 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 |
|
---|
27 | def 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 |
|
---|
37 | def 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")
|
---|