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