[291] | 1 | # Create your views here.
|
---|
| 2 |
|
---|
| 3 | from django.core import serializers
|
---|
| 4 | from django.http import HttpResponse
|
---|
[298] | 5 | from django.forms.models import modelformset_factory, inlineformset_factory
|
---|
[301] | 6 | from django.forms.models import BaseModelFormSet
|
---|
[298] | 7 | from django.template import RequestContext
|
---|
| 8 |
|
---|
[291] | 9 | from django.shortcuts import render_to_response, get_object_or_404
|
---|
[301] | 10 | from running.models import Result, RunningUser
|
---|
[291] | 11 | import json
|
---|
| 12 |
|
---|
| 13 | def user_index(request, user_name):
|
---|
[301] | 14 | running_user = get_object_or_404(RunningUser, username=user_name)
|
---|
[298] | 15 | ResultFormSet = modelformset_factory(Result, fields=('date', 'distance', 'time'), exclude=('user'), extra=3, can_delete=True)
|
---|
| 16 | if request.method == 'POST':
|
---|
[301] | 17 | if not request.user.is_authenticated():
|
---|
| 18 | return HttpResponseForbidden('Not Logged In!')
|
---|
[298] | 19 | formset = ResultFormSet(request.POST, request.FILES)
|
---|
| 20 | if formset.is_valid():
|
---|
| 21 | instances = formset.save(commit=False)
|
---|
| 22 | for instance in instances:
|
---|
[301] | 23 | instance.user = running_user
|
---|
[298] | 24 | instance.save()
|
---|
[301] | 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))
|
---|
[291] | 31 |
|
---|
| 32 | def index(request):
|
---|
[301] | 33 | user_list = RunningUser.objects.all()
|
---|
[291] | 34 | return render_to_response('index.html', { 'user_list' : user_list })
|
---|
| 35 |
|
---|
| 36 | def 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 |
|
---|
| 46 | def json_user_column(request,user_id):
|
---|
[301] | 47 | user = get_object_or_404(RunningUser, pk=user_id)
|
---|
[291] | 48 | result_list = Result.objects.filter(user=user).values_list('date', 'distance')
|
---|
[299] | 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 |
|
---|
[291] | 58 | json_object = { 'label' : [] , 'data' : [] }
|
---|
[299] | 59 | for (date,distance) in total_list.iteritems():
|
---|
| 60 | json_object['label'].append(date)
|
---|
[291] | 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 |
|
---|
| 65 | def json_user_total(request,user_id):
|
---|
[301] | 66 | user = get_object_or_404(RunningUser, pk=user_id)
|
---|
| 67 | result_list = Result.objects.filter(user=user).values_list('date', 'distance')
|
---|
[291] | 68 | json_list = []
|
---|
[298] | 69 | distance_total = 0.0
|
---|
[291] | 70 | # Output date object in epoch milliseconds as JavaScript find this easy to use.
|
---|
| 71 | for (date,distance) in result_list:
|
---|
[298] | 72 | distance_total += float(distance)
|
---|
| 73 | json_list.append({'x': int(date.strftime("%s")) * 1000, 'y' : distance_total})
|
---|
[291] | 74 | data = json.dumps(json_list, ensure_ascii=False, indent=2)
|
---|
| 75 | return HttpResponse(data,mimetype="application/json")
|
---|
[301] | 76 |
|
---|
| 77 | def 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")
|
---|