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