filtre
[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 StatutDossierForm(forms.ModelForm):
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)
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
55
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
61 commentaire = u"%s => %s : %s" % (self.wf.etat_initial,
62 self.wf.etat_final,
63 self.data['commentaire'])
64 self.wf.texte = commentaire
65 self.wf.save()
66 return obj
67
68
69 class 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
99
100 class ImportAdmin(admin.ModelAdmin,):
101
102 def _etat(self, obj):
103 return obj.get_etat_display()
104
105
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):
119 actions = super(ImportAdmin, self).get_actions(request)
120 del actions['delete_selected']
121 return actions
122
123
124 class ImportDossierAdmin(ImportAdmin):
125 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
126 list_display = ('_poste', '_employe', '_dae',)
127 list_filter = ('poste__implantation', 'etat', )
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
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:
156 try:
157 d.importer(dry_run=True)
158 copie = d.importer()
159 messages.add_message(request, messages.SUCCESS,
160 u"%s importé dans RH" % d)
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')
167 return redirect(url)
168
169
170 class ImportPosteAdmin(ImportAdmin):
171 search_fields = ('poste__nom', )
172 list_display = ('nom', '_implantation', '_dae', '_etat',)
173 list_filter = ('implantation',)
174 actions = ['importer_dans_RH', ]
175
176 def _implantation(self, obj):
177 return obj.implantation
178
179 def _dae(self, obj):
180 url = reverse('poste_consulter', args=("dae-%s" % obj.id, ))
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:
188 try:
189 p.importer(dry_run=True)
190 copie = p.importer()
191 messages.add_message(request, messages.SUCCESS,
192 u"%s importé dans RH" % d)
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')
199 return redirect(url)
200
201 admin.site.register(Poste, PosteAdmin)
202 admin.site.register(Dossier, DossierAdmin)
203 admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
204 admin.site.register(DossierFinalise, ImportDossierAdmin)
205 admin.site.register(PosteFinalise, ImportPosteAdmin)