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