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