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