[#3165] Remplacé les filtres et protection par région pour des filtres et protections...
[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
b31ce2d7 17 postes = rh.Poste.objects.filter(
3f5cbabe 18 Q(nom__icontains=q) |
3121c13c 19 Q(type_poste__nom__icontains=q) |
16b1454e
OL
20 Q(rh_dossiers__employe__nom__icontains=q) |
21 Q(rh_dossiers__employe__prenom__icontains=q)
3121c13c 22 ).distinct()
524ffcf0 23 return postes
3121c13c
OL
24
25 def format_result(self, poste):
196e2d22 26 q = self.q
b31ce2d7
EMS
27 filtre = Q(poste=poste) & (
28 Q(poste__nom__icontains=q) | Q(employe__nom__icontains=q) |
29 Q(employe__prenom__icontains=q)
30 )
196e2d22 31 dossiers = rh.Dossier.objects.filter(filtre)
b31ce2d7 32
388547a4 33 nom_poste = poste.nom
b31ce2d7 34
196e2d22
OL
35 if len(dossiers) == 1:
36 dossier = dossiers[0]
388547a4 37 employe = dossier.employe
8deddf9e 38 else:
16b1454e 39 dossiers = poste.rh_dossiers.all()
8deddf9e 40 if len(dossiers) > 0:
8deddf9e 41 employe = unicode(dossiers[0].employe)
388547a4
OL
42 else:
43 employe = ""
b31ce2d7
EMS
44 return "[%s] %s (%s) (%s)" % (
45 poste.implantation.id, nom_poste, poste.id, employe
46 )
3121c13c
OL
47
48 def format_item(self, poste):
b31ce2d7
EMS
49 """
50 the display of a currently selected object in the area below the
51 search box. html is OK
52 """
3121c13c
OL
53 return self.format_result(poste)
54
55 def get_objects(self, ids):
b31ce2d7
EMS
56 """
57 given a list of ids, return the objects ordered as you would like
58 them on the admin page. this is for displaying the currently
59 selected items (in the case of a ManyToMany field)
3121c13c 60 """
03b395db
OL
61 return rh.Poste.objects.filter(pk__in=ids)
62
b31ce2d7 63
03b395db
OL
64class Dossier(object):
65
b31ce2d7 66 def get_query(self, q, request):
3383b2d1 67 employe = groups.get_employe_from_user(request.user)
09aa8374 68 prefixe_implantation = 'poste__implantation'
a4534f29 69
09aa8374
OL
70 q_recherche = Q(poste__nom__icontains=q) | \
71 Q(poste__type_poste__nom__icontains=q) | \
a4534f29 72 Q(employe__nom__icontains=q) | \
03b395db 73 Q(employe__prenom__icontains=q)
a4534f29 74
b0cf30b8
EMS
75 q_place = Q(**{
76 prefixe_implantation + '__zone_administrative':
77 employe.implantation.zone_administrative
78 })
a4534f29 79
3383b2d1
OL
80 user_groupes = [g.name for g in request.user.groups.all()]
81 if groups.DRH_NIVEAU_1 in user_groupes:
a4534f29
OL
82 q_filtre = q_recherche
83 else:
c0492570 84 q_filtre = q_place & q_recherche
a4534f29 85 return rh.Dossier.objects.filter(q_filtre).distinct()
03b395db
OL
86
87 def format_result(self, dossier):
88 return dossier.__unicode__()
89
90 def format_item(self, dossier):
b31ce2d7
EMS
91 """
92 the display of a currently selected object in the area below the
93 search box. html is OK
94 """
03b395db
OL
95 return self.format_result(dossier)
96
97 def get_objects(self, ids):
b31ce2d7
EMS
98 """
99 given a list of ids, return the objects ordered as you would like
100 them on the admin page. this is for displaying the currently
101 selected items (in the case of a ManyToMany field)
03b395db
OL
102 """
103 return rh.Dossier.objects.filter(pk__in=ids)
068d1462 104
b31ce2d7 105
068d1462
OL
106class Poste(object):
107
b31ce2d7 108 def get_query(self, q, request):
3383b2d1 109 employe = groups.get_employe_from_user(request.user)
3f5cbabe 110 prefixe_implantation = 'poste__implantation'
068d1462 111
b31ce2d7
EMS
112 q_recherche = \
113 Q(poste__nom__icontains=q) | \
114 Q(poste__type_poste__nom__icontains=q)
068d1462 115
3383b2d1 116 if groups.is_user_dans_services_centraux(request.user):
b31ce2d7 117 q_place = Q(**{prefixe_implantation: employe.implantation})
068d1462 118 else:
b31ce2d7 119 q_place = Q(**{
b0cf30b8
EMS
120 prefixe_implantation + '__zone_administrative':
121 employe.implantation.zone_administrative
b31ce2d7 122 })
068d1462 123
3383b2d1
OL
124 user_groupes = [g.name for g in request.user.groups.all()]
125 if groups.DRH_NIVEAU_1 in user_groupes:
068d1462
OL
126 q_filtre = q_recherche
127 else:
128 q_filtre = q_place & q_recherche
e503e64d 129 return rh.Dossier.objects.filter(q_filtre).order_by('-date_debut')
068d1462
OL
130
131 def format_result(self, dossier):
e503e64d
OL
132 annee = dossier.date_debut.year
133 if dossier.date_fin is not None:
134 annee = dossier.date_fin.year
b31ce2d7
EMS
135 return u"[%s] %s %s" % (
136 dossier.poste.implantation, annee, dossier.poste.nom
137 )
068d1462
OL
138
139 def format_item(self, dossier):
b31ce2d7
EMS
140 """
141 the display of a currently selected object in the area below the
142 search box. html is OK
143 """
068d1462
OL
144 return self.format_result(dossier)
145
146 def get_objects(self, ids):
b31ce2d7
EMS
147 """
148 given a list of ids, return the objects ordered as you would like
149 them on the admin page. this is for displaying the currently
150 selected items (in the case of a ManyToMany field)
068d1462
OL
151 """
152 return rh.Dossier.objects.filter(pk__in=ids)