Commit | Line | Data |
---|---|---|
f258e4e7 OL |
1 | # -*- encoding: utf-8 -*- |
2 | ||
3 | from django.db import models | |
4 | from django.db.models import Q | |
5 | from utils import is_user_dans_service, get_employe_from_user | |
515124ec | 6 | from workflow import POSTE_ETAT_HAUTE_DIRECTION, POSTE_ETAT_POLE_FINANCIER, MAP_GROUPE_ETATS_A_FAIRE |
f258e4e7 OL |
7 | from workflow import dae_groupes, \ |
8 | grp_administrateurs, \ | |
9 | grp_gestionnaires, \ | |
10 | grp_directeurs_bureau, \ | |
11 | grp_drh, \ | |
12 | grp_pole_financier, \ | |
13 | grp_haute_direction, \ | |
14 | grp_service_utilisateurs, \ | |
15 | grp_directeurs_service, \ | |
16 | grp_correspondants_rh | |
17 | ||
515124ec | 18 | |
f258e4e7 OL |
19 | class SecurityManager(models.Manager): |
20 | ||
21 | prefixe_implantation = None | |
22 | ||
515124ec OL |
23 | def mes_choses_a_faire(self, user): |
24 | q = Q() | |
25 | for g in user.groups.all(): | |
26 | etats = MAP_GROUPE_ETATS_A_FAIRE.get(g, ()) | |
27 | for etat in etats: | |
28 | q = q | Q(etat=etat) | |
29 | ||
30 | qs = self.ma_region_ou_service(user).filter(q) | |
31 | return qs | |
32 | ||
f258e4e7 OL |
33 | def ma_region_ou_service(self, user): |
34 | """ | |
35 | Filtrage des postes en fonction du user connecté (region / service) | |
36 | On s'intéresse aussi au groupe auquel appartient le user car certains groupes | |
37 | peuvent tout voir. | |
38 | """ | |
39 | ||
072d6547 OL |
40 | # On s'assure que le manager travaille sur un modèle avec WF, autrement |
41 | # on ne teste pas sur l'état | |
42 | try: | |
43 | self.model._meta.get_field_by_name('etat') | |
44 | workflowed_model = True | |
45 | except: | |
46 | workflowed_model = False | |
47 | ||
f258e4e7 OL |
48 | employe = get_employe_from_user(user) |
49 | ||
50 | ############################################ | |
51 | # TRAITEMENT NORMAL | |
52 | ############################################ | |
53 | ||
54 | # SERVICE | |
55 | if is_user_dans_service(user): | |
56 | q = Q(**{ '%s' % self.prefixe_implantation : employe.implantation }) | |
57 | # REGION | |
58 | else: | |
59 | q = Q(**{ '%s__region' % self.prefixe_implantation : employe.implantation.region }) | |
60 | liste = self.get_query_set().filter(q) | |
61 | ||
62 | ############################################ | |
63 | # TRAITEMENT POLE FINANCIER | |
64 | ############################################ | |
072d6547 | 65 | if workflowed_model and grp_pole_financier in user.groups.all(): |
62cfa562 | 66 | liste = self.get_query_set().filter(etat=POSTE_ETAT_POLE_FINANCIER) |
f258e4e7 OL |
67 | |
68 | ############################################ | |
69 | # TRAITEMENT HAUTE DIRECTION | |
70 | ############################################ | |
072d6547 | 71 | if workflowed_model and grp_haute_direction in user.groups.all(): |
f258e4e7 OL |
72 | liste = self.get_query_set().filter(etat=POSTE_ETAT_HAUTE_DIRECTION) |
73 | ||
74 | ############################################ | |
75 | # TRAITEMENT DRH | |
76 | ############################################ | |
77 | if grp_drh in user.groups.all(): | |
78 | liste = self.get_query_set() | |
79 | ||
80 | return liste | |
81 | ||
82 | ||
83 | class PosteManager(SecurityManager): | |
84 | """ | |
85 | Chargement de tous les objets FK existants sur chaque QuerySet. | |
86 | """ | |
87 | prefixe_implantation = "implantation" | |
88 | ||
b15bf543 OL |
89 | def ma_region_ou_service(self, user): |
90 | return super(PosteManager, self).filter(actif=True) | |
91 | ||
f258e4e7 OL |
92 | def get_query_set(self): |
93 | fkeys = ( | |
94 | 'id_rh', | |
95 | 'responsable', | |
96 | 'implantation', | |
97 | 'type_poste', | |
98 | 'service', | |
99 | 'classement_min', | |
100 | 'classement_max', | |
101 | 'valeur_point_min', | |
102 | 'valeur_point_max', | |
103 | ) | |
104 | return super(PosteManager, self).get_query_set() \ | |
105 | .select_related(*fkeys).all() | |
106 | ||
107 | ||
108 | class DossierManager(SecurityManager): | |
109 | prefixe_implantation = "poste__implantation" | |
110 | ||
b15bf543 OL |
111 | def ma_region_ou_service(self, user): |
112 | return super(DossierManager, self).filter(poste__actif=True) | |
113 | ||
114 |