[#2355] Ajouter un état de validation: DRH-2
[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_drh2, \
17 grp_accior, \
18 grp_abf, \
19 grp_haute_direction, \
20 grp_service_utilisateurs, \
21 grp_correspondants_rh
22
23
24 class SecurityManager(models.Manager):
25
26 prefixe_service = None
27 prefixe_implantation = None
28
29 def mes_choses_a_faire(self, user):
30 q = Q()
31 employe = get_employe_from_user(user)
32 rien_a_faire = True
33 for g in user.groups.all():
34 etats = MAP_GROUPE_ETATS_A_FAIRE.get(g, ())
35 for etat in etats:
36 rien_a_faire = False
37 q2 = Q(etat=etat)
38 if g == grp_service_utilisateurs:
39 q2 &= Q(**{self.prefixe_service: employe.service})
40 elif g not in (grp_accior, grp_abf, grp_haute_direction, grp_drh, grp_drh2):
41 q2 &= Q(**{self.prefixe_implantation: employe.implantation.region})
42 q |= q2
43
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
49 return qs
50
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 """
57 employe = get_employe_from_user(user)
58
59 ############################################
60 # TRAITEMENT NORMAL
61 ############################################
62 # REGION
63 q = Q(**{ self.prefixe_implantation : employe.implantation.region })
64
65 # SERVICE
66 if self.prefixe_service and grp_service_utilisateurs in user.groups.all():
67 q = q | Q(**{ self.prefixe_service : employe.service})
68
69 liste = self.get_query_set().filter(q)
70
71 ############################################
72 # TRAITEMENT ACCIOR
73 ############################################
74 if grp_accior in user.groups.all():
75 liste = self.get_query_set().all()
76
77 ############################################
78 # TRAITEMENT ABF
79 ############################################
80 if grp_abf in user.groups.all():
81 liste = self.get_query_set().all()
82
83 ############################################
84 # TRAITEMENT HAUTE DIRECTION
85 ############################################
86 if grp_haute_direction in user.groups.all():
87 liste = self.get_query_set().all()
88
89 ############################################
90 # TRAITEMENT DRH
91 ############################################
92 if grp_drh in user.groups.all() or grp_drh2 in user.groups.all():
93 liste = self.get_query_set().all()
94
95 return liste
96
97
98 class PosteManager(SecurityManager):
99 """
100 Chargement de tous les objets FK existants sur chaque QuerySet.
101 """
102 prefixe_service = "service"
103 prefixe_implantation = "implantation__region"
104
105 def ma_region_ou_service(self, user):
106 return super(PosteManager, self).ma_region_ou_service(user).filter(actif=True)
107
108 def get_query_set(self):
109 fkeys = (
110 'id_rh',
111 'responsable',
112 'implantation',
113 'implantation.bureau_rattachement',
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
125 class DossierManager(SecurityManager):
126 prefixe_service = "poste__service"
127 prefixe_implantation = "poste__implantation__region"
128
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
136 def ma_region_ou_service(self, user):
137 return super(DossierManager, self).ma_region_ou_service(user).filter(poste__actif=True)
138
139
140 class PosteComparaisonManager(SecurityManager):
141 use_for_related_fields = True
142 prefixe_implantation = "implantation__region"
143
144 class DossierComparaisonManager(SecurityManager):
145 use_for_related_fields = True
146 prefixe_implantation = "implantation__region"