1 a la fois import
[auf_rh_dae.git] / project / dae / admin.py
1 # -*- encoding: utf-8 -*-
2
3 from django import forms
4
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 from auf.django.workflow.models import WorkflowCommentaire
12 from models import Poste, Dossier, DossierFinalise, PosteFinalise
13 from rh.decorators import in_drh_or_admin
14
15 class PosteAdmin(VersionAdmin):
16 list_display = ('nom', 'implantation', 'etat', )
17
18
19 class DossierAdmin(VersionAdmin):
20 list_display = ('_poste', '_implantation', 'employe', 'etat', )
21 list_filter = ('etat', )
22 exclude = ('etat', )
23
24 def _poste(self, obj):
25 return obj.poste.nom
26
27 def _implantation(self, obj):
28 return obj.poste.implantation
29
30
31 class ProxyDossierStatut(Dossier):
32 class Meta:
33 proxy = True
34 verbose_name = "Statut du dossier"
35 verbose_name_plural = "Statut des dossiers"
36
37
38 class ProxyPosteStatut(Poste):
39 class Meta:
40 proxy = True
41 verbose_name = "Statut du poste"
42 verbose_name_plural = "Statut des postes"
43
44
45 class StatutForm(forms.ModelForm):
46 commentaire = forms.CharField(label="Commentaire",
47 widget=forms.widgets.Textarea())
48
49 def __init__(self, *args, **kwargs):
50 super(StatutForm, self).__init__(*args, **kwargs)
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
58
59 def save(self, *args, **kwargs):
60 # Le dossier se voit attribuée un commentaire WF verbeux
61 obj = super(StatutForm, self).save(*args, **kwargs)
62 self.wf.content_object = obj
63 self.wf.etat_final = obj.etat
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),
67 self.data['commentaire'])
68 self.wf.texte = commentaire
69 self.wf.save()
70 return obj
71
72
73 class StatutDossierForm(StatutForm):
74
75 class Meta:
76 model = ProxyDossierStatut
77 fields = ('etat', )
78
79
80 class StatutPosteForm(StatutForm):
81
82 class Meta:
83 model = ProxyPosteStatut
84 fields = ('etat', )
85
86
87 class StatutAdmin(VersionAdmin):
88 action = None
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):
100 form = super(StatutAdmin, self).get_form(request,
101 obj, **kwargs)
102 form.user = request.user
103 return form
104
105 def _etat(self, obj):
106 return obj.get_etat_display()
107 _etat.short_description = "État"
108 _etat.admin_order_field = "etat"
109
110 class DossierStatutAdmin(StatutAdmin):
111 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
112 list_display = ('_poste', '_implantation', 'employe', '_etat', )
113 list_filter = ('poste__implantation', )
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
121 _implantation.short_description = u"Implantation"
122 _implantation.admin_order_field = "poste__implantation"
123
124 class PosteStatutAdmin(StatutAdmin):
125 search_fields = ('nom', )
126 list_display = ('nom', 'implantation', )
127 list_filter = ('implantation', 'etat', )
128 form = StatutPosteForm
129
130
131 class ImportAdmin(admin.ModelAdmin,):
132
133 def _etat(self, obj):
134 return obj.get_etat_display()
135 _etat.short_description = u"État"
136 _etat.admin_order_field = "etat"
137
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):
145 return in_drh_or_admin(request.user)
146
147 def get_actions(self, request):
148 actions = super(ImportAdmin, self).get_actions(request)
149 del actions['delete_selected']
150 return actions
151
152
153 class ImportDossierAdmin(ImportAdmin):
154 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
155 list_display = ('_poste', '_implantation', '_employe', '_dae', '_etat')
156 list_filter = ('poste__implantation', 'etat', )
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
175 _implantation.short_description = u"Implantation"
176 _implantation.admin_order_field = "poste__implantation"
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
183 def importer_dans_RH(self, request, data):
184 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
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
191 dae = [d for d in Dossier.objects.filter(id__in=selected)]
192 for d in dae:
193 try:
194 d.importer(dry_run=True)
195 copie = d.importer()
196 messages.add_message(request, messages.SUCCESS,
197 u"%s importé dans RH" % d)
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')
204 return redirect(url)
205
206
207 class ImportPosteAdmin(ImportAdmin):
208 search_fields = ('poste__nom', )
209 list_display = ('nom', '_implantation', '_dae', '_etat',)
210 list_filter = ('implantation',)
211 actions = ['importer_dans_RH', ]
212
213 def _implantation(self, obj):
214 return obj.implantation
215 _implantation.short_description = u"Implantation"
216 _implantation.admin_order_field = "implantation"
217
218 def _dae(self, obj):
219 url = reverse('poste_consulter', args=("dae-%s" % obj.id, ))
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)
225 if len(selected) > 1:
226 messages.add_message(request, messages.ERROR,
227 u"Un seul poste ne peut être importé à la fois")
228 url = reverse('admin:dae_dossierfinalise_changelist')
229 return redirect(url)
230 dae = [d for d in Poste.objects.filter(id__in=selected)]
231 for p in dae:
232 try:
233 p.importer(dry_run=True)
234 copie = p.importer()
235 messages.add_message(request, messages.SUCCESS,
236 u"%s importé dans RH" % d)
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')
243 return redirect(url)
244
245
246 admin.site.register(Poste, PosteAdmin)
247 admin.site.register(Dossier, DossierAdmin)
248 admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
249 admin.site.register(ProxyPosteStatut, PosteStatutAdmin)
250 admin.site.register(DossierFinalise, ImportDossierAdmin)
251 admin.site.register(PosteFinalise, ImportPosteAdmin)