[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
|
---|
| 6 | from django.template import RequestContext
|
---|
| 7 |
|
---|
[291] | 8 | from django.shortcuts import render_to_response, get_object_or_404
|
---|
| 9 | from running.models import Result, User
|
---|
| 10 | import json
|
---|
| 11 |
|
---|
| 12 | def user_index(request, user_name):
|
---|
| 13 | user = get_object_or_404(User, name=user_name)
|
---|
[298] | 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()
|
---|
[291] | 22 |
|
---|
[298] | 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 |
|
---|
[291] | 27 | def index(request):
|
---|
| 28 | user_list = User.objects.all()
|
---|
| 29 | return render_to_response('index.html', { 'user_list' : user_list })
|
---|
| 30 |
|
---|
| 31 | def 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 |
|
---|
| 41 | def 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')
|
---|
[299] | 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 |
|
---|
[291] | 53 | json_object = { 'label' : [] , 'data' : [] }
|
---|
[299] | 54 | for (date,distance) in total_list.iteritems():
|
---|
| 55 | json_object['label'].append(date)
|
---|
[291] | 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 |
|
---|
| 60 | def json_user_total(request,user_id):
|
---|
| 61 | result_list = Result.objects.values_list('date', 'distance')
|
---|
| 62 | json_list = []
|
---|
[298] | 63 | distance_total = 0.0
|
---|
[291] | 64 | # Output date object in epoch milliseconds as JavaScript find this easy to use.
|
---|
| 65 | for (date,distance) in result_list:
|
---|
[298] | 66 | distance_total += float(distance)
|
---|
| 67 | json_list.append({'x': int(date.strftime("%s")) * 1000, 'y' : distance_total})
|
---|
[291] | 68 | data = json.dumps(json_list, ensure_ascii=False, indent=2)
|
---|
| 69 | return HttpResponse(data,mimetype="application/json")
|
---|