# Create your views here. from django.core import serializers from django.http import HttpResponse from django.shortcuts import render_to_response, get_object_or_404 from running.models import Result, User import json def user_index(request, user_name): user = get_object_or_404(User, name=user_name) return render_to_response('user_index.html', { 'user' : user }) def index(request): user_list = User.objects.all() return render_to_response('index.html', { 'user_list' : user_list }) def zoom(request): result_list = Result.objects.values_list('date', 'distance') json_list = [] # Output date object in epoch milliseconds as JavaScript find this easy to use. for (date,distance) in result_list: json_list.append({'x': int(date.strftime("%s")) * 1000, 'y' : float(distance)}) data = json.dumps(json_list, ensure_ascii=False, indent=2) return HttpResponse(data,mimetype="application/json") def json_user_column(request,user_id): user = get_object_or_404(User, pk=user_id) result_list = Result.objects.filter(user=user).values_list('date', 'distance') json_object = { 'label' : [] , 'data' : [] } for (date,distance) in result_list: json_object['label'].append(str(date)) json_object['data'].append(float(distance)) data = json.dumps(json_object, ensure_ascii=False, indent=2) return HttpResponse(data,mimetype="application/json") def json_user_total(request,user_id): result_list = Result.objects.values_list('date', 'distance') json_list = [] # Output date object in epoch milliseconds as JavaScript find this easy to use. for (date,distance) in result_list: json_list.append({'x': int(date.strftime("%s")) * 1000, 'y' : float(distance)}) data = json.dumps(json_list, ensure_ascii=False, indent=2) return HttpResponse(data,mimetype="application/json")