fix PJ
[auf_rh_dae.git] / project / dae / catalogues.py
1 # -*- encoding: utf-8 -*-
2
3 from django.db.models import Q
4 from rh import models as rh
5 from utils import get_employe_from_user, is_user_dans_services_centraux
6 from workflow import grp_drh
7
8 class Responsable(object):
9 q = ""
10 def get_query(self,q,request):
11 if len(q) < 4:
12 return rh.Poste.objects.none()
13
14 self.q = q
15 postes = rh.Poste.objects.filter(
16 Q(nom__icontains=q) |
17 Q(type_poste__nom__icontains=q) |
18 Q(rh_dossiers__employe__nom__icontains=q) |
19 Q(rh_dossiers__employe__prenom__icontains=q)
20 ).distinct()
21 return postes
22
23 def format_result(self, poste):
24 q = self.q
25 filtre = Q(poste=poste) & (Q(poste__nom__icontains=q) | Q(employe__nom__icontains=q) | Q(employe__prenom__icontains=q))
26 dossiers = rh.Dossier.objects.filter(filtre)
27
28 nom_poste = poste.nom
29
30 if len(dossiers) == 1:
31 dossier = dossiers[0]
32 employe = dossier.employe
33 else:
34 dossiers = poste.rh_dossiers.all()
35 if len(dossiers) > 0:
36 employe = unicode(dossiers[0].employe)
37 else:
38 employe = ""
39 return "[%s] %s (%s) (%s)" % (poste.implantation.id, nom_poste, poste.id, employe)
40
41 def format_item(self, poste):
42 """ the display of a currently selected object in the area below the search box. html is OK """
43 return self.format_result(poste)
44
45 def get_objects(self, ids):
46 """ given a list of ids, return the objects ordered as you would like them on the admin page.
47 this is for displaying the currently selected items (in the case of a ManyToMany field)
48 """
49 return rh.Poste.objects.filter(pk__in=ids)
50
51 class Dossier(object):
52
53 def get_query(self,q,request):
54 employe = get_employe_from_user(request.user)
55 prefixe_implantation = 'poste__implantation'
56
57 q_recherche = Q(poste__nom__icontains=q) | \
58 Q(poste__type_poste__nom__icontains=q) | \
59 Q(employe__nom__icontains=q) | \
60 Q(employe__prenom__icontains=q)
61
62 if is_user_dans_services_centraux(request.user):
63 q_place = Q(**{ '%s' % prefixe_implantation : employe.implantation })
64 else:
65 q_place = Q(**{ '%s__region' % prefixe_implantation : employe.implantation.region })
66
67
68 if grp_drh in request.user.groups.all():
69 q_filtre = q_recherche
70 else:
71 q_filtre = q_place & q_recherche
72 return rh.Dossier.objects.filter(q_filtre).distinct()
73
74 def format_result(self, dossier):
75 return dossier.__unicode__()
76
77 def format_item(self, dossier):
78 """ the display of a currently selected object in the area below the search box. html is OK """
79 return self.format_result(dossier)
80
81 def get_objects(self, ids):
82 """ given a list of ids, return the objects ordered as you would like them on the admin page.
83 this is for displaying the currently selected items (in the case of a ManyToMany field)
84 """
85 return rh.Dossier.objects.filter(pk__in=ids)
86
87 class Poste(object):
88
89 def get_query(self,q,request):
90 employe = get_employe_from_user(request.user)
91 prefixe_implantation = 'poste__implantation'
92
93 q_recherche = Q(poste__nom__icontains=q) | Q(poste__type_poste__nom__icontains=q)
94
95 if is_user_dans_services_centraux(request.user):
96 q_place = Q(**{ '%s' % prefixe_implantation : employe.implantation })
97 else:
98 q_place = Q(**{ '%s__region' % prefixe_implantation : employe.implantation.region })
99
100
101 if grp_drh in request.user.groups.all():
102 q_filtre = q_recherche
103 else:
104 q_filtre = q_place & q_recherche
105 return rh.Dossier.objects.filter(q_filtre).order_by('-date_debut')
106
107 def format_result(self, dossier):
108 annee = dossier.date_debut.year
109 if dossier.date_fin is not None:
110 annee = dossier.date_fin.year
111 return u"[%s] %s %s" % (dossier.poste.implantation, annee, dossier.poste.nom)
112
113 def format_item(self, dossier):
114 """ the display of a currently selected object in the area below the search box. html is OK """
115 return self.format_result(dossier)
116
117 def get_objects(self, ids):
118 """ given a list of ids, return the objects ordered as you would like them on the admin page.
119 this is for displaying the currently selected items (in the case of a ManyToMany field)
120 """
121 return rh.Dossier.objects.filter(pk__in=ids)