Commit | Line | Data |
---|---|---|
f258e4e7 OL |
1 | # -*- encoding: utf-8 -*- |
2 | ||
3 | from django.db import models | |
4 | from django.db.models import Q | |
d8cfc3d5 | 5 | from utils import get_employe_from_user |
18c6d4c0 | 6 | from workflow import POSTE_ETAT_HAUTE_DIRECTION, \ |
d8cfc3d5 OL |
7 | POSTE_ETAT_ACCIOR, \ |
8 | POSTE_ETAT_AFB, \ | |
18c6d4c0 OL |
9 | POSTE_ETAT_REFUSE, \ |
10 | DOSSIER_ETAT_REFUSE, \ | |
11 | MAP_GROUPE_ETATS_A_FAIRE | |
f258e4e7 OL |
12 | from workflow import dae_groupes, \ |
13 | grp_administrateurs, \ | |
f258e4e7 OL |
14 | grp_directeurs_bureau, \ |
15 | grp_drh, \ | |
d8cfc3d5 OL |
16 | grp_accior, \ |
17 | grp_afb, \ | |
f258e4e7 OL |
18 | grp_haute_direction, \ |
19 | grp_service_utilisateurs, \ | |
f258e4e7 OL |
20 | grp_correspondants_rh |
21 | ||
515124ec | 22 | |
f258e4e7 OL |
23 | class 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() | |
bb59cdf6 | 30 | rien_a_faire = True |
515124ec OL |
31 | for g in user.groups.all(): |
32 | etats = MAP_GROUPE_ETATS_A_FAIRE.get(g, ()) | |
33 | for etat in etats: | |
bb59cdf6 | 34 | rien_a_faire = False |
515124ec OL |
35 | q = q | Q(etat=etat) |
36 | ||
bb59cdf6 OL |
37 | if rien_a_faire: |
38 | qs = self.ma_region_ou_service(user).none() | |
39 | else: | |
40 | qs = self.ma_region_ou_service(user).filter(q) | |
41 | ||
515124ec OL |
42 | return qs |
43 | ||
f258e4e7 OL |
44 | def ma_region_ou_service(self, user): |
45 | """ | |
46 | Filtrage des postes en fonction du user connecté (region / service) | |
47 | On s'intéresse aussi au groupe auquel appartient le user car certains groupes | |
48 | peuvent tout voir. | |
49 | """ | |
072d6547 OL |
50 | # On s'assure que le manager travaille sur un modèle avec WF, autrement |
51 | # on ne teste pas sur l'état | |
52 | try: | |
53 | self.model._meta.get_field_by_name('etat') | |
54 | workflowed_model = True | |
55 | except: | |
56 | workflowed_model = False | |
57 | ||
f258e4e7 OL |
58 | employe = get_employe_from_user(user) |
59 | ||
60 | ############################################ | |
61 | # TRAITEMENT NORMAL | |
62 | ############################################ | |
f258e4e7 | 63 | # SERVICE |
189b6306 OL |
64 | if workflowed_model and grp_service_utilisateurs in user.groups.all(): |
65 | q = Q(**{ self.prefixe_service : employe.service}) | |
f258e4e7 OL |
66 | # REGION |
67 | else: | |
189b6306 | 68 | q = Q(**{ self.prefixe_implantation : employe.implantation.region }) |
f258e4e7 | 69 | |
d8cfc3d5 OL |
70 | #if workflowed_model: |
71 | # q_non_refuse = ~Q(etat__in=(POSTE_ETAT_REFUSE, DOSSIER_ETAT_REFUSE,)) | |
72 | # q = q & q_non_refuse | |
3420143c OL |
73 | |
74 | liste = self.get_query_set().filter(q) | |
f258e4e7 | 75 | |
189b6306 | 76 | |
0adcf7d1 | 77 | ############################################ |
d8cfc3d5 | 78 | # TRAITEMENT ACCIOR |
0adcf7d1 | 79 | ############################################ |
d8cfc3d5 OL |
80 | if workflowed_model and grp_accior in user.groups.all(): |
81 | liste = self.get_query_set().filter(q_non_refuse) | |
82 | ||
83 | ############################################ | |
84 | # TRAITEMENT AFB | |
85 | ############################################ | |
86 | if workflowed_model and grp_afb in user.groups.all(): | |
0af51c94 | 87 | liste = self.get_query_set().filter(q_non_refuse) |
18c6d4c0 | 88 | |
0adcf7d1 OL |
89 | ############################################ |
90 | # TRAITEMENT HAUTE DIRECTION | |
91 | ############################################ | |
92 | if workflowed_model and grp_haute_direction in user.groups.all(): | |
0af51c94 | 93 | liste = self.get_query_set().filter(q_non_refuse) |
f258e4e7 OL |
94 | |
95 | ############################################ | |
96 | # TRAITEMENT DRH | |
97 | ############################################ | |
98 | if grp_drh in user.groups.all(): | |
99 | liste = self.get_query_set() | |
18c6d4c0 | 100 | |
f258e4e7 OL |
101 | return liste |
102 | ||
103 | ||
104 | class PosteManager(SecurityManager): | |
105 | """ | |
106 | Chargement de tous les objets FK existants sur chaque QuerySet. | |
107 | """ | |
189b6306 OL |
108 | prefixe_service = "service" |
109 | prefixe_implantation = "implantation__region" | |
f258e4e7 | 110 | |
b15bf543 | 111 | def ma_region_ou_service(self, user): |
18c6d4c0 | 112 | return super(PosteManager, self).ma_region_ou_service(user).filter(actif=True) |
b15bf543 | 113 | |
f258e4e7 OL |
114 | def get_query_set(self): |
115 | fkeys = ( | |
116 | 'id_rh', | |
117 | 'responsable', | |
118 | 'implantation', | |
119 | 'type_poste', | |
120 | 'service', | |
121 | 'classement_min', | |
122 | 'classement_max', | |
123 | 'valeur_point_min', | |
124 | 'valeur_point_max', | |
125 | ) | |
126 | return super(PosteManager, self).get_query_set() \ | |
127 | .select_related(*fkeys).all() | |
128 | ||
129 | ||
130 | class DossierManager(SecurityManager): | |
189b6306 OL |
131 | prefixe_service = "poste__service" |
132 | prefixe_implantation = "poste__implantation__region" | |
f258e4e7 | 133 | |
b15bf543 | 134 | def ma_region_ou_service(self, user): |
18c6d4c0 | 135 | return super(DossierManager, self).ma_region_ou_service(user).filter(poste__actif=True) |
b15bf543 OL |
136 | |
137 |