Merge branch 'hotfix' into olivier
[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 from workflow import grp_drh, \
8 grp_drh2, \
9 grp_accior, \
10 grp_abf, \
11 grp_haute_direction, \
12 grp_service_utilisateurs
13
14 from rh.managers import DossierManager as RHDossierManager
15 from rh.managers import PosteManager as RHPosteManager
16 from rh.managers import PosteComparaisonManager as RHPosteComparaisonManager
17 from rh.managers import DossierComparaisonManager as RHDossierComparaisonManager
18
19
20 class TodoManagerMixin(object):
21
22 def mes_choses_a_faire(self, user):
23 q = Q()
24 employe = get_employe_from_user(user)
25 rien_a_faire = True
26 for g in user.groups.all():
27 etats = MAP_GROUPE_ETATS_A_FAIRE.get(g, ())
28 for etat in etats:
29 rien_a_faire = False
30 q2 = Q(etat=etat)
31 if g == grp_service_utilisateurs:
32 q2 &= Q(**{self.prefixe_service: employe.service})
33 elif g not in (grp_accior, grp_abf, grp_haute_direction, grp_drh, grp_drh2):
34 q2 &= Q(**{self.prefixe_implantation: employe.implantation.region})
35 q |= q2
36
37 if rien_a_faire:
38 qs = self.ma_region_ou_service(user).none()
39 else:
40 qs = self.ma_region_ou_service(user).filter(q)
41
42 return qs
43
44 class DossierManager(RHDossierManager, TodoManagerMixin):
45 pass
46
47 class PosteManager(RHPosteManager, TodoManagerMixin):
48 pass
49
50 class PosteComparaisonManager(RHPosteComparaisonManager, TodoManagerMixin):
51 pass
52
53 class DossierComparaisonManager(RHDossierComparaisonManager, TodoManagerMixin):
54 pass
55
56 class DossierFinaliseManager(models.Manager):
57 """
58 Dossiers finalisés, qui ne sont pas encore importés
59 """
60 def get_query_set(self):
61 from dae.models import ImportDossier
62 dae_importees = [str(i.dae_id) for i in ImportDossier.objects.all()]
63 qs = super(DossierFinaliseManager,
64 self).get_query_set().filter(etat=DOSSIER_ETAT_FINALISE,).extra(where=['id\
65 NOT IN (%s)' % ", ".join(dae_importees)])
66 return qs
67
68