filtre
[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
746d94af 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
746d94af 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
38class StatutDossierForm(forms.ModelForm):
460771c1
OL
39 commentaire = forms.CharField(label="Commentaire",
40 widget=forms.widgets.Textarea())
41
42 class Meta:
43 model = ProxyDossierStatut
44 fields = ('etat', )
45
46 def __init__(self, *args, **kwargs):
47 super(StatutDossierForm, self).__init__(*args, **kwargs)
460771c1
OL
48 # On retire le controle check des états pour faire ce qu'on veut
49 self._meta.model.__setattr__ = object.__setattr__
50
51 # on prépare le commentaire
52 self.wf = WorkflowCommentaire()
53 self.wf.etat_initial = self.instance.etat
54 self.wf.owner_id = self.user.id
746d94af 55
460771c1
OL
56 def save(self, *args, **kwargs):
57 # Le dossier se voit attribuée un commentaire WF verbeux
58 obj = super(StatutDossierForm, self).save(*args, **kwargs)
59 self.wf.content_object = obj
60 self.wf.etat_final = obj.etat
746d94af
OL
61 commentaire = u"%s => %s : %s" % (self.wf.etat_initial,
62 self.wf.etat_final,
460771c1
OL
63 self.data['commentaire'])
64 self.wf.texte = commentaire
65 self.wf.save()
66 return obj
746d94af 67
460771c1
OL
68
69class DossierStatutAdmin(VersionAdmin):
70 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
71 list_display = ('_poste', '_implantation', 'employe', '_etat', )
72 form = StatutDossierForm
73 action = None
74
75 def _poste(self, obj):
76 return obj.poste.nom
77
78 def _implantation(self, obj):
79 return obj.poste.implantation
80
81 def _etat(self, obj):
82 return obj.get_etat_display()
83
84 def has_delete_permission(self, request, obj=None):
85 return False
86
87 def has_add_permission(self, request):
88 return False
89
90 def has_change_permission(self, request, obj=None):
91 return request.user.is_superuser
92
93 def get_form(self, request, obj=None, **kwargs):
94 form = super(DossierStatutAdmin, self).get_form(request,
95 obj, **kwargs)
96 form.user = request.user
97 return form
98
746d94af 99
a47ed016 100class ImportAdmin(admin.ModelAdmin,):
47d7067b 101
7bb305d9
OL
102 def _etat(self, obj):
103 return obj.get_etat_display()
104
105
47d7067b
OL
106 def has_delete_permission(self, request, obj=None):
107 return False
108
109 def has_add_permission(self, request):
110 return False
111
112 def has_change_permission(self, request, obj=None):
113 if obj is None:
114 return True
115 else:
116 return False
117
118 def get_actions(self, request):
a47ed016 119 actions = super(ImportAdmin, self).get_actions(request)
47d7067b
OL
120 del actions['delete_selected']
121 return actions
122
a47ed016
OL
123
124class ImportDossierAdmin(ImportAdmin):
498622d7
OL
125 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
126 list_display = ('_poste', '_employe', '_dae',)
127 list_filter = ('poste__implantation', 'etat', )
a47ed016
OL
128 actions = ['importer_dans_RH', ]
129
130 def _employe(self, obj):
131 return obj.employe
132
133 def _poste(self, obj):
134 rh = obj.poste.rh_importation()
135 if rh is not None:
136 extra = u"<a href='%s' style='color: red;'> (déjà importé)</a>" % \
137 reverse('admin:rh_poste_change', args=(rh.id,))
138 else:
139 extra = u""
140
141 return u"%s%s" % (obj.poste.nom, extra)
142 _poste.allow_tags = True
143
144 def _implantation(self, obj):
145 return obj.poste.implantation
146
147 def _dae(self, obj):
148 url = reverse('embauche_consulter', args=(obj.id, ))
149 return "<a href='%s'>voir la DAE</a>" % url
150 _dae.allow_tags = True
151
47d7067b
OL
152 def importer_dans_RH(self, request, data):
153 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
154 dae = [d for d in Dossier.objects.filter(id__in=selected)]
155 for d in dae:
b1e49599
OL
156 try:
157 d.importer(dry_run=True)
158 copie = d.importer()
746d94af
OL
159 messages.add_message(request, messages.SUCCESS,
160 u"%s importé dans RH" % d)
b1e49599
OL
161 url = reverse('admin:rh_dossier_change', args=(copie.id, ))
162 except Exception, e:
163 mail_admins('DAE import', unicode(e))
164 messages.add_message(request, messages.ERROR, u"%s ne peut pas\
165 être importé" % d)
166 url = reverse('admin:dae_dossierfinalise_changelist')
47d7067b
OL
167 return redirect(url)
168
a47ed016
OL
169
170class ImportPosteAdmin(ImportAdmin):
498622d7
OL
171 search_fields = ('poste__nom', )
172 list_display = ('nom', '_implantation', '_dae', '_etat',)
173 list_filter = ('implantation',)
a47ed016
OL
174 actions = ['importer_dans_RH', ]
175
176 def _implantation(self, obj):
177 return obj.implantation
178
179 def _dae(self, obj):
7bb305d9 180 url = reverse('poste_consulter', args=("dae-%s" % obj.id, ))
a47ed016
OL
181 return "<a href='%s'>voir le poste dans DAE</a>" % url
182 _dae.allow_tags = True
183
184 def importer_dans_RH(self, request, data):
185 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
186 dae = [d for d in Poste.objects.filter(id__in=selected)]
187 for p in dae:
b1e49599
OL
188 try:
189 p.importer(dry_run=True)
190 copie = p.importer()
746d94af
OL
191 messages.add_message(request, messages.SUCCESS,
192 u"%s importé dans RH" % d)
b1e49599
OL
193 url = reverse('admin:rh_poste_change', args=(copie.id, ))
194 except Exception, e:
195 mail_admins('DAE import', unicode(e))
196 messages.add_message(request, messages.ERROR, u"%s ne peut pas\
197 être importé" % p)
198 url = reverse('admin:dae_postefinalise_changelist')
a47ed016
OL
199 return redirect(url)
200
b318efda
OL
201admin.site.register(Poste, PosteAdmin)
202admin.site.register(Dossier, DossierAdmin)
460771c1 203admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
47d7067b 204admin.site.register(DossierFinalise, ImportDossierAdmin)
a47ed016 205admin.site.register(PosteFinalise, ImportPosteAdmin)