From c4ffd72888d75d0541f506cc977f03304dfa398c Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 30 Nov 2012 16:49:54 +0100 Subject: [PATCH] Add number of references in the same school year for referents --- stages/static/js/attribution.js | 11 ++++++++++- stages/views.py | 24 ++++++++++++++++++++++-- templates/attribution.html | 2 +- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/stages/static/js/attribution.js b/stages/static/js/attribution.js index f04b2d7..8c2f016 100644 --- a/stages/static/js/attribution.js +++ b/stages/static/js/attribution.js @@ -87,12 +87,16 @@ function update_trainings(period_id) { var li = $(this).parents('li'); $.post('/training/del/', {pk: li.attr('id').split('_')[1], - csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val()}, function() { + csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val()}, function(data) { li.remove(); // dispatch student and corp in their listings update_students($('#period_select').val()); update_corporations($('#period_select').val()); set_export_visibility(); + // Decrement referent number + var referent = $('#referent_select option[value="' + data.ref_id + '"]') + var parsed = referent.text().match(/(.*)\((\d+)\)/); + referent.text(parsed[1] +' (' + (parseInt(parsed[2]) - 1) + ')'); }); }); set_export_visibility(); @@ -182,7 +186,12 @@ $(document).ready(function() { current_student = null; current_avail = null; $('input#valid_training').hide(); + + // Update referent select + var parsed = $('#referent_select option:selected').text().match(/(.*)\((\d+)\)/); + $('#referent_select option:selected').text(parsed[1] +' (' + (parseInt(parsed[2]) + 1) + ')'); $('#referent_select').val(''); + update_trainings($('#period_select').val()); } ); diff --git a/stages/views.py b/stages/views.py index e491ab3..27cd89a 100644 --- a/stages/views.py +++ b/stages/views.py @@ -2,7 +2,9 @@ from __future__ import unicode_literals import json +from datetime import date +from django.db.models import Count from django.http import HttpResponse, HttpResponseNotAllowed from django.shortcuts import get_object_or_404 from django.views.generic import DetailView, TemplateView, ListView @@ -11,6 +13,15 @@ from .forms import PeriodForm from .models import Section, Student, Corporation, Period, Training, Referent, Availability +def school_year_start(): + """ Return first official day of current school year """ + current_year = date.today().year + if date(current_year, 8, 1) > date.today(): + return date(current_year-1, 8, 1) + else: + return date(current_year, 8, 1) + + class StudentSummaryView(DetailView): model = Student template_name = 'student_summary.html' @@ -41,10 +52,17 @@ class AttributionView(TemplateView): def get_context_data(self, **kwargs): context = super(AttributionView, self).get_context_data(**kwargs) + # Need 2 queries, because referents with no training item would not appear in the second query + referents = Referent.objects.all().order_by('last_name', 'first_name') + ref_counts = dict([(ref.id, ref.num_refs) + for ref in Referent.objects.filter(training__availability__period__end_date__gte=school_year_start + ).annotate(num_refs=Count('training'))]) + for ref in referents: + ref.num_refs = ref_counts.get(ref.id, 0) context.update({ #'period_form': PeriodForm(), 'sections': Section.objects.all(), - 'referents': Referent.objects.all().order_by('last_name', 'first_name'), + 'referents': referents, }) return context @@ -100,11 +118,13 @@ def new_training(request): return HttpResponse('OK') def del_training(request): + """ Delete training and return the referent id """ if request.method != 'POST': return HttpResponseNotAllowed() training = get_object_or_404(Training, pk=request.POST.get('pk')) + ref_id = training.referent_id training.delete() - return HttpResponse('OK') + return HttpResponse(json.dumps({'ref_id': ref_id}), content_type="application/json") def stages_export(request): diff --git a/templates/attribution.html b/templates/attribution.html index 4b02668..94b22f9 100644 --- a/templates/attribution.html +++ b/templates/attribution.html @@ -82,7 +82,7 @@
{% csrf_token %}
+ {% for ref in referents %}{% endfor %}