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