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