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