merge import DAE
[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
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" % (self.wf.etat_initial,
65 self.wf.etat_final,
66 self.data['commentaire'])
67 self.wf.texte = commentaire
68 self.wf.save()
69 return obj
70
71
72 class StatutDossierForm(StatutForm):
73
74 class Meta:
75 model = ProxyDossierStatut
76 fields = ('etat', )
77
78
79 class StatutPosteForm(StatutForm):
80
81 class Meta:
82 model = ProxyPosteStatut
83 fields = ('etat', )
84
85
86 class StatutAdmin(VersionAdmin):
87 action = None
88
89 def has_delete_permission(self, request, obj=None):
90 return False
91
92 def has_add_permission(self, request):
93 return False
94
95 def has_change_permission(self, request, obj=None):
96 return request.user.is_superuser
97
98 def get_form(self, request, obj=None, **kwargs):
99 form = super(StatutAdmin, self).get_form(request,
100 obj, **kwargs)
101 form.user = request.user
102 return form
103
104 def _etat(self, obj):
105 return obj.get_etat_display()
106
107
108 class DossierStatutAdmin(StatutAdmin):
109 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
110 list_display = ('_poste', '_implantation', 'employe', '_etat', )
111 form = StatutDossierForm
112
113 def _poste(self, obj):
114 return obj.poste.nom
115
116 def _implantation(self, obj):
117 return obj.poste.implantation
118
119
120 class PosteStatutAdmin(StatutAdmin):
121 search_fields = ('nom', )
122 list_display = ('nom', 'implantation', '_etat', )
123 form = StatutPosteForm
124
125
126 class ImportAdmin(admin.ModelAdmin,):
127
128 def _etat(self, obj):
129 return obj.get_etat_display()
130
131 def has_delete_permission(self, request, obj=None):
132 return False
133
134 def has_add_permission(self, request):
135 return False
136
137 def has_change_permission(self, request, obj=None):
138 if obj is None:
139 return True
140 else:
141 return False
142
143 def get_actions(self, request):
144 actions = super(ImportAdmin, self).get_actions(request)
145 del actions['delete_selected']
146 return actions
147
148
149 class ImportDossierAdmin(ImportAdmin):
150 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
151 list_display = ('_poste', '_implantation', '_employe', '_dae',)
152 list_filter = ('poste__implantation', 'etat', )
153 actions = ['importer_dans_RH', ]
154
155 def _employe(self, obj):
156 return obj.employe
157
158 def _poste(self, obj):
159 rh = obj.poste.rh_importation()
160 if rh is not None:
161 extra = u"<a href='%s' style='color: red;'> (déjà importé)</a>" % \
162 reverse('admin:rh_poste_change', args=(rh.id,))
163 else:
164 extra = u""
165
166 return u"%s%s" % (obj.poste.nom, extra)
167 _poste.allow_tags = True
168
169 def _implantation(self, obj):
170 return obj.poste.implantation
171
172 def _dae(self, obj):
173 url = reverse('embauche_consulter', args=(obj.id, ))
174 return "<a href='%s'>voir la DAE</a>" % url
175 _dae.allow_tags = True
176
177 def importer_dans_RH(self, request, data):
178 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
179 dae = [d for d in Dossier.objects.filter(id__in=selected)]
180 for d in dae:
181 try:
182 d.importer(dry_run=True)
183 copie = d.importer()
184 messages.add_message(request, messages.SUCCESS,
185 u"%s importé dans RH" % d)
186 url = reverse('admin:rh_dossier_change', args=(copie.id, ))
187 except Exception, e:
188 mail_admins('DAE import', unicode(e))
189 messages.add_message(request, messages.ERROR, u"%s ne peut pas\
190 être importé" % d)
191 url = reverse('admin:dae_dossierfinalise_changelist')
192 return redirect(url)
193
194
195 class ImportPosteAdmin(ImportAdmin):
196 search_fields = ('poste__nom', )
197 list_display = ('nom', '_implantation', '_dae', '_etat',)
198 list_filter = ('implantation',)
199 actions = ['importer_dans_RH', ]
200
201 def _implantation(self, obj):
202 return obj.implantation
203
204 def _dae(self, obj):
205 url = reverse('poste_consulter', args=("dae-%s" % obj.id, ))
206 return "<a href='%s'>voir le poste dans DAE</a>" % url
207 _dae.allow_tags = True
208
209 def importer_dans_RH(self, request, data):
210 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
211 dae = [d for d in Poste.objects.filter(id__in=selected)]
212 for p in dae:
213 try:
214 p.importer(dry_run=True)
215 copie = p.importer()
216 messages.add_message(request, messages.SUCCESS,
217 u"%s importé dans RH" % d)
218 url = reverse('admin:rh_poste_change', args=(copie.id, ))
219 except Exception, e:
220 mail_admins('DAE import', unicode(e))
221 messages.add_message(request, messages.ERROR, u"%s ne peut pas\
222 être importé" % p)
223 url = reverse('admin:dae_postefinalise_changelist')
224 return redirect(url)
225
226
227 admin.site.register(Poste, PosteAdmin)
228 admin.site.register(Dossier, DossierAdmin)
229 admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
230 admin.site.register(ProxyPosteStatut, PosteStatutAdmin)
231 admin.site.register(DossierFinalise, ImportDossierAdmin)
232 admin.site.register(PosteFinalise, ImportPosteAdmin)