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