Filtres sur rapport poste
[auf_rh_dae.git] / project / rh / views.py
1 # -*- encoding: utf-8 -*-
2 from datetime import date
3
4 from django.contrib.auth.decorators import login_required
5 from django.utils.encoding import smart_str
6 from django.shortcuts import redirect, render_to_response, get_object_or_404
7 from django.template import RequestContext
8
9 from datamaster_modeles import models as ref
10 from project.lib import get_employe_from_id
11
12 from rh import models as rh
13
14 # homes
15 @login_required
16 def profil(request):
17 """Profil personnel de l'employé - éditable"""
18 rc = RequestContext(request)
19 c = {}
20
21 employe = rc['this_employe']
22
23 c['user'] = request.user
24 c['employe'] = employe
25 return render_to_response('rh/profil.html', c, rc)
26
27 # employes
28 @login_required
29 def employes_liste(request):
30 """Liste des employés."""
31 today = date.today()
32 employes = rh.Employe.objects \
33 .filter(actif=True, supprime=False) \
34 .filter(dossiers__actif=True, dossiers__supprime=False) \
35 .exclude(dossiers__date_debut__gt=today) \
36 .exclude(dossiers__date_fin__lt=today) \
37 .order_by('nom')
38 c = {
39 'user':request.user,
40 'employes':employes,
41 }
42 return render_to_response('rh/employes_liste.html', c, RequestContext(request))
43
44 @login_required
45 def employe(request, id):
46 """Information publique sur un employé."""
47 try:
48 employe = rh.Employe.objects.get(pk=id)
49 except:
50 employe = rh.Employe.objects.none()
51 c = {
52 'user':request.user,
53 'employe':employe,
54 }
55 return render_to_response('rh/employe.html', c, RequestContext(request))
56
57
58 # Rapports
59
60 @login_required
61 def rapports_poste(request):
62
63 lookup_params = dict(request.GET.items())
64
65 for key, value in lookup_params.items():
66 if not isinstance(key, str):
67 # 'key' will be used as a keyword argument later, so Python
68 # requires it to be a string.
69 del lookup_params[key]
70 lookup_params[smart_str(key)] = value
71
72 postes = rh.Poste.actifs.select_related('implantation').all()
73 postes = postes.filter(**lookup_params)
74
75 poste_ids = set([p.pk for p in postes])
76
77 dossiers = rh.Dossier.objects.select_related('poste', 'employe').filter(poste__in=poste_ids)
78
79 employes = dict((d.poste.id, d.employe) for d in dossiers)
80
81 out = []
82
83 for p in postes:
84 line = {}
85 out.append(line)
86
87 line['id'] = p.id
88 line['nom'] = p.nom
89 line['implantation'] = p.implantation.nom
90 employe = employes.get(p.id)
91 if employe:
92 line['employe_id'] = employe.id
93 line['employe_nom'] = employe.nom
94 line['employe_prenom'] = employe.prenom
95
96 c = {
97 'title': 'Rapport des postes',
98 'postes': out,
99 }
100
101 return render_to_response('rh/rapports/postes.html', c, RequestContext(request))