source: django_apps/running/views.py@ 299

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

Week totals

File size: 2.8 KB
Line 
1# Create your views here.
2
3from django.core import serializers
4from django.http import HttpResponse
5from django.forms.models import modelformset_factory, inlineformset_factory
6from django.template import RequestContext
7
8from django.shortcuts import render_to_response, get_object_or_404
9from running.models import Result, User
10import json
11
12def user_index(request, user_name):
13 user = get_object_or_404(User, name=user_name)
14 ResultFormSet = modelformset_factory(Result, fields=('date', 'distance', 'time'), exclude=('user'), extra=3, can_delete=True)
15 if request.method == 'POST':
16 formset = ResultFormSet(request.POST, request.FILES)
17 if formset.is_valid():
18 instances = formset.save(commit=False)
19 for instance in instances:
20 instance.user = user
21 instance.save()
22
23 result_list = Result.objects.filter(user=user)
24 formset = ResultFormSet(queryset=result_list)
25 return render_to_response('user_index.html', { 'user' : user, 'formset' : formset }, context_instance=RequestContext(request))
26
27def index(request):
28 user_list = User.objects.all()
29 return render_to_response('index.html', { 'user_list' : user_list })
30
31def zoom(request):
32 result_list = Result.objects.values_list('date', 'distance')
33 json_list = []
34 # Output date object in epoch milliseconds as JavaScript find this easy to use.
35 for (date,distance) in result_list:
36 json_list.append({'x': int(date.strftime("%s")) * 1000, 'y' : float(distance)})
37
38 data = json.dumps(json_list, ensure_ascii=False, indent=2)
39 return HttpResponse(data,mimetype="application/json")
40
41def json_user_column(request,user_id):
42 user = get_object_or_404(User, pk=user_id)
43 result_list = Result.objects.filter(user=user).values_list('date', 'distance')
44
45 total_list = {}
46 for (date,distance) in result_list:
47 key = date.strftime("%Y-%m")
48 if total_list.has_key(key):
49 total_list[key] += distance
50 else:
51 total_list[key] = distance
52
53 json_object = { 'label' : [] , 'data' : [] }
54 for (date,distance) in total_list.iteritems():
55 json_object['label'].append(date)
56 json_object['data'].append(float(distance))
57 data = json.dumps(json_object, ensure_ascii=False, indent=2)
58 return HttpResponse(data,mimetype="application/json")
59
60def json_user_total(request,user_id):
61 result_list = Result.objects.values_list('date', 'distance')
62 json_list = []
63 distance_total = 0.0
64 # Output date object in epoch milliseconds as JavaScript find this easy to use.
65 for (date,distance) in result_list:
66 distance_total += float(distance)
67 json_list.append({'x': int(date.strftime("%s")) * 1000, 'y' : distance_total})
68 data = json.dumps(json_list, ensure_ascii=False, indent=2)
69 return HttpResponse(data,mimetype="application/json")
Note: See TracBrowser for help on using the repository browser.