Changeset 301
- Timestamp:
- Apr 7, 2011, 8:56:04 PM (14 years ago)
- Location:
- django_apps/running
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
django_apps/running/admin.py
r291 r301 5 5 fields = [ 'date', 'distance', 'time' ] 6 6 7 class UserAdmin(admin.ModelAdmin):7 class RunningUserAdmin(admin.ModelAdmin): 8 8 inlines = [ ResultInline, ] 9 9 class Media: 10 10 css = { "all" : ("css/hide_admin_original.css",) } 11 admin.site.register( User,UserAdmin)11 admin.site.register(RunningUser, RunningUserAdmin) 12 12 13 13 -
django_apps/running/models.py
r291 r301 2 2 import datetime 3 3 4 from django.contrib.auth.models import User 5 4 6 # Create your models here. 5 class User(models.Model): 6 name = models.CharField(max_length=50) 7 def __unicode__(self): 8 return self.name 7 class RunningUser(User): 8 class Meta: 9 proxy = True 9 10 10 11 class Result(models.Model): 11 12 user = models.ForeignKey(User) 12 13 distance = models.DecimalField(max_digits=5,decimal_places=2) 13 time = models.TimeField( )14 time = models.TimeField(default='00:00:00') 14 15 date = models.DateField(default=datetime.date.today) 15 16 -
django_apps/running/static/year-total.js
r300 r301 1 1 function yearTotalGraph(data, binder) { 2 if (data.length == 0) { 3 return; 4 } 2 5 var start = data[0].x; 3 6 var end = data[data.length - 1].x; … … 33 36 pv.search.index(data, d2, function(d) d.x) + 1); 34 37 fx.domain(d1, d2); 35 fy.domain(scale.checked ? [0, pv.max(dd, function(d) d.y) ] : y.domain());38 fy.domain(scale.checked ? [0, pv.max(dd, function(d) d.y) * 1.1] : y.domain()); 36 39 return dd; 37 40 }) -
django_apps/running/templates/index.html
r291 r301 1 1 <ul> 2 2 {% for user in user_list %} 3 <li><a href="{% url running.views.user_index user. name %}">{{ user }}</li>3 <li><a href="{% url running.views.user_index user.username %}">{{ user }}</li> 4 4 {%endfor %} 5 5 </ul> -
django_apps/running/templates/user_index.html
r298 r301 20 20 </head> 21 21 <body> 22 <a href="{% url running.views.index %}">User Overview</a><p /> 23 <h1>Welcome {{ user }}</h1> 22 <table> 23 <tr> 24 <td> 25 <a href="{% url running.views.index %}">User Overview</a> 26 </td> 27 <td>-</td> 28 <td> 29 {% if user.is_authenticated %} 30 <form method="post" action="{% url logout %}"> 31 <input type="submit" value="logout" /> 32 {% csrf_token %} 33 <input type="hidden" name="next" value="{{ request.path }}" /> 34 </form> 35 {% else %} 36 <form method="post" action="{% url login %}"> 37 {% csrf_token %} 38 <table> 39 <tr> 40 <td><input type="text" name="username" /></td> 41 <td><input type="password" name="password" /></td> 42 <td><input type="submit" value="login" /></td> 43 </tr> 44 </table> 45 <input type="hidden" name="next" value="{{ request.path }}" /> 46 </form> 47 {% endif %} 48 </td> 49 </tr> 50 </table> 51 <h1>Welcome {{ user.username|default:"New User" }}</h1> 52 <a href="#results">Add new results</a> 53 24 54 <div id="center"> 25 <h3>Week totals </h3>55 <h3>Week totals in KM</h3> 26 56 <div id="fig2"> 27 57 <div id="week-statistics"> </div> 28 58 </div> 29 <h3>Accumulated Totals </h3>59 <h3>Accumulated Totals in KM</h3> 30 60 <div id="fig"> 31 61 <div style="text-align:right;padding-right:20;"> … … 35 65 <div id="total-statistics"> </div> 36 66 </div> 67 <h3>Avergage Speed in KM</h3> 68 <div id="fig"> 69 <div style="text-align:right;padding-right:20;"> 70 <input checked id="scale" type="checkbox" onchange="vis.render()" 71 ><label for="scale">Scale to fit</label> 72 </div> 73 <div id="speed-statistics"> </div> 74 </div> 37 75 </div> 76 {% if user.is_authenticated %} 77 {% if user.id == runninguser.id %} 38 78 <form method="post" action=""> 39 79 {% csrf_token %} … … 63 103 <input type="submit" value="Submit Values"/> 64 104 </form> 65 Beheer al je gegevens <a href="{% url admin:running_user_change user.id %}">hier</a>. 105 {% else %} 106 Bewerk je eigen gegevens <a href="{% url running.views.user_index user.username %}">hier</a>. 107 {% endif %} 108 {% else %} 109 Login om je gegevens toe te voegen. 110 {% endif %} 111 <a name="results" /> 66 112 67 113 … … 69 115 // Graph Week totals 70 116 var xhr = new XMLHttpRequest(); 71 xhr.open('GET', '{% url running.views.json_user_column user.id %}', false);117 xhr.open('GET', '{% url running.views.json_user_column runninguser.id %}', false); 72 118 xhr.send(null); 73 119 var myData = JSON.parse(xhr.responseText, function (key, value) { … … 81 127 // Graph Year totals 82 128 var xhr = new XMLHttpRequest(); 83 xhr.open('GET', '{% url running.views.json_user_total user.id %}', false);129 xhr.open('GET', '{% url running.views.json_user_total runninguser.id %}', false); 84 130 xhr.send(null); 85 131 … … 95 141 yearTotalGraph(data, 'total-statistics'); 96 142 </script> 143 144 145 <script type="text/javascript+protovis"> 146 // Graph Average Speed 147 var xhr = new XMLHttpRequest(); 148 xhr.open('GET', '{% url running.views.json_user_speed runninguser.id %}', false); 149 xhr.send(null); 150 151 var data = JSON.parse(xhr.responseText, function (key, value) { 152 var type; 153 if ( value != null) { 154 if ( key === 'x') { 155 return new Date(value); 156 } 157 } 158 return value; 159 }); 160 yearTotalGraph(data, 'speed-statistics'); 161 </script> 97 162 </body> 98 163 </html> -
django_apps/running/urls.py
r291 r301 8 8 url(r'^json/user/column/(\d+)/$', 'running.views.json_user_column'), 9 9 url(r'^json/user/total/(\d+)/$', 'running.views.json_user_total'), 10 url(r'^json/user/speed/(\d+)/$', 'running.views.json_user_speed'), 11 url(r'^accounts/login/$', 'django.contrib.auth.views.login', name='login' ), 12 url(r'^accounts/logout/$', 'django.contrib.auth.views.logout', name='logout'), 10 13 ) -
django_apps/running/views.py
r299 r301 4 4 from django.http import HttpResponse 5 5 from django.forms.models import modelformset_factory, inlineformset_factory 6 from django.forms.models import BaseModelFormSet 6 7 from django.template import RequestContext 7 8 8 9 from django.shortcuts import render_to_response, get_object_or_404 9 from running.models import Result, User10 from running.models import Result, RunningUser 10 11 import json 11 12 12 13 def user_index(request, user_name): 13 user = get_object_or_404(User,name=user_name)14 running_user = get_object_or_404(RunningUser, username=user_name) 14 15 ResultFormSet = modelformset_factory(Result, fields=('date', 'distance', 'time'), exclude=('user'), extra=3, can_delete=True) 15 16 if request.method == 'POST': 17 if not request.user.is_authenticated(): 18 return HttpResponseForbidden('Not Logged In!') 16 19 formset = ResultFormSet(request.POST, request.FILES) 17 20 if formset.is_valid(): 18 21 instances = formset.save(commit=False) 19 22 for instance in instances: 20 instance.user = user23 instance.user = running_user 21 24 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)) 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)) 26 31 27 32 def index(request): 28 user_list = User.objects.all()33 user_list = RunningUser.objects.all() 29 34 return render_to_response('index.html', { 'user_list' : user_list }) 30 35 … … 40 45 41 46 def json_user_column(request,user_id): 42 user = get_object_or_404( User, pk=user_id)47 user = get_object_or_404(RunningUser, pk=user_id) 43 48 result_list = Result.objects.filter(user=user).values_list('date', 'distance') 44 49 … … 59 64 60 65 def json_user_total(request,user_id): 61 result_list = Result.objects.values_list('date', 'distance') 66 user = get_object_or_404(RunningUser, pk=user_id) 67 result_list = Result.objects.filter(user=user).values_list('date', 'distance') 62 68 json_list = [] 63 69 distance_total = 0.0 … … 68 74 data = json.dumps(json_list, ensure_ascii=False, indent=2) 69 75 return HttpResponse(data,mimetype="application/json") 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")
Note:
See TracChangeset
for help on using the changeset viewer.