Bugfix: la haute direction ne pouvait pas valider une DAE.
[auf_rh_dae.git] / project / dae / managers.py
CommitLineData
f258e4e7
OL
1# -*- encoding: utf-8 -*-
2
3from django.db import models
4from django.db.models import Q
d8cfc3d5 5from utils import get_employe_from_user
18c6d4c0 6from workflow import POSTE_ETAT_HAUTE_DIRECTION, \
d8cfc3d5 7 POSTE_ETAT_ACCIOR, \
9a62bc55 8 POSTE_ETAT_ABF, \
8ae5fbb1 9 DOSSIER_ETAT_ACCIOR, \
9a62bc55 10 DOSSIER_ETAT_ABF, \
18c6d4c0 11 MAP_GROUPE_ETATS_A_FAIRE
f258e4e7
OL
12from workflow import dae_groupes, \
13 grp_administrateurs, \
f258e4e7
OL
14 grp_directeurs_bureau, \
15 grp_drh, \
d8cfc3d5 16 grp_accior, \
9a62bc55 17 grp_abf, \
f258e4e7
OL
18 grp_haute_direction, \
19 grp_service_utilisateurs, \
f258e4e7
OL
20 grp_correspondants_rh
21
515124ec 22
f258e4e7
OL
23class SecurityManager(models.Manager):
24
189b6306 25 prefixe_service = None
f258e4e7
OL
26 prefixe_implantation = None
27
515124ec
OL
28 def mes_choses_a_faire(self, user):
29 q = Q()
291bbfd9 30 employe = get_employe_from_user(user)
bb59cdf6 31 rien_a_faire = True
515124ec
OL
32 for g in user.groups.all():
33 etats = MAP_GROUPE_ETATS_A_FAIRE.get(g, ())
34 for etat in etats:
bb59cdf6 35 rien_a_faire = False
291bbfd9
EMS
36 q2 = Q(etat=etat)
37 if g == grp_service_utilisateurs:
38 q2 &= Q(**{self.prefixe_service: employe.service})
39 elif g not in (grp_accior, grp_abf, grp_haute_direction, grp_drh):
40 q2 &= Q(**{self.prefixe_implantation: employe.implantation.region})
41 q |= q2
515124ec 42
bb59cdf6
OL
43 if rien_a_faire:
44 qs = self.ma_region_ou_service(user).none()
45 else:
46 qs = self.ma_region_ou_service(user).filter(q)
47
515124ec
OL
48 return qs
49
f258e4e7
OL
50 def ma_region_ou_service(self, user):
51 """
52 Filtrage des postes en fonction du user connecté (region / service)
53 On s'intéresse aussi au groupe auquel appartient le user car certains groupes
54 peuvent tout voir.
55 """
f258e4e7
OL
56 employe = get_employe_from_user(user)
57
58 ############################################
59 # TRAITEMENT NORMAL
757c0fd5 60 ############################################
8ae5fbb1
OL
61 # REGION
62 q = Q(**{ self.prefixe_implantation : employe.implantation.region })
63
f258e4e7 64 # SERVICE
320d7584 65 if self.prefixe_service and grp_service_utilisateurs in user.groups.all():
8ae5fbb1 66 q = q | Q(**{ self.prefixe_service : employe.service})
f258e4e7 67
3420143c 68 liste = self.get_query_set().filter(q)
f258e4e7 69
0adcf7d1 70 ############################################
d8cfc3d5 71 # TRAITEMENT ACCIOR
757c0fd5 72 ############################################
320d7584 73 if grp_accior in user.groups.all():
757c0fd5 74 liste = self.get_query_set().all()
d8cfc3d5
OL
75
76 ############################################
9a62bc55 77 # TRAITEMENT ABF
757c0fd5 78 ############################################
320d7584 79 if grp_abf in user.groups.all():
bbb2458d 80 liste = self.get_query_set().all()
18c6d4c0 81
0adcf7d1
OL
82 ############################################
83 # TRAITEMENT HAUTE DIRECTION
757c0fd5 84 ############################################
320d7584 85 if grp_haute_direction in user.groups.all():
bbb2458d 86 liste = self.get_query_set().all()
f258e4e7
OL
87
88 ############################################
89 # TRAITEMENT DRH
757c0fd5 90 ############################################
f258e4e7 91 if grp_drh in user.groups.all():
bbb2458d 92 liste = self.get_query_set().all()
18c6d4c0 93
f258e4e7
OL
94 return liste
95
96
97class PosteManager(SecurityManager):
98 """
99 Chargement de tous les objets FK existants sur chaque QuerySet.
100 """
189b6306
OL
101 prefixe_service = "service"
102 prefixe_implantation = "implantation__region"
f258e4e7 103
b15bf543 104 def ma_region_ou_service(self, user):
18c6d4c0 105 return super(PosteManager, self).ma_region_ou_service(user).filter(actif=True)
b15bf543 106
f258e4e7
OL
107 def get_query_set(self):
108 fkeys = (
109 'id_rh',
110 'responsable',
111 'implantation',
0b2edb6e 112 'implantation.bureau_rattachement',
f258e4e7
OL
113 'type_poste',
114 'service',
115 'classement_min',
116 'classement_max',
117 'valeur_point_min',
118 'valeur_point_max',
119 )
120 return super(PosteManager, self).get_query_set() \
121 .select_related(*fkeys).all()
122
123
124class DossierManager(SecurityManager):
189b6306
OL
125 prefixe_service = "poste__service"
126 prefixe_implantation = "poste__implantation__region"
f258e4e7 127
0b2edb6e
OL
128 def get_query_set(self):
129 fkeys = (
130 'poste',
131 )
132 return super(DossierManager, self).get_query_set() \
133 .select_related(*fkeys).all()
134
b15bf543 135 def ma_region_ou_service(self, user):
18c6d4c0 136 return super(DossierManager, self).ma_region_ou_service(user).filter(poste__actif=True)
b15bf543
OL
137
138
1de3da13
EMS
139class PosteComparaisonManager(SecurityManager):
140 use_for_related_fields = True
141 prefixe_implantation = "implantation__region"
320d7584
EMS
142
143class DossierComparaisonManager(SecurityManager):
144 use_for_related_fields = True
145 prefixe_implantation = "implantation__region"