#4660
[auf_rh_dae.git] / project / dae / catalogues.py
CommitLineData
3121c13c
OL
1# -*- encoding: utf-8 -*-
2
3121c13c 3from django.db.models import Q
b31ce2d7 4
3383b2d1 5from project import groups
b31ce2d7
EMS
6from project.rh import models as rh
7
3121c13c
OL
8
9class Responsable(object):
196e2d22 10 q = ""
b31ce2d7
EMS
11
12 def get_query(self, q, request):
524ffcf0
OL
13 if len(q) < 4:
14 return rh.Poste.objects.none()
15
196e2d22 16 self.q = q
38340bbd
OL
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()]))
524ffcf0 33 return postes
3121c13c
OL
34
35 def format_result(self, poste):
196e2d22 36 q = self.q
b31ce2d7
EMS
37 filtre = Q(poste=poste) & (
38 Q(poste__nom__icontains=q) | Q(employe__nom__icontains=q) |
39 Q(employe__prenom__icontains=q)
40 )
38340bbd 41 dossiers = rh.Dossier.objects.actifs().filter(filtre)
b31ce2d7 42
388547a4 43 nom_poste = poste.nom
b31ce2d7 44
196e2d22
OL
45 if len(dossiers) == 1:
46 dossier = dossiers[0]
388547a4 47 employe = dossier.employe
8deddf9e 48 else:
38340bbd 49 dossiers = poste.rh_dossiers.all().order_by('-date_debut')
8deddf9e 50 if len(dossiers) > 0:
8deddf9e 51 employe = unicode(dossiers[0].employe)
388547a4
OL
52 else:
53 employe = ""
b31ce2d7
EMS
54 return "[%s] %s (%s) (%s)" % (
55 poste.implantation.id, nom_poste, poste.id, employe
56 )
3121c13c
OL
57
58 def format_item(self, poste):
b31ce2d7
EMS
59 """
60 the display of a currently selected object in the area below the
61 search box. html is OK
62 """
3121c13c
OL
63 return self.format_result(poste)
64
65 def get_objects(self, ids):
b31ce2d7
EMS
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)
3121c13c 70 """
03b395db
OL
71 return rh.Poste.objects.filter(pk__in=ids)
72
b31ce2d7 73
03b395db
OL
74class Dossier(object):
75
b31ce2d7 76 def get_query(self, q, request):
3383b2d1 77 employe = groups.get_employe_from_user(request.user)
09aa8374 78 prefixe_implantation = 'poste__implantation'
a4534f29 79
09aa8374
OL
80 q_recherche = Q(poste__nom__icontains=q) | \
81 Q(poste__type_poste__nom__icontains=q) | \
a4534f29 82 Q(employe__nom__icontains=q) | \
03b395db 83 Q(employe__prenom__icontains=q)
a4534f29 84
b0cf30b8
EMS
85 q_place = Q(**{
86 prefixe_implantation + '__zone_administrative':
87 employe.implantation.zone_administrative
88 })
a4534f29 89
3383b2d1
OL
90 user_groupes = [g.name for g in request.user.groups.all()]
91 if groups.DRH_NIVEAU_1 in user_groupes:
a4534f29
OL
92 q_filtre = q_recherche
93 else:
c0492570 94 q_filtre = q_place & q_recherche
a4534f29 95 return rh.Dossier.objects.filter(q_filtre).distinct()
03b395db
OL
96
97 def format_result(self, dossier):
98 return dossier.__unicode__()
99
100 def format_item(self, dossier):
b31ce2d7
EMS
101 """
102 the display of a currently selected object in the area below the
103 search box. html is OK
104 """
03b395db
OL
105 return self.format_result(dossier)
106
107 def get_objects(self, ids):
b31ce2d7
EMS
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)
03b395db
OL
112 """
113 return rh.Dossier.objects.filter(pk__in=ids)
068d1462 114
b31ce2d7 115
068d1462
OL
116class Poste(object):
117
b31ce2d7 118 def get_query(self, q, request):
3383b2d1 119 employe = groups.get_employe_from_user(request.user)
3f5cbabe 120 prefixe_implantation = 'poste__implantation'
068d1462 121
b31ce2d7
EMS
122 q_recherche = \
123 Q(poste__nom__icontains=q) | \
124 Q(poste__type_poste__nom__icontains=q)
068d1462 125
3383b2d1 126 if groups.is_user_dans_services_centraux(request.user):
b31ce2d7 127 q_place = Q(**{prefixe_implantation: employe.implantation})
068d1462 128 else:
b31ce2d7 129 q_place = Q(**{
b0cf30b8
EMS
130 prefixe_implantation + '__zone_administrative':
131 employe.implantation.zone_administrative
b31ce2d7 132 })
068d1462 133
3383b2d1 134 user_groupes = [g.name for g in request.user.groups.all()]
15da8a54
EMS
135 if groups.DRH_NIVEAU_1 in user_groupes or \
136 groups.DRH_NIVEAU_2 in user_groupes:
068d1462
OL
137 q_filtre = q_recherche
138 else:
139 q_filtre = q_place & q_recherche
e503e64d 140 return rh.Dossier.objects.filter(q_filtre).order_by('-date_debut')
068d1462
OL
141
142 def format_result(self, dossier):
e503e64d
OL
143 annee = dossier.date_debut.year
144 if dossier.date_fin is not None:
145 annee = dossier.date_fin.year
b31ce2d7
EMS
146 return u"[%s] %s %s" % (
147 dossier.poste.implantation, annee, dossier.poste.nom
148 )
068d1462
OL
149
150 def format_item(self, dossier):
b31ce2d7
EMS
151 """
152 the display of a currently selected object in the area below the
153 search box. html is OK
154 """
068d1462
OL
155 return self.format_result(dossier)
156
157 def get_objects(self, ids):
b31ce2d7
EMS
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)
068d1462
OL
162 """
163 return rh.Dossier.objects.filter(pk__in=ids)