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