PEP8
[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 has_delete_permission(self, request, obj=None):
103 return False
104
105 def has_add_permission(self, request):
106 return False
107
108 def has_change_permission(self, request, obj=None):
109 if obj is None:
110 return True
111 else:
112 return False
113
114 def get_actions(self, request):
115 actions = super(ImportAdmin, self).get_actions(request)
116 del actions['delete_selected']
117 return actions
118
119
120 class ImportDossierAdmin(ImportAdmin):
121 list_display = ('_poste', '_employe', '_dae', )
122 actions = ['importer_dans_RH', ]
123
124 def _employe(self, obj):
125 return obj.employe
126
127 def _poste(self, obj):
128 rh = obj.poste.rh_importation()
129 if rh is not None:
130 extra = u"<a href='%s' style='color: red;'> (déjà importé)</a>" % \
131 reverse('admin:rh_poste_change', args=(rh.id,))
132 else:
133 extra = u""
134
135 return u"%s%s" % (obj.poste.nom, extra)
136 _poste.allow_tags = True
137
138 def _implantation(self, obj):
139 return obj.poste.implantation
140
141 def _dae(self, obj):
142 url = reverse('embauche_consulter', args=(obj.id, ))
143 return "<a href='%s'>voir la DAE</a>" % url
144 _dae.allow_tags = True
145
146 def importer_dans_RH(self, request, data):
147 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
148 dae = [d for d in Dossier.objects.filter(id__in=selected)]
149 for d in dae:
150 try:
151 d.importer(dry_run=True)
152 copie = d.importer()
153 messages.add_message(request, messages.SUCCESS,
154 u"%s importé dans RH" % d)
155 url = reverse('admin:rh_dossier_change', args=(copie.id, ))
156 except Exception, e:
157 mail_admins('DAE import', unicode(e))
158 messages.add_message(request, messages.ERROR, u"%s ne peut pas\
159 être importé" % d)
160 url = reverse('admin:dae_dossierfinalise_changelist')
161 return redirect(url)
162
163
164 class ImportPosteAdmin(ImportAdmin):
165 list_display = ('nom', '_implantation', '_dae', )
166 actions = ['importer_dans_RH', ]
167
168 def _implantation(self, obj):
169 return obj.implantation
170
171 def _dae(self, obj):
172 url = reverse('poste_consulter', args=(obj.id, ))
173 return "<a href='%s'>voir le poste dans DAE</a>" % url
174 _dae.allow_tags = True
175
176 def importer_dans_RH(self, request, data):
177 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
178 dae = [d for d in Poste.objects.filter(id__in=selected)]
179 for p in dae:
180 try:
181 p.importer(dry_run=True)
182 copie = p.importer()
183 messages.add_message(request, messages.SUCCESS,
184 u"%s importé dans RH" % d)
185 url = reverse('admin:rh_poste_change', args=(copie.id, ))
186 except Exception, e:
187 mail_admins('DAE import', unicode(e))
188 messages.add_message(request, messages.ERROR, u"%s ne peut pas\
189 être importé" % p)
190 url = reverse('admin:dae_postefinalise_changelist')
191 return redirect(url)
192
193 admin.site.register(Poste, PosteAdmin)
194 admin.site.register(Dossier, DossierAdmin)
195 admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
196 admin.site.register(DossierFinalise, ImportDossierAdmin)
197 admin.site.register(PosteFinalise, ImportPosteAdmin)