Merge branch 'master' into dev
[auf_rh_dae.git] / project / dae / admin.py
CommitLineData
17353922
OL
1# -*- encoding: utf-8 -*-
2
460771c1 3from django import forms
b1e49599 4
47d7067b 5from django.core.urlresolvers import reverse
b1e49599 6from django.core.mail import mail_admins
47d7067b
OL
7from django.contrib import admin
8from django.contrib import messages
9from django.shortcuts import redirect
17353922 10from reversion.admin import VersionAdmin
460771c1 11from auf.django.workflow.models import WorkflowCommentaire
a47ed016 12from models import Poste, Dossier, DossierFinalise, PosteFinalise
71aec2c3 13from rh.decorators import in_drh_or_admin
746d94af 14
17353922 15class PosteAdmin(VersionAdmin):
b473fbb6 16 list_display = ('nom', 'implantation', 'etat', )
515124ec 17
a6245d0f 18
4be5ead0
OL
19class DossierAdmin(VersionAdmin):
20 list_display = ('_poste', '_implantation', 'employe', 'etat', )
317ce433 21 list_filter = ('etat', )
460771c1 22 exclude = ('etat', )
4be5ead0
OL
23
24 def _poste(self, obj):
25 return obj.poste.nom
26
27 def _implantation(self, obj):
28 return obj.poste.implantation
17353922 29
a6245d0f 30
460771c1
OL
31class ProxyDossierStatut(Dossier):
32 class Meta:
33 proxy = True
34 verbose_name = "Statut du dossier"
35 verbose_name_plural = "Statut des dossiers"
36
37
a6245d0f
OL
38class ProxyPosteStatut(Poste):
39 class Meta:
40 proxy = True
41 verbose_name = "Statut du poste"
42 verbose_name_plural = "Statut des postes"
43
44
45class StatutForm(forms.ModelForm):
460771c1
OL
46 commentaire = forms.CharField(label="Commentaire",
47 widget=forms.widgets.Textarea())
48
460771c1 49 def __init__(self, *args, **kwargs):
a6245d0f 50 super(StatutForm, self).__init__(*args, **kwargs)
460771c1
OL
51 # On retire le controle check des états pour faire ce qu'on veut
52 self._meta.model.__setattr__ = object.__setattr__
53
54 # on prépare le commentaire
55 self.wf = WorkflowCommentaire()
56 self.wf.etat_initial = self.instance.etat
57 self.wf.owner_id = self.user.id
746d94af 58
460771c1
OL
59 def save(self, *args, **kwargs):
60 # Le dossier se voit attribuée un commentaire WF verbeux
a6245d0f 61 obj = super(StatutForm, self).save(*args, **kwargs)
460771c1
OL
62 self.wf.content_object = obj
63 self.wf.etat_final = obj.etat
df625f22
OL
64 commentaire = u"%s => %s : %s" % (
65 self.wf.get_etat_label(self.wf.etat_initial),
66 self.wf.get_etat_label(self.wf.etat_final),
460771c1
OL
67 self.data['commentaire'])
68 self.wf.texte = commentaire
69 self.wf.save()
70 return obj
460771c1 71
460771c1 72
a6245d0f 73class StatutDossierForm(StatutForm):
460771c1 74
a6245d0f
OL
75 class Meta:
76 model = ProxyDossierStatut
77 fields = ('etat', )
460771c1 78
460771c1 79
a6245d0f 80class StatutPosteForm(StatutForm):
04a96961 81
a6245d0f
OL
82 class Meta:
83 model = ProxyPosteStatut
84 fields = ('etat', )
460771c1 85
a6245d0f
OL
86
87class StatutAdmin(VersionAdmin):
88 action = None
460771c1
OL
89
90 def has_delete_permission(self, request, obj=None):
91 return False
92
93 def has_add_permission(self, request):
94 return False
95
96 def has_change_permission(self, request, obj=None):
97 return request.user.is_superuser
98
99 def get_form(self, request, obj=None, **kwargs):
a6245d0f 100 form = super(StatutAdmin, self).get_form(request,
460771c1
OL
101 obj, **kwargs)
102 form.user = request.user
103 return form
104
a6245d0f
OL
105 def _etat(self, obj):
106 return obj.get_etat_display()
df625f22
OL
107 _etat.short_description = "État"
108 _etat.admin_order_field = "etat"
a6245d0f
OL
109
110class DossierStatutAdmin(StatutAdmin):
111 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
112 list_display = ('_poste', '_implantation', 'employe', '_etat', )
df625f22 113 list_filter = ('poste__implantation', )
a6245d0f
OL
114 form = StatutDossierForm
115
116 def _poste(self, obj):
117 return obj.poste.nom
118
119 def _implantation(self, obj):
120 return obj.poste.implantation
df625f22
OL
121 _implantation.short_description = u"Implantation"
122 _implantation.admin_order_field = "poste__implantation"
a6245d0f 123
a6245d0f
OL
124class PosteStatutAdmin(StatutAdmin):
125 search_fields = ('nom', )
df625f22
OL
126 list_display = ('nom', 'implantation', )
127 list_filter = ('implantation', 'etat', )
a6245d0f
OL
128 form = StatutPosteForm
129
130
a47ed016 131class ImportAdmin(admin.ModelAdmin,):
47d7067b 132
7bb305d9
OL
133 def _etat(self, obj):
134 return obj.get_etat_display()
df625f22
OL
135 _etat.short_description = u"État"
136 _etat.admin_order_field = "etat"
7bb305d9 137
47d7067b
OL
138 def has_delete_permission(self, request, obj=None):
139 return False
140
141 def has_add_permission(self, request):
142 return False
143
144 def has_change_permission(self, request, obj=None):
71aec2c3 145 return in_drh_or_admin(request.user)
47d7067b
OL
146
147 def get_actions(self, request):
a47ed016 148 actions = super(ImportAdmin, self).get_actions(request)
47d7067b
OL
149 del actions['delete_selected']
150 return actions
151
a47ed016
OL
152
153class ImportDossierAdmin(ImportAdmin):
498622d7 154 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
df625f22 155 list_display = ('_poste', '_implantation', '_employe', '_dae', '_etat')
498622d7 156 list_filter = ('poste__implantation', 'etat', )
a47ed016
OL
157 actions = ['importer_dans_RH', ]
158
159 def _employe(self, obj):
160 return obj.employe
161
162 def _poste(self, obj):
163 rh = obj.poste.rh_importation()
164 if rh is not None:
165 extra = u"<a href='%s' style='color: red;'> (déjà importé)</a>" % \
166 reverse('admin:rh_poste_change', args=(rh.id,))
167 else:
168 extra = u""
169
170 return u"%s%s" % (obj.poste.nom, extra)
171 _poste.allow_tags = True
172
173 def _implantation(self, obj):
174 return obj.poste.implantation
df625f22
OL
175 _implantation.short_description = u"Implantation"
176 _implantation.admin_order_field = "poste__implantation"
a47ed016
OL
177
178 def _dae(self, obj):
179 url = reverse('embauche_consulter', args=(obj.id, ))
180 return "<a href='%s'>voir la DAE</a>" % url
181 _dae.allow_tags = True
182
47d7067b
OL
183 def importer_dans_RH(self, request, data):
184 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
87057596
OL
185 if len(selected) > 1:
186 messages.add_message(request, messages.ERROR,
187 u"Un seul dossier ne peut être importé à la fois")
188 url = reverse('admin:dae_dossierfinalise_changelist')
189 return redirect(url)
190
47d7067b
OL
191 dae = [d for d in Dossier.objects.filter(id__in=selected)]
192 for d in dae:
b1e49599
OL
193 try:
194 d.importer(dry_run=True)
195 copie = d.importer()
746d94af
OL
196 messages.add_message(request, messages.SUCCESS,
197 u"%s importé dans RH" % d)
b1e49599
OL
198 url = reverse('admin:rh_dossier_change', args=(copie.id, ))
199 except Exception, e:
200 mail_admins('DAE import', unicode(e))
201 messages.add_message(request, messages.ERROR, u"%s ne peut pas\
202 être importé" % d)
203 url = reverse('admin:dae_dossierfinalise_changelist')
47d7067b
OL
204 return redirect(url)
205
a47ed016
OL
206
207class ImportPosteAdmin(ImportAdmin):
498622d7
OL
208 search_fields = ('poste__nom', )
209 list_display = ('nom', '_implantation', '_dae', '_etat',)
210 list_filter = ('implantation',)
a47ed016
OL
211 actions = ['importer_dans_RH', ]
212
213 def _implantation(self, obj):
214 return obj.implantation
df625f22
OL
215 _implantation.short_description = u"Implantation"
216 _implantation.admin_order_field = "implantation"
a47ed016
OL
217
218 def _dae(self, obj):
c14835f5 219 url = reverse('poste_consulter', args=("dae-%s" % obj.id, ))
a47ed016
OL
220 return "<a href='%s'>voir le poste dans DAE</a>" % url
221 _dae.allow_tags = True
222
223 def importer_dans_RH(self, request, data):
224 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
87057596
OL
225 if len(selected) > 1:
226 messages.add_message(request, messages.ERROR,
227 u"Un seul poste ne peut être importé à la fois")
5ce89af9 228 url = reverse('admin:dae_postefinalise_changelist')
87057596 229 return redirect(url)
a47ed016
OL
230 dae = [d for d in Poste.objects.filter(id__in=selected)]
231 for p in dae:
b1e49599
OL
232 try:
233 p.importer(dry_run=True)
234 copie = p.importer()
746d94af
OL
235 messages.add_message(request, messages.SUCCESS,
236 u"%s importé dans RH" % d)
b1e49599
OL
237 url = reverse('admin:rh_poste_change', args=(copie.id, ))
238 except Exception, e:
239 mail_admins('DAE import', unicode(e))
240 messages.add_message(request, messages.ERROR, u"%s ne peut pas\
241 être importé" % p)
242 url = reverse('admin:dae_postefinalise_changelist')
a47ed016
OL
243 return redirect(url)
244
80758fa8 245
b318efda
OL
246admin.site.register(Poste, PosteAdmin)
247admin.site.register(Dossier, DossierAdmin)
460771c1 248admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
a6245d0f 249admin.site.register(ProxyPosteStatut, PosteStatutAdmin)
47d7067b 250admin.site.register(DossierFinalise, ImportDossierAdmin)
a47ed016 251admin.site.register(PosteFinalise, ImportPosteAdmin)