Merge branch 'master' into hotfix
[auf_rh_dae.git] / project / rh / managers.py
1 # -*- encoding: utf-8 -*-
2
3 from datetime import date
4
5 from django.db import models
6 from django.db.models import Q
7 from django.db.models.query import QuerySet
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 \
13 grp_drh, grp_drh2, grp_accior, grp_abf, grp_haute_direction, \
14 grp_service_utilisateurs
15
16
17 class SecurityManager(models.Manager):
18
19 prefixe_service = None
20 prefixe_implantation = None
21
22 def ma_region_ou_service(self, user):
23 """
24 Filtrage des postes en fonction du user connecté (region / service)
25 On s'intéresse aussi au groupe auquel appartient le user car
26 certains groupes peuvent tout voir.
27 """
28 employe = get_employe_from_user(user)
29
30 ############################################
31 # TRAITEMENT NORMAL
32 ############################################
33 # REGION
34 q = Q(**{self.prefixe_implantation: employe.implantation.region})
35
36 # SERVICE
37 if self.prefixe_service \
38 and grp_service_utilisateurs in user.groups.all():
39 q = q | Q(**{self.prefixe_service: employe.service})
40
41 liste = self.get_query_set().filter(q)
42
43 ############################################
44 # TRAITEMENT ACCIOR
45 ############################################
46 if grp_accior in user.groups.all():
47 liste = self.get_query_set().all()
48
49 ############################################
50 # TRAITEMENT ABF
51 ############################################
52 if grp_abf in user.groups.all():
53 liste = self.get_query_set().all()
54
55 ############################################
56 # TRAITEMENT HAUTE DIRECTION
57 ############################################
58 if grp_haute_direction in user.groups.all():
59 liste = self.get_query_set().all()
60
61 ############################################
62 # TRAITEMENT DRH
63 ############################################
64 if grp_drh in user.groups.all() or grp_drh2 in user.groups.all():
65 liste = self.get_query_set().all()
66
67 if user.is_superuser:
68 liste = self.get_query_set().all()
69
70 return liste
71
72
73 class ActifsQuerySet(QuerySet):
74
75 def _actifs(self, debut_field, fin_field, date_min=None, date_max=None,
76 annee=None):
77 qs = self
78 if annee:
79 janvier = date(annee, 1, 1)
80 decembre = date(annee, 12, 31)
81 date_min = max(janvier, date_min) if date_min else janvier
82 date_max = min(decembre, date_max) if date_max else decembre
83 if not date_min and not date_max:
84 date_min = date_max = date.today()
85 if date_min:
86 qs = qs.filter(
87 Q(**{fin_field + '__gte': date_min}) |
88 Q(**{fin_field: None})
89 )
90 if date_max:
91 qs = qs.filter(
92 Q(**{debut_field + '__lte': date_max}) |
93 Q(**{debut_field: None})
94 )
95 return qs
96
97 def actifs(self, *args, **kwargs):
98 return self._actifs('date_debut', 'date_fin', *args, **kwargs)
99
100
101 class PosteQuerySet(NoDeleteQuerySet, ActifsQuerySet):
102 pass
103
104
105 class PosteManager(SecurityManager, NoDeleteManager):
106 prefixe_service = "service"
107 prefixe_implantation = "implantation__region"
108
109 def get_query_set(self):
110 return PosteQuerySet(self.model).filter(supprime=False) \
111 .select_related('type_poste')
112
113 def actifs(self, *args, **kwargs):
114 return self.get_query_set().actifs(*args, **kwargs)
115
116 def ma_region_ou_service(self, user):
117 return super(PosteManager, self).ma_region_ou_service(user)
118
119
120 class DossierQuerySet(NoDeleteQuerySet, ActifsQuerySet):
121 pass
122
123
124 class DossierManager(SecurityManager, NoDeleteManager):
125 prefixe_service = "poste__service"
126 prefixe_implantation = "poste__implantation__region"
127
128 def get_query_set(self):
129 return DossierQuerySet(self.model) \
130 .filter(supprime=False) \
131 .select_related('poste', 'employe')
132
133 def actifs(self, *args, **kwargs):
134 return self.get_query_set().actifs(*args, **kwargs)
135
136
137 class RemunerationQuerySet(NoDeleteQuerySet, ActifsQuerySet):
138
139 def actifs(self, *args, **kwargs):
140 return self \
141 ._actifs('date_debut', 'date_fin', *args, **kwargs) \
142 ._actifs(
143 'dossier__date_debut', 'dossier__date_fin', *args, **kwargs
144 )
145
146
147 class RemunerationManager(NoDeleteManager):
148
149 def get_query_set(self):
150 return RemunerationQuerySet(self.model).filter(supprime=False)
151
152 def actifs(self, *args, **kwargs):
153 return self.get_query_set().actifs(*args, **kwargs)
154
155
156 class EmployeQuerySet(NoDeleteQuerySet, ActifsQuerySet):
157
158 def actifs(self, date_min=None, date_max=None, annee=None):
159 return self \
160 ._actifs('rh_dossiers__date_debut', 'rh_dossiers__date_fin') \
161 .distinct()
162
163
164 class EmployeManager(NoDeleteManager):
165 def get_query_set(self):
166 return EmployeQuerySet(self.model) \
167 .filter(supprime=False)
168
169 def actifs(self, *args, **kwargs):
170 return self.get_query_set().actifs(*args, **kwargs)
171
172
173 class PosteComparaisonManager(SecurityManager):
174 use_for_related_fields = True
175 prefixe_implantation = "implantation__region"
176
177
178 class DossierComparaisonManager(SecurityManager):
179 use_for_related_fields = True
180 prefixe_implantation = "implantation__region"
181
182
183 class DeviseManager(NoDeleteManager):
184 pass
185
186
187 class ServiceManager(NoDeleteManager):
188 pass
189
190
191 class TypeRemunerationManager(NoDeleteManager):
192 pass