#2779
[auf_rh_dae.git] / project / rh / managers.py
CommitLineData
3f5cbabe
OL
1
2# -*- encoding: utf-8 -*-
3
2a5259fe 4import datetime
3f5cbabe
OL
5from django.db import models
6from django.db.models import Q
a2c3ad52 7from auf.django.metadata.managers import NoDeleteManager
3f5cbabe
OL
8from utils import get_employe_from_user
9from 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
21class 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
9817fed5
OL
70 if user.is_superuser:
71 liste = self.get_query_set().all()
72
3f5cbabe
OL
73 return liste
74
75
a2c3ad52 76class PosteManager(SecurityManager, NoDeleteManager):
3f5cbabe
OL
77 """
78 Chargement de tous les objets FK existants sur chaque QuerySet.
79 """
80 prefixe_service = "service"
81 prefixe_implantation = "implantation__region"
82
f614ca5c 83 def actifs(self):
2a5259fe
OL
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)
f614ca5c 86
3f5cbabe 87 def ma_region_ou_service(self, user):
f614ca5c 88 return super(PosteManager, self).ma_region_ou_service(user)
3f5cbabe 89
0b0545bd
OL
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()
3f5cbabe
OL
105
106
a2c3ad52 107class DossierManager(SecurityManager, NoDeleteManager):
3f5cbabe
OL
108 prefixe_service = "poste__service"
109 prefixe_implantation = "poste__implantation__region"
110
111 def get_query_set(self):
112 fkeys = (
113 'poste',
0b0545bd 114 'employe',
3f5cbabe
OL
115 )
116 return super(DossierManager, self).get_query_set() \
117 .select_related(*fkeys).all()
118
119 def ma_region_ou_service(self, user):
f614ca5c 120 return super(DossierManager, self).ma_region_ou_service(user)
3f5cbabe
OL
121
122
123class PosteComparaisonManager(SecurityManager):
124 use_for_related_fields = True
125 prefixe_implantation = "implantation__region"
126
4bdadf8b 127
3f5cbabe
OL
128class DossierComparaisonManager(SecurityManager):
129 use_for_related_fields = True
130 prefixe_implantation = "implantation__region"
4bdadf8b
OL
131
132
133class DeviseManager(NoDeleteManager):
134
135 def get_query_set(self):
136 return super(DeviseManager, self).get_query_set().filter(archive=False)