Merge branch 'reversion'
[auf_rh_dae.git] / project / rh / managers.py
1 # -*- encoding: utf-8 -*-
2
3 import datetime
4 from datetime import date
5
6 from auf.django.metadata.managers import NoDeleteManager, NoDeleteQuerySet
7 from django.db import models
8 from django.db.models import Q
9
10 from project.groups import get_employe_from_user
11 from project.groups import \
12 grp_drh, grp_drh2, grp_accior, grp_abf, grp_haute_direction, \
13 grp_service_utilisateurs
14
15
16 class 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)
24 On s'intéresse aussi au groupe auquel appartient le user car
25 certains groupes peuvent tout voir.
26 """
27 employe = get_employe_from_user(user)
28
29 ############################################
30 # TRAITEMENT NORMAL
31 ############################################
32 # REGION
33 q = Q(**{self.prefixe_implantation: employe.implantation.region})
34
35 # SERVICE
36 if self.prefixe_service \
37 and grp_service_utilisateurs in user.groups.all():
38 q = q | Q(**{self.prefixe_service: employe.service})
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
66 if user.is_superuser:
67 liste = self.get_query_set().all()
68
69 return liste
70
71
72 class PosteManager(SecurityManager):
73 """
74 Chargement de tous les objets FK existants sur chaque QuerySet.
75 """
76 prefixe_service = "service"
77 prefixe_implantation = "implantation__region"
78
79 def actifs(self):
80 return super(PosteManager, self).get_query_set().filter(
81 Q(date_fin__gt=datetime.datetime.now()) |
82 Q(date_fin__isnull=True)
83 )
84
85 def ma_region_ou_service(self, user):
86 return super(PosteManager, self).ma_region_ou_service(user)
87
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()
103
104
105 class DossierManager(SecurityManager):
106 prefixe_service = "poste__service"
107 prefixe_implantation = "poste__implantation__region"
108
109 def get_query_set(self):
110 fkeys = (
111 'poste',
112 'employe',
113 )
114 return super(DossierManager, self).get_query_set() \
115 .select_related(*fkeys).all()
116
117 def ma_region_ou_service(self, user):
118 return super(DossierManager, self).ma_region_ou_service(user)
119
120
121 class EmployeQuerySet(NoDeleteQuerySet):
122
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(
134 Q(rh_dossiers__date_fin__gte=date_min) |
135 Q(rh_dossiers__date_fin=None)
136 )
137 if date_max:
138 qs = qs.filter(
139 Q(rh_dossiers__date_debut__lte=date_max) |
140 Q(rh_dossiers__date_debut=None)
141 )
142 return qs.distinct()
143
144
145 class EmployeManager(NoDeleteManager):
146 def get_query_set(self):
147 return EmployeQuerySet(self.model)
148
149 def actifs(self, *args, **kwargs):
150 return self.get_query_set().actifs(*args, **kwargs)
151
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 TypeRemunerationManager(models.Manager):
164 pass