fix for 500 error
[auf_rh_dae.git] / project / dae / catalogues.py
... / ...
CommitLineData
1# -*- encoding: utf-8 -*-
2
3import datetime
4
5from django.db.models import Q
6from project import groups
7from project.rh import models as rh
8
9
10class Responsable(object):
11 q = ""
12
13 def get_query(self, q, request):
14 if len(q) < 4:
15 return rh.Poste.objects.none()
16
17 self.q = q
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()]))
34 return postes
35
36 def format_result(self, poste):
37 q = self.q
38 filtre = Q(poste=poste) & (
39 Q(poste__nom__icontains=q) | Q(employe__nom__icontains=q) |
40 Q(employe__prenom__icontains=q)
41 )
42 dossiers = rh.Dossier.objects.actifs().filter(filtre)
43
44 nom_poste = poste.nom
45
46 if len(dossiers) == 1:
47 dossier = dossiers[0]
48 employe = dossier.employe
49 else:
50 dossiers = poste.rh_dossiers.all().order_by('-date_debut')
51 if len(dossiers) > 0:
52 employe = unicode(dossiers[0].employe)
53 else:
54 employe = ""
55 return "[%s] %s (%s) (%s)" % (
56 poste.implantation.id, nom_poste, poste.id, employe
57 )
58
59 def format_item(self, poste):
60 """
61 the display of a currently selected object in the area below the
62 search box. html is OK
63 """
64 return self.format_result(poste)
65
66 def get_objects(self, ids):
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)
71 """
72 return rh.Poste.objects.filter(pk__in=ids)
73
74
75class Dossier(object):
76
77 def get_query(self, q, request):
78 employe = groups.get_employe_from_user(request.user)
79 prefixe_implantation = 'poste__implantation'
80
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 )
87
88 q_place = Q(**{
89 prefixe_implantation + '__zone_administrative':
90 employe.implantation.zone_administrative
91 })
92
93
94 user_groupes = [g.name for g in request.user.groups.all()]
95 if groups.DRH_NIVEAU_1 in user_groupes:
96 q_filtre = q_recherche
97 else:
98 q_filtre = q_place & q_recherche
99 return rh.Dossier.objects.ma_region_ou_service(request.user).filter(q_filtre).distinct()
100
101 def format_result(self, dossier):
102 return dossier.__unicode__()
103
104 def format_item(self, dossier):
105 """
106 the display of a currently selected object in the area below the
107 search box. html is OK
108 """
109 return self.format_result(dossier)
110
111 def get_objects(self, ids):
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)
116 """
117 return rh.Dossier.objects.filter(pk__in=ids)
118
119
120class Poste(object):
121
122 def get_query(self, q, request):
123 employe = groups.get_employe_from_user(request.user)
124 prefixe_implantation = 'poste__implantation'
125
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 ))
135
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 # })
143
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')
155
156 def format_result(self, dossier):
157 annee = dossier.date_debut.year
158 if dossier.date_fin is not None:
159 annee = dossier.date_fin.year
160 return u"[%s] %s %s" % (
161 dossier.poste.implantation, annee, dossier.poste.nom
162 )
163
164 def format_item(self, dossier):
165 """
166 the display of a currently selected object in the area below the
167 search box. html is OK
168 """
169 return self.format_result(dossier)
170
171 def get_objects(self, ids):
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)
176 """
177 return rh.Dossier.objects.filter(pk__in=ids)