Changeset 301


Ignore:
Timestamp:
Apr 7, 2011, 8:56:04 PM (14 years ago)
Author:
Rick van der Zwet
Message:
  • Login/logout
  • Revised admin form admin form
Location:
django_apps/running
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • django_apps/running/admin.py

    r291 r301  
    55  fields = [ 'date', 'distance', 'time' ]
    66
    7 class UserAdmin(admin.ModelAdmin):
     7class RunningUserAdmin(admin.ModelAdmin):
    88  inlines = [ ResultInline, ]
    99  class Media:
    1010    css = { "all" : ("css/hide_admin_original.css",) }
    11 admin.site.register(User, UserAdmin)
     11admin.site.register(RunningUser, RunningUserAdmin)
    1212
    1313
  • django_apps/running/models.py

    r291 r301  
    22import datetime
    33
     4from django.contrib.auth.models import User
     5
    46# Create your models here.
    5 class User(models.Model):
    6   name = models.CharField(max_length=50)
    7   def __unicode__(self):
    8     return self.name
     7class RunningUser(User):
     8  class Meta:
     9    proxy = True
    910
    1011class Result(models.Model):
    1112  user = models.ForeignKey(User)
    1213  distance = models.DecimalField(max_digits=5,decimal_places=2)
    13   time = models.TimeField()
     14  time = models.TimeField(default='00:00:00')
    1415  date = models.DateField(default=datetime.date.today)
    1516
  • django_apps/running/static/year-total.js

    r300 r301  
    11function yearTotalGraph(data, binder) {
     2  if (data.length == 0) {
     3    return;
     4  }
    25  var start = data[0].x;
    36  var end = data[data.length - 1].x;
     
    3336                  pv.search.index(data, d2, function(d) d.x) + 1);
    3437          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());
    3639          return dd;
    3740        })
  • django_apps/running/templates/index.html

    r291 r301  
    11<ul>
    22{% 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>
    44{%endfor %}
    55</ul>
  • django_apps/running/templates/user_index.html

    r298 r301  
    2020  </head>
    2121  <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
    2454    <div id="center">
    25       <h3>Week totals</h3>
     55      <h3>Week totals in KM</h3>
    2656      <div id="fig2">
    2757        <div id="week-statistics"> </div>
    2858      </div>
    29       <h3>Accumulated Totals</h3>
     59      <h3>Accumulated Totals in KM</h3>
    3060      <div id="fig">
    3161        <div style="text-align:right;padding-right:20;">
     
    3565        <div id="total-statistics"> </div>
    3666      </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>
    3775    </div>
     76    {% if user.is_authenticated %}
     77    {% if user.id == runninguser.id %}
    3878    <form method="post" action="">
    3979    {% csrf_token %}
     
    63103    <input type="submit" value="Submit Values"/>
    64104    </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" />
    66112
    67113
     
    69115  // Graph Week totals
    70116  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);
    72118  xhr.send(null);
    73119  var myData = JSON.parse(xhr.responseText, function (key, value) {
     
    81127  // Graph Year totals
    82128  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);
    84130  xhr.send(null);
    85131 
     
    95141  yearTotalGraph(data, 'total-statistics');
    96142</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>
    97162  </body>
    98163</html>
  • django_apps/running/urls.py

    r291 r301  
    88    url(r'^json/user/column/(\d+)/$', 'running.views.json_user_column'),
    99    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'),
    1013)
  • django_apps/running/views.py

    r299 r301  
    44from django.http import HttpResponse
    55from django.forms.models import modelformset_factory, inlineformset_factory
     6from django.forms.models import BaseModelFormSet
    67from django.template import RequestContext
    78
    89from django.shortcuts import render_to_response, get_object_or_404
    9 from running.models import Result, User
     10from running.models import Result, RunningUser
    1011import json
    1112
    1213def 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)
    1415    ResultFormSet = modelformset_factory(Result, fields=('date', 'distance', 'time'), exclude=('user'), extra=3, can_delete=True)
    1516    if request.method == 'POST':
     17       if not request.user.is_authenticated():
     18          return HttpResponseForbidden('Not Logged In!')
    1619       formset = ResultFormSet(request.POST, request.FILES)
    1720       if formset.is_valid():
    1821         instances = formset.save(commit=False)
    1922         for instance in instances:
    20            instance.user = user
     23           instance.user = running_user
    2124           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))
    2631
    2732def index(request):
    28     user_list = User.objects.all()
     33    user_list = RunningUser.objects.all()
    2934    return render_to_response('index.html', { 'user_list' : user_list })
    3035
     
    4045
    4146def 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)
    4348    result_list = Result.objects.filter(user=user).values_list('date', 'distance')
    4449
     
    5964
    6065def 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')
    6268    json_list = []
    6369    distance_total = 0.0
     
    6874    data = json.dumps(json_list, ensure_ascii=False, indent=2)
    6975    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 TracChangeset for help on using the changeset viewer.