close Warning: Can't use blame annotator:
No changeset 422 in the repository

source: django_apps/running/views.py@ 422

Last change on this file since 422 was 301, checked in by Rick van der Zwet, 14 years ago
  • Login/logout
  • Revised admin form admin form
File size: 3.9 KB
RevLine 
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.forms.models import BaseModelFormSet
7from django.template import RequestContext
8
9from django.shortcuts import render_to_response, get_object_or_404
10from running.models import Result, RunningUser
11import json
12
13def user_index(request, user_name):
14 running_user = get_object_or_404(RunningUser, username=user_name)
15 ResultFormSet = modelformset_factory(Result, fields=('date', 'distance', 'time'), exclude=('user'), extra=3, can_delete=True)
16 if request.method == 'POST':
17 if not request.user.is_authenticated():
18 return HttpResponseForbidden('Not Logged In!')
19 formset = ResultFormSet(request.POST, request.FILES)
20 if formset.is_valid():
21 instances = formset.save(commit=False)
22 for instance in instances:
23 instance.user = running_user
24 instance.save()
25 result_list = Result.objects.filter(user=running_user).order_by('date')
26 formset = ResultFormSet(queryset=result_list)
27 else:
28 result_list = Result.objects.filter(user=running_user).order_by('date')
29 formset = ResultFormSet(queryset=result_list)
30 return render_to_response('user_index.html', { 'runninguser' : running_user, 'formset' : formset, 'request' : request }, context_instance=RequestContext(request))
31
32def index(request):
33 user_list = RunningUser.objects.all()
34 return render_to_response('index.html', { 'user_list' : user_list })
35
36def zoom(request):
37 result_list = Result.objects.values_list('date', 'distance')
38 json_list = []
39 # Output date object in epoch milliseconds as JavaScript find this easy to use.
40 for (date,distance) in result_list:
41 json_list.append({'x': int(date.strftime("%s")) * 1000, 'y' : float(distance)})
42
43 data = json.dumps(json_list, ensure_ascii=False, indent=2)
44 return HttpResponse(data,mimetype="application/json")
45
46def json_user_column(request,user_id):
47 user = get_object_or_404(RunningUser, pk=user_id)
48 result_list = Result.objects.filter(user=user).values_list('date', 'distance')
49
50 total_list = {}
51 for (date,distance) in result_list:
52 key = date.strftime("%Y-%m")
53 if total_list.has_key(key):
54 total_list[key] += distance
55 else:
56 total_list[key] = distance
57
58 json_object = { 'label' : [] , 'data' : [] }
59 for (date,distance) in total_list.iteritems():
60 json_object['label'].append(date)
61 json_object['data'].append(float(distance))
62 data = json.dumps(json_object, ensure_ascii=False, indent=2)
63 return HttpResponse(data,mimetype="application/json")
64
65def json_user_total(request,user_id):
66 user = get_object_or_404(RunningUser, pk=user_id)
67 result_list = Result.objects.filter(user=user).values_list('date', 'distance')
68 json_list = []
69 distance_total = 0.0
70 # Output date object in epoch milliseconds as JavaScript find this easy to use.
71 for (date,distance) in result_list:
72 distance_total += float(distance)
73 json_list.append({'x': int(date.strftime("%s")) * 1000, 'y' : distance_total})
74 data = json.dumps(json_list, ensure_ascii=False, indent=2)
75 return HttpResponse(data,mimetype="application/json")
76
77def json_user_speed(request,user_id):
78 user = get_object_or_404(RunningUser, pk=user_id)
79 result_list = Result.objects.filter(user=user).values_list('date', 'distance', 'time')
80 json_list = []
81 # Output date object in epoch milliseconds as JavaScript find this easy to use.
82 for (date,distance,time) in result_list:
83 hour = float(time.second + time.minute * 60) / 3600 + time.hour
84 if hour == 0:
85 continue
86 json_list.append({'x': int(date.strftime("%s")) * 1000, 'y' : float(distance) / hour})
87 data = json.dumps(json_list, ensure_ascii=False, indent=2)
88 return HttpResponse(data,mimetype="application/json")
Note: See TracBrowser for help on using the repository browser.