591ffec3c6cbc294a1d6debcc083cb153b5cb21f
[auf_rh_dae.git] / project / rh / managers.py
1
2 # -*- encoding: utf-8 -*-
3
4 from django.db import models
5 from django.db.models import Q
6 from utils import get_employe_from_user
7 from workflow import dae_groupes, \
8 grp_administrateurs, \
9 grp_directeurs_bureau, \
10 grp_drh, \
11 grp_drh2, \
12 grp_accior, \
13 grp_abf, \
14 grp_haute_direction, \
15 grp_service_utilisateurs, \
16 grp_correspondants_rh
17
18
19 class SecurityManager(models.Manager):
20
21 prefixe_service = None
22 prefixe_implantation = None
23
24 def ma_region_ou_service(self, user):
25 """
26 Filtrage des postes en fonction du user connecté (region / service)
27 On s'intéresse aussi au groupe auquel appartient le user car certains groupes
28 peuvent tout voir.
29 """
30 employe = get_employe_from_user(user)
31
32 ############################################
33 # TRAITEMENT NORMAL
34 ############################################
35 # REGION
36 q = Q(**{ self.prefixe_implantation : employe.implantation.region })
37
38 # SERVICE
39 if self.prefixe_service and grp_service_utilisateurs in user.groups.all():
40 q = q | Q(**{ self.prefixe_service : employe.service})
41
42 liste = self.get_query_set().filter(q)
43
44 ############################################
45 # TRAITEMENT ACCIOR
46 ############################################
47 if grp_accior in user.groups.all():
48 liste = self.get_query_set().all()
49
50 ############################################
51 # TRAITEMENT ABF
52 ############################################
53 if grp_abf in user.groups.all():
54 liste = self.get_query_set().all()
55
56 ############################################
57 # TRAITEMENT HAUTE DIRECTION
58 ############################################
59 if grp_haute_direction in user.groups.all():
60 liste = self.get_query_set().all()
61
62 ############################################
63 # TRAITEMENT DRH
64 ############################################
65 if grp_drh in user.groups.all() or grp_drh2 in user.groups.all():
66 liste = self.get_query_set().all()
67
68 return liste
69
70
71 class PosteManager(SecurityManager):
72 """
73 Chargement de tous les objets FK existants sur chaque QuerySet.
74 """
75 prefixe_service = "service"
76 prefixe_implantation = "implantation__region"
77
78 def ma_region_ou_service(self, user):
79 return super(PosteManager, self).ma_region_ou_service(user).filter(actif=True)
80
81 def get_query_set(self):
82 fkeys = (
83 'id_rh',
84 'responsable',
85 'implantation',
86 'implantation.bureau_rattachement',
87 'type_poste',
88 'service',
89 'classement_min',
90 'classement_max',
91 'valeur_point_min',
92 'valeur_point_max',
93 )
94 return super(PosteManager, self).get_query_set() \
95 .select_related(*fkeys).all()
96
97
98 class DossierManager(SecurityManager):
99 prefixe_service = "poste__service"
100 prefixe_implantation = "poste__implantation__region"
101
102 def get_query_set(self):
103 fkeys = (
104 'poste',
105 )
106 return super(DossierManager, self).get_query_set() \
107 .select_related(*fkeys).all()
108
109 def ma_region_ou_service(self, user):
110 return super(DossierManager, self).ma_region_ou_service(user).filter(poste__actif=True)
111
112
113 class PosteComparaisonManager(SecurityManager):
114 use_for_related_fields = True
115 prefixe_implantation = "implantation__region"
116
117 class DossierComparaisonManager(SecurityManager):
118 use_for_related_fields = True
119 prefixe_implantation = "implantation__region"