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