Add number of references in the same school year for referents

This commit is contained in:
Claude Paroz 2012-11-30 16:49:54 +01:00
parent 26452f9068
commit c4ffd72888
3 changed files with 33 additions and 4 deletions

View file

@ -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());
}
);

View file

@ -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):