From 894cc172a816792e606bed47681f50163fa800a9 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 15 Jan 2016 18:17:35 +0100 Subject: [PATCH] Display student age at period start on attributions view --- stages/models.py | 9 ++++++++- stages/static/js/attribution.js | 18 ++++++++++-------- stages/views.py | 8 ++++++++ templates/student_summary.html | 2 +- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/stages/models.py b/stages/models.py index 6ed079e..b2a1360 100644 --- a/stages/models.py +++ b/stages/models.py @@ -1,4 +1,4 @@ -from datetime import date +from datetime import date, timedelta from django.db import models @@ -77,6 +77,13 @@ class Student(models.Model): def __str__(self): return '%s %s' % (self.last_name, self.first_name) + def age_at(self, date_): + """Return age of student at `date_` time, as a string.""" + age = (date.today() - self.birth_date) / timedelta(days=365.2425) + age_y = int(age) + age_m = int((age - age_y) * 12) + return '%d ans%s' % (age_y, ' %d m.' % age_m if age_m > 0 else '') + @classmethod def prepare_import(cls, values): ''' Hook for tabimport, before new object get created ''' diff --git a/stages/static/js/attribution.js b/stages/static/js/attribution.js index d23836f..7460c95 100644 --- a/stages/static/js/attribution.js +++ b/stages/static/js/attribution.js @@ -144,14 +144,16 @@ $(document).ready(function() { }); $('#student_select').change(function(ev) { - $('#student_detail').load('/student/' + $(this).val() + '/summary/', function() { - $('div#previous_stages_head').toggle(function() { - $('ul#previous_stages_list').toggle(); - $(this).find('img').attr('src', static_url + 'img/open.png'); - }, function() { - $('ul#previous_stages_list').toggle(); - $(this).find('img').attr('src', static_url + 'img/closed.png'); - }); + $('#student_detail').load( + '/student/' + $(this).val() + '/summary/?period=' + $.cookie('periode'), + function() { + $('div#previous_stages_head').toggle(function() { + $('ul#previous_stages_list').toggle(); + $(this).find('img').attr('src', static_url + 'img/open.png'); + }, function() { + $('ul#previous_stages_list').toggle(); + $(this).find('img').attr('src', static_url + 'img/closed.png'); + }); }).addClass("filled"); current_student = $(this).val(); if (current_avail !== null) $('input#valid_training').show(); diff --git a/stages/views.py b/stages/views.py index 5ece624..402e1de 100644 --- a/stages/views.py +++ b/stages/views.py @@ -102,6 +102,14 @@ class StudentSummaryView(DetailView): context = super(StudentSummaryView, self).get_context_data(**kwargs) context['previous_stages'] = self.object.training_set.all( ).select_related('availability__corporation').order_by('availability__period__end_date') + period_id = self.request.GET.get('period') + if period_id: + try: + period = Period.objects.get(pk=int(period_id)) + except Period.DoesNotExist: + pass + else: + context['age_for_stage'] = self.object.age_at(period.start_date) return context diff --git a/templates/student_summary.html b/templates/student_summary.html index 7a09d1c..26ad8eb 100644 --- a/templates/student_summary.html +++ b/templates/student_summary.html @@ -12,5 +12,5 @@
{{ object.first_name }} {{ object.last_name }}
{{ object.pcode }} {{ object.city }}
- Date de naissance: {{ object.birth_date }} + Date de naissance: {{ object.birth_date }} {% if age_for_stage %}({{ age_for_stage }}){% endif %}