debug ajout resp implantation
[auf_rh_dae.git] / project / rh / managers.py
1 # -*- encoding: utf-8 -*-
2
3 import datetime
4 from datetime import date
5
6 from django.db import models
7 from django.db.models import Q
8
9 from auf.django.metadata.managers import NoDeleteManager, NoDeleteQuerySet
10
11 from project.groups import get_employe_from_user
12 from project.groups import grp_administrateurs, \
13 grp_directeurs_bureau, \
14 grp_drh, \
15 grp_drh2, \
16 grp_accior, \
17 grp_abf, \
18 grp_haute_direction, \
19 grp_service_utilisateurs, \
20 grp_correspondants_rh
21
22
23 class SecurityManager(models.Manager):
24
25 prefixe_service = None
26 prefixe_implantation = None
27
28 def ma_region_ou_service(self, user):
29 """
30 Filtrage des postes en fonction du user connecté (region / service)
31 On s'intéresse aussi au groupe auquel appartient le user car certains groupes
32 peuvent tout voir.
33 """
34 employe = get_employe_from_user(user)
35
36 ############################################
37 # TRAITEMENT NORMAL
38 ############################################
39 # REGION
40 q = Q(**{ self.prefixe_implantation : employe.implantation.region })
41
42 # SERVICE
43 if self.prefixe_service and grp_service_utilisateurs in user.groups.all():
44 q = q | Q(**{ self.prefixe_service : employe.service})
45
46 liste = self.get_query_set().filter(q)
47
48 ############################################
49 # TRAITEMENT ACCIOR
50 ############################################
51 if grp_accior in user.groups.all():
52 liste = self.get_query_set().all()
53
54 ############################################
55 # TRAITEMENT ABF
56 ############################################
57 if grp_abf in user.groups.all():
58 liste = self.get_query_set().all()
59
60 ############################################
61 # TRAITEMENT HAUTE DIRECTION
62 ############################################
63 if grp_haute_direction in user.groups.all():
64 liste = self.get_query_set().all()
65
66 ############################################
67 # TRAITEMENT DRH
68 ############################################
69 if grp_drh in user.groups.all() or grp_drh2 in user.groups.all():
70 liste = self.get_query_set().all()
71
72 if user.is_superuser:
73 liste = self.get_query_set().all()
74
75 return liste
76
77
78 class PosteManager(SecurityManager, NoDeleteManager):
79 """
80 Chargement de tous les objets FK existants sur chaque QuerySet.
81 """
82 prefixe_service = "service"
83 prefixe_implantation = "implantation__region"
84
85 def actifs(self):
86 q_actif = Q(date_fin__gt=datetime.datetime.now()) | Q(date_fin__isnull=True)
87 return super(PosteManager, self).get_query_set().filter(q_actif)
88
89 def ma_region_ou_service(self, user):
90 return super(PosteManager, self).ma_region_ou_service(user)
91
92 def get_query_set(self):
93 fkeys = (
94 #'id_rh',
95 #'responsable',
96 #'implantation',
97 #'implantation.bureau_rattachement',
98 'type_poste',
99 #'service',
100 #'classement_min',
101 #'classement_max',
102 #'valeur_point_min',
103 #'valeur_point_max',
104 )
105 return super(PosteManager, self).get_query_set() \
106 .select_related(*fkeys).all()
107
108
109 class DossierManager(SecurityManager, NoDeleteManager):
110 prefixe_service = "poste__service"
111 prefixe_implantation = "poste__implantation__region"
112
113 def get_query_set(self):
114 fkeys = (
115 'poste',
116 'employe',
117 )
118 return super(DossierManager, self).get_query_set() \
119 .select_related(*fkeys).all()
120
121 def ma_region_ou_service(self, user):
122 return super(DossierManager, self).ma_region_ou_service(user)
123
124
125 class EmployeQuerySet(NoDeleteQuerySet):
126 def actifs(self, date_min=None, date_max=None, annee=None):
127 qs = self
128 if annee:
129 janvier = date(annee, 1, 1)
130 decembre = date(annee, 12, 31)
131 date_min = max(janvier, date_min) if date_min else janvier
132 date_max = min(decembre, date_max) if date_max else decembre
133 if not date_min and not date_max:
134 date_min = date_max = date.today()
135 if date_min:
136 qs = qs.filter(
137 Q(rh_dossiers__date_fin__gte=date_min) | Q(rh_dossiers__date_fin=None)
138 )
139 if date_max:
140 qs = qs.filter(
141 Q(rh_dossiers__date_debut__lte=date_max) | Q(rh_dossiers__date_debut=None)
142 )
143 return qs.distinct()
144
145 class EmployeManager(NoDeleteManager):
146 def get_query_set(self):
147 return EmployeQuerySet(self.model) \
148 .filter(supprime=False)
149
150 def actifs(self, *args, **kwargs):
151 return self.get_query_set().actifs(*args, **kwargs)
152
153 class PosteComparaisonManager(SecurityManager):
154 use_for_related_fields = True
155 prefixe_implantation = "implantation__region"
156
157
158 class DossierComparaisonManager(SecurityManager):
159 use_for_related_fields = True
160 prefixe_implantation = "implantation__region"
161
162
163 class DeviseManager(NoDeleteManager):
164 pass
165
166 class ServiceManager(NoDeleteManager):
167 pass
168
169 class TypeRemunerationManager(NoDeleteManager):
170 pass