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