fix PJ
[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 class DossierAdmin(VersionAdmin):
18 list_display = ('_poste', '_implantation', 'employe', 'etat', )
19 list_filter = ('etat', )
20 exclude = ('etat', )
21
22 def _poste(self, obj):
23 return obj.poste.nom
24
25 def _implantation(self, obj):
26 return obj.poste.implantation
27
28 class ProxyDossierStatut(Dossier):
29 class Meta:
30 proxy = True
31 verbose_name = "Statut du dossier"
32 verbose_name_plural = "Statut des dossiers"
33
34
35 class 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
67 class 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
97 class ImportAdmin(admin.ModelAdmin,):
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):
112 actions = super(ImportAdmin, self).get_actions(request)
113 del actions['delete_selected']
114 return actions
115
116
117 class 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
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:
147 try:
148 d.importer(dry_run=True)
149 copie = d.importer()
150 messages.add_message(request, messages.SUCCESS, u"%s importé dans\
151 RH" % d)
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')
158 return redirect(url)
159
160
161 class 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:
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')
188 return redirect(url)
189
190 admin.site.register(Poste, PosteAdmin)
191 admin.site.register(Dossier, DossierAdmin)
192 admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
193 admin.site.register(DossierFinalise, ImportDossierAdmin)
194 admin.site.register(PosteFinalise, ImportPosteAdmin)