Merge branch 'olivier' into dev
[auf_rh_dae.git] / project / dae / managers.py
1 # -*- encoding: utf-8 -*-
2
3 from django.db import models
4 from django.db.models import Q
5 from utils import get_employe_from_user
6 from workflow import MAP_GROUPE_ETATS_A_FAIRE, DOSSIER_ETAT_FINALISE, \
7 POSTE_ETAT_FINALISE
8 from workflow import grp_drh, \
9 grp_drh2, \
10 grp_accior, \
11 grp_abf, \
12 grp_haute_direction, \
13 grp_service_utilisateurs
14
15 from rh.managers import DossierManager as RHDossierManager
16 from rh.managers import PosteManager as RHPosteManager
17 from rh.managers import PosteComparaisonManager as RHPosteComparaisonManager
18 from rh.managers import DossierComparaisonManager as RHDossierComparaisonManager
19
20
21 class TodoManagerMixin(object):
22
23 def mes_choses_a_faire(self, user):
24 q = Q()
25 employe = get_employe_from_user(user)
26 rien_a_faire = True
27 for g in user.groups.all():
28 etats = MAP_GROUPE_ETATS_A_FAIRE.get(g, ())
29 for etat in etats:
30 rien_a_faire = False
31 q2 = Q(etat=etat)
32 if g == grp_service_utilisateurs:
33 q2 &= Q(**{self.prefixe_service: employe.service})
34 elif g not in (grp_accior, grp_abf, grp_haute_direction, grp_drh, grp_drh2):
35 q2 &= Q(**{self.prefixe_implantation: employe.implantation.region})
36 q |= q2
37
38 if rien_a_faire:
39 qs = self.ma_region_ou_service(user).none()
40 else:
41 qs = self.ma_region_ou_service(user).filter(q)
42
43 return qs
44
45 class DossierManager(RHDossierManager, TodoManagerMixin):
46 pass
47
48 class PosteManager(RHPosteManager, TodoManagerMixin):
49 pass
50
51 class PosteComparaisonManager(RHPosteComparaisonManager, TodoManagerMixin):
52 pass
53
54 class DossierComparaisonManager(RHDossierComparaisonManager, TodoManagerMixin):
55 pass
56
57 class DossierFinaliseManager(models.Manager):
58 """
59 Dossiers finalisés, qui ne sont pas encore importés
60 """
61 def get_query_set(self):
62 from dae.models import ImportDossier
63 dae_importees = [str(i.dae_id) for i in ImportDossier.objects.all()]
64 qs = super(DossierFinaliseManager, self).get_query_set().all()
65 if len(dae_importees) > 0:
66 qs = qs.extra(where=['dae_dossier.id NOT IN (%s)' % ", ".join(dae_importees)])
67 return qs
68
69
70 class PosteFinaliseManager(models.Manager):
71 """
72 Dossiers finalisés, qui ne sont pas encore importés
73 """
74 def get_query_set(self):
75 from dae.models import ImportPoste
76 dae_importees = [str(i.dae_id) for i in ImportPoste.objects.all()]
77
78 qs = super(PosteFinaliseManager, self).get_query_set().all()
79 if len(dae_importees) > 0:
80 qs = qs.extra(where=['dae_poste.id NOT IN (%s)' % ", ".join(dae_importees)])
81 return qs
82
83