select opt employe poste
[auf_rh_dae.git] / project / dae / catalogues.py
1 # -*- encoding: utf-8 -*-
2
3 from django.db.models import Q
4 from rh import models as rh
5 from utils import get_employe_from_user, is_user_dans_services_centraux
6 from workflow import grp_drh
7
8 class Responsable(object):
9 q = ""
10 def get_query(self,q,request):
11 if len(q) < 4:
12 return rh.Poste.objects.none()
13
14 self.q = q
15 postes = rh.Poste.objects.filter(
16 Q(nom__icontains=q) |
17 Q(type_poste__nom__icontains=q) |
18 Q(rh_dossiers__employe__nom__icontains=q) |
19 Q(rh_dossiers__employe__prenom__icontains=q)
20 ).distinct()
21 return postes
22
23 def format_result(self, poste):
24 q = self.q
25 filtre = Q(poste=poste) & (Q(poste__nom__icontains=q) | Q(employe__nom__icontains=q) | Q(employe__prenom__icontains=q))
26 dossiers = rh.Dossier.objects.filter(filtre)
27
28 # certains postes ont un id de type de poste qui n'existe pas
29 try:
30 nom_poste = poste.type_poste.nom
31 except:
32 nom_poste = ""
33
34 if len(dossiers) == 1:
35 dossier = dossiers[0]
36 if nom_poste == poste.nom:
37 return "[%s] %s (%s) (%s)" % (poste.implantation.id, nom_poste, poste.id, dossier.employe)
38 else:
39 return "[%s] %s %s (%s) (%s)" % (poste.implantation.id, nom_poste, poste.nom, poste.id, dossier.employe)
40 else:
41 dossiers = poste.rh_dossiers.all()
42 if len(dossiers) > 0:
43 complement1 = dossiers[0].poste.nom
44 employe = unicode(dossiers[0].employe)
45 return "[%s] %s %s (%s) (%s)" % (poste.implantation.id, nom_poste, complement1, poste.id, employe)
46
47 def format_item(self, poste):
48 """ the display of a currently selected object in the area below the search box. html is OK """
49 return self.format_result(poste)
50
51 def get_objects(self, ids):
52 """ given a list of ids, return the objects ordered as you would like them on the admin page.
53 this is for displaying the currently selected items (in the case of a ManyToMany field)
54 """
55 return rh.Poste.objects.filter(pk__in=ids)
56
57 class Dossier(object):
58
59 def get_query(self,q,request):
60 employe = get_employe_from_user(request.user)
61 prefixe_implantation = 'poste__implantation'
62
63 q_recherche = Q(poste__nom__icontains=q) | \
64 Q(poste__type_poste__nom__icontains=q) | \
65 Q(employe__nom__icontains=q) | \
66 Q(employe__prenom__icontains=q)
67
68 if is_user_dans_services_centraux(request.user):
69 q_place = Q(**{ '%s' % prefixe_implantation : employe.implantation })
70 else:
71 q_place = Q(**{ '%s__region' % prefixe_implantation : employe.implantation.region })
72
73
74 if grp_drh in request.user.groups.all():
75 q_filtre = q_recherche
76 else:
77 q_filtre = q_place & q_recherche
78 return rh.Dossier.objects.filter(q_filtre).distinct()
79
80 def format_result(self, dossier):
81 return dossier.__unicode__()
82
83 def format_item(self, dossier):
84 """ the display of a currently selected object in the area below the search box. html is OK """
85 return self.format_result(dossier)
86
87 def get_objects(self, ids):
88 """ given a list of ids, return the objects ordered as you would like them on the admin page.
89 this is for displaying the currently selected items (in the case of a ManyToMany field)
90 """
91 return rh.Dossier.objects.filter(pk__in=ids)
92
93 class Poste(object):
94
95 def get_query(self,q,request):
96 employe = get_employe_from_user(request.user)
97 prefixe_implantation = 'poste__implantation'
98
99 q_recherche = Q(poste__nom__icontains=q) | Q(poste__type_poste__nom__icontains=q)
100
101 if is_user_dans_services_centraux(request.user):
102 q_place = Q(**{ '%s' % prefixe_implantation : employe.implantation })
103 else:
104 q_place = Q(**{ '%s__region' % prefixe_implantation : employe.implantation.region })
105
106
107 if grp_drh in request.user.groups.all():
108 q_filtre = q_recherche
109 else:
110 q_filtre = q_place & q_recherche
111 return rh.Dossier.objects.filter(q_filtre).distinct()
112
113 def format_result(self, dossier):
114 return u"[%s] %s" % (dossier.poste.implantation, dossier.poste.type_poste.nom)
115
116 def format_item(self, dossier):
117 """ the display of a currently selected object in the area below the search box. html is OK """
118 return self.format_result(dossier)
119
120 def get_objects(self, ids):
121 """ given a list of ids, return the objects ordered as you would like them on the admin page.
122 this is for displaying the currently selected items (in the case of a ManyToMany field)
123 """
124 return rh.Dossier.objects.filter(pk__in=ids)