[#2190] S'assurer que les gens ne voient pas des comparaisons salariales qu'ils ne...
[auf_rh_dae.git] / project / dae / managers.py
1 # -*- encoding: utf-8 -*-
2
3 from django.db import models
4 from django.db.models import Q
5 from utils import get_employe_from_user
6 from workflow import POSTE_ETAT_HAUTE_DIRECTION, \
7 POSTE_ETAT_ACCIOR, \
8 POSTE_ETAT_ABF, \
9 DOSSIER_ETAT_ACCIOR, \
10 DOSSIER_ETAT_ABF, \
11 MAP_GROUPE_ETATS_A_FAIRE
12 from workflow import dae_groupes, \
13 grp_administrateurs, \
14 grp_directeurs_bureau, \
15 grp_drh, \
16 grp_accior, \
17 grp_abf, \
18 grp_haute_direction, \
19 grp_service_utilisateurs, \
20 grp_correspondants_rh
21
22
23 class SecurityManager(models.Manager):
24
25 prefixe_service = None
26 prefixe_implantation = None
27
28 def mes_choses_a_faire(self, user):
29 q = Q()
30 employe = get_employe_from_user(user)
31 rien_a_faire = True
32 for g in user.groups.all():
33 etats = MAP_GROUPE_ETATS_A_FAIRE.get(g, ())
34 for etat in etats:
35 rien_a_faire = False
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
42
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
48 return qs
49
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 """
56 employe = get_employe_from_user(user)
57
58 ############################################
59 # TRAITEMENT NORMAL
60 ############################################
61 # REGION
62 q = Q(**{ self.prefixe_implantation : employe.implantation.region })
63
64 # SERVICE
65 if self.prefixe_service and grp_service_utilisateurs in user.groups.all():
66 q = q | Q(**{ self.prefixe_service : employe.service})
67
68 liste = self.get_query_set().filter(q)
69
70 ############################################
71 # TRAITEMENT ACCIOR
72 ############################################
73 if grp_accior in user.groups.all():
74 liste = self.get_query_set().all()
75
76 ############################################
77 # TRAITEMENT ABF
78 ############################################
79 if grp_abf in user.groups.all():
80 liste = self.get_query_set().all()
81
82 ############################################
83 # TRAITEMENT HAUTE DIRECTION
84 ############################################
85 if grp_haute_direction in user.groups.all():
86 liste = self.get_query_set().all()
87
88 ############################################
89 # TRAITEMENT DRH
90 ############################################
91 if grp_drh in user.groups.all():
92 liste = self.get_query_set().all()
93
94 return liste
95
96
97 class PosteManager(SecurityManager):
98 """
99 Chargement de tous les objets FK existants sur chaque QuerySet.
100 """
101 prefixe_service = "service"
102 prefixe_implantation = "implantation__region"
103
104 def ma_region_ou_service(self, user):
105 return super(PosteManager, self).ma_region_ou_service(user).filter(actif=True)
106
107 def get_query_set(self):
108 fkeys = (
109 'id_rh',
110 'responsable',
111 'implantation',
112 'implantation.bureau_rattachement',
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
124 class DossierManager(SecurityManager):
125 prefixe_service = "poste__service"
126 prefixe_implantation = "poste__implantation__region"
127
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
135 def ma_region_ou_service(self, user):
136 return super(DossierManager, self).ma_region_ou_service(user).filter(poste__actif=True)
137
138
139 class PosteComparaisonManager(SecurityManager):
140 use_for_related_fields = True
141 prefixe_implantation = "implantation__region"
142
143 class DossierComparaisonManager(SecurityManager):
144 use_for_related_fields = True
145 prefixe_implantation = "implantation__region"