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