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