ajout des dates auto dans le dossier
[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
17353922 13
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
746d94af
OL
64 commentaire = u"%s => %s : %s" % (self.wf.etat_initial,
65 self.wf.etat_final,
460771c1
OL
66 self.data['commentaire'])
67 self.wf.texte = commentaire
68 self.wf.save()
69 return obj
460771c1 70
460771c1 71
a6245d0f 72class StatutDossierForm(StatutForm):
460771c1 73
a6245d0f
OL
74 class Meta:
75 model = ProxyDossierStatut
76 fields = ('etat', )
460771c1 77
460771c1 78
a6245d0f 79class StatutPosteForm(StatutForm):
04a96961 80
a6245d0f
OL
81 class Meta:
82 model = ProxyPosteStatut
83 fields = ('etat', )
460771c1 84
a6245d0f
OL
85
86class StatutAdmin(VersionAdmin):
87 action = None
460771c1
OL
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):
a6245d0f 99 form = super(StatutAdmin, self).get_form(request,
460771c1
OL
100 obj, **kwargs)
101 form.user = request.user
102 return form
103
a6245d0f
OL
104 def _etat(self, obj):
105 return obj.get_etat_display()
106
107
108class 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
a6245d0f
OL
120class PosteStatutAdmin(StatutAdmin):
121 search_fields = ('nom', )
122 list_display = ('nom', 'implantation', '_etat', )
123 form = StatutPosteForm
124
125
a47ed016 126class ImportAdmin(admin.ModelAdmin,):
47d7067b 127
7bb305d9
OL
128 def _etat(self, obj):
129 return obj.get_etat_display()
130
47d7067b
OL
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):
a47ed016 144 actions = super(ImportAdmin, self).get_actions(request)
47d7067b
OL
145 del actions['delete_selected']
146 return actions
147
a47ed016
OL
148
149class ImportDossierAdmin(ImportAdmin):
498622d7 150 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
04a96961 151 list_display = ('_poste', '_implantation', '_employe', '_dae',)
498622d7 152 list_filter = ('poste__implantation', 'etat', )
a47ed016
OL
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
47d7067b
OL
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:
b1e49599
OL
181 try:
182 d.importer(dry_run=True)
183 copie = d.importer()
746d94af
OL
184 messages.add_message(request, messages.SUCCESS,
185 u"%s importé dans RH" % d)
b1e49599
OL
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')
47d7067b
OL
192 return redirect(url)
193
a47ed016
OL
194
195class ImportPosteAdmin(ImportAdmin):
498622d7
OL
196 search_fields = ('poste__nom', )
197 list_display = ('nom', '_implantation', '_dae', '_etat',)
198 list_filter = ('implantation',)
a47ed016
OL
199 actions = ['importer_dans_RH', ]
200
201 def _implantation(self, obj):
202 return obj.implantation
203
204 def _dae(self, obj):
c14835f5 205 url = reverse('poste_consulter', args=("dae-%s" % obj.id, ))
a47ed016
OL
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:
b1e49599
OL
213 try:
214 p.importer(dry_run=True)
215 copie = p.importer()
746d94af
OL
216 messages.add_message(request, messages.SUCCESS,
217 u"%s importé dans RH" % d)
b1e49599
OL
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')
a47ed016
OL
224 return redirect(url)
225
80758fa8 226
b318efda
OL
227admin.site.register(Poste, PosteAdmin)
228admin.site.register(Dossier, DossierAdmin)
460771c1 229admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
a6245d0f 230admin.site.register(ProxyPosteStatut, PosteStatutAdmin)
47d7067b 231admin.site.register(DossierFinalise, ImportDossierAdmin)
a47ed016 232admin.site.register(PosteFinalise, ImportPosteAdmin)