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 | |
3420143c OL |
64 | if workflowed_model: |
65 | q_non_refuse = ~Q(etat__in=(POSTE_ETAT_REFUSE, DOSSIER_ETAT_REFUSE,)) | |
66 | q = q & q_non_refuse | |
67 | ||
68 | liste = self.get_query_set().filter(q) | |
f258e4e7 | 69 | |
18c6d4c0 OL |
70 | # Il peut être bon que ces personnes connaissent l'état d'avancement du poste ou dossier |
71 | ############################################# | |
72 | ## TRAITEMENT POLE FINANCIER | |
73 | ############################################# | |
74 | #if workflowed_model and grp_pole_financier in user.groups.all(): | |
75 | # liste = self.get_query_set().filter(etat=POSTE_ETAT_POLE_FINANCIER) | |
76 | ||
77 | ############################################# | |
78 | ## TRAITEMENT HAUTE DIRECTION | |
79 | ############################################# | |
80 | #if workflowed_model and grp_haute_direction in user.groups.all(): | |
81 | # liste = self.get_query_set().filter(etat=POSTE_ETAT_HAUTE_DIRECTION) | |
f258e4e7 OL |
82 | |
83 | ############################################ | |
84 | # TRAITEMENT DRH | |
85 | ############################################ | |
86 | if grp_drh in user.groups.all(): | |
87 | liste = self.get_query_set() | |
18c6d4c0 | 88 | |
f258e4e7 OL |
89 | return liste |
90 | ||
91 | ||
92 | class PosteManager(SecurityManager): | |
93 | """ | |
94 | Chargement de tous les objets FK existants sur chaque QuerySet. | |
95 | """ | |
96 | prefixe_implantation = "implantation" | |
97 | ||
b15bf543 | 98 | def ma_region_ou_service(self, user): |
18c6d4c0 | 99 | return super(PosteManager, self).ma_region_ou_service(user).filter(actif=True) |
b15bf543 | 100 | |
f258e4e7 OL |
101 | def get_query_set(self): |
102 | fkeys = ( | |
103 | 'id_rh', | |
104 | 'responsable', | |
105 | 'implantation', | |
106 | 'type_poste', | |
107 | 'service', | |
108 | 'classement_min', | |
109 | 'classement_max', | |
110 | 'valeur_point_min', | |
111 | 'valeur_point_max', | |
112 | ) | |
113 | return super(PosteManager, self).get_query_set() \ | |
114 | .select_related(*fkeys).all() | |
115 | ||
116 | ||
117 | class DossierManager(SecurityManager): | |
118 | prefixe_implantation = "poste__implantation" | |
119 | ||
b15bf543 | 120 | def ma_region_ou_service(self, user): |
18c6d4c0 | 121 | return super(DossierManager, self).ma_region_ou_service(user).filter(poste__actif=True) |
b15bf543 OL |
122 | |
123 |