#4660
[auf_rh_dae.git] / project / dae / catalogues.py
1 # -*- encoding: utf-8 -*-
2
3 from django.db.models import Q
4
5 from project import groups
6 from project.rh import models as rh
7
8
9 class Responsable(object):
10 q = ""
11
12 def get_query(self, q, request):
13 if len(q) < 4:
14 return rh.Poste.objects.none()
15
16 self.q = q
17 filtre = (
18 Q(poste__nom__icontains=q) |
19 Q(poste__type_poste__nom__icontains=q) |
20 Q(employe__nom__icontains=q) |
21 Q(employe__prenom__icontains=q)
22 )
23
24 dossiers = rh.Dossier.objects.\
25 select_related('poste', 'poste__type_poste').\
26 actifs().\
27 filter(filtre)
28 q = q.lower()
29 postes = list(set([d.poste for d in dossiers if q in
30 unicode(d.employe).lower() or \
31 q in d.poste.nom.lower() or \
32 q in d.poste.type_poste.nom.lower()]))
33 return postes
34
35 def format_result(self, poste):
36 q = self.q
37 filtre = Q(poste=poste) & (
38 Q(poste__nom__icontains=q) | Q(employe__nom__icontains=q) |
39 Q(employe__prenom__icontains=q)
40 )
41 dossiers = rh.Dossier.objects.actifs().filter(filtre)
42
43 nom_poste = poste.nom
44
45 if len(dossiers) == 1:
46 dossier = dossiers[0]
47 employe = dossier.employe
48 else:
49 dossiers = poste.rh_dossiers.all().order_by('-date_debut')
50 if len(dossiers) > 0:
51 employe = unicode(dossiers[0].employe)
52 else:
53 employe = ""
54 return "[%s] %s (%s) (%s)" % (
55 poste.implantation.id, nom_poste, poste.id, employe
56 )
57
58 def format_item(self, poste):
59 """
60 the display of a currently selected object in the area below the
61 search box. html is OK
62 """
63 return self.format_result(poste)
64
65 def get_objects(self, ids):
66 """
67 given a list of ids, return the objects ordered as you would like
68 them on the admin page. this is for displaying the currently
69 selected items (in the case of a ManyToMany field)
70 """
71 return rh.Poste.objects.filter(pk__in=ids)
72
73
74 class Dossier(object):
75
76 def get_query(self, q, request):
77 employe = groups.get_employe_from_user(request.user)
78 prefixe_implantation = 'poste__implantation'
79
80 q_recherche = Q(poste__nom__icontains=q) | \
81 Q(poste__type_poste__nom__icontains=q) | \
82 Q(employe__nom__icontains=q) | \
83 Q(employe__prenom__icontains=q)
84
85 q_place = Q(**{
86 prefixe_implantation + '__zone_administrative':
87 employe.implantation.zone_administrative
88 })
89
90 user_groupes = [g.name for g in request.user.groups.all()]
91 if groups.DRH_NIVEAU_1 in user_groupes:
92 q_filtre = q_recherche
93 else:
94 q_filtre = q_place & q_recherche
95 return rh.Dossier.objects.filter(q_filtre).distinct()
96
97 def format_result(self, dossier):
98 return dossier.__unicode__()
99
100 def format_item(self, dossier):
101 """
102 the display of a currently selected object in the area below the
103 search box. html is OK
104 """
105 return self.format_result(dossier)
106
107 def get_objects(self, ids):
108 """
109 given a list of ids, return the objects ordered as you would like
110 them on the admin page. this is for displaying the currently
111 selected items (in the case of a ManyToMany field)
112 """
113 return rh.Dossier.objects.filter(pk__in=ids)
114
115
116 class Poste(object):
117
118 def get_query(self, q, request):
119 employe = groups.get_employe_from_user(request.user)
120 prefixe_implantation = 'poste__implantation'
121
122 q_recherche = \
123 Q(poste__nom__icontains=q) | \
124 Q(poste__type_poste__nom__icontains=q)
125
126 if groups.is_user_dans_services_centraux(request.user):
127 q_place = Q(**{prefixe_implantation: employe.implantation})
128 else:
129 q_place = Q(**{
130 prefixe_implantation + '__zone_administrative':
131 employe.implantation.zone_administrative
132 })
133
134 user_groupes = [g.name for g in request.user.groups.all()]
135 if groups.DRH_NIVEAU_1 in user_groupes or \
136 groups.DRH_NIVEAU_2 in user_groupes:
137 q_filtre = q_recherche
138 else:
139 q_filtre = q_place & q_recherche
140 return rh.Dossier.objects.filter(q_filtre).order_by('-date_debut')
141
142 def format_result(self, dossier):
143 annee = dossier.date_debut.year
144 if dossier.date_fin is not None:
145 annee = dossier.date_fin.year
146 return u"[%s] %s %s" % (
147 dossier.poste.implantation, annee, dossier.poste.nom
148 )
149
150 def format_item(self, dossier):
151 """
152 the display of a currently selected object in the area below the
153 search box. html is OK
154 """
155 return self.format_result(dossier)
156
157 def get_objects(self, ids):
158 """
159 given a list of ids, return the objects ordered as you would like
160 them on the admin page. this is for displaying the currently
161 selected items (in the case of a ManyToMany field)
162 """
163 return rh.Dossier.objects.filter(pk__in=ids)