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