945bac3f985d0db7d74fb188d7f62f7d35033998
[auf_rh_dae.git] / project / dae / admin.py
1 # -*- encoding: utf-8 -*-
2
3 from django.contrib import admin
4 from django import forms
5 from reversion.admin import VersionAdmin
6 from auf.django.workflow.models import WorkflowCommentaire
7 from models import Poste, Dossier
8
9 class PosteAdmin(VersionAdmin):
10 list_display = ('nom', 'implantation', 'etat', )
11
12 class DossierAdmin(VersionAdmin):
13 list_display = ('_poste', '_implantation', 'employe', 'etat', )
14 list_filter = ('etat', )
15 exclude = ('etat', )
16
17 def _poste(self, obj):
18 return obj.poste.nom
19
20 def _implantation(self, obj):
21 return obj.poste.implantation
22
23 class ProxyDossierStatut(Dossier):
24 class Meta:
25 proxy = True
26 verbose_name = "Statut du dossier"
27 verbose_name_plural = "Statut des dossiers"
28
29
30 class StatutDossierForm(forms.ModelForm):
31
32 commentaire = forms.CharField(label="Commentaire",
33 widget=forms.widgets.Textarea())
34
35 class Meta:
36 model = ProxyDossierStatut
37 fields = ('etat', )
38
39 def __init__(self, *args, **kwargs):
40 super(StatutDossierForm, self).__init__(*args, **kwargs)
41
42 # On retire le controle check des états pour faire ce qu'on veut
43 self._meta.model.__setattr__ = object.__setattr__
44
45 # on prépare le commentaire
46 self.wf = WorkflowCommentaire()
47 self.wf.etat_initial = self.instance.etat
48 self.wf.owner_id = self.user.id
49
50 def save(self, *args, **kwargs):
51 # Le dossier se voit attribuée un commentaire WF verbeux
52 obj = super(StatutDossierForm, self).save(*args, **kwargs)
53 self.wf.content_object = obj
54 self.wf.etat_final = obj.etat
55 commentaire = u"%s => %s : %s" % (self.wf.etat_initial, self.wf.etat_final,
56 self.data['commentaire'])
57 self.wf.texte = commentaire
58 self.wf.save()
59 return obj
60
61
62 class DossierStatutAdmin(VersionAdmin):
63 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
64 list_display = ('_poste', '_implantation', 'employe', '_etat', )
65 form = StatutDossierForm
66 action = None
67
68 def _poste(self, obj):
69 return obj.poste.nom
70
71 def _implantation(self, obj):
72 return obj.poste.implantation
73
74 def _etat(self, obj):
75 return obj.get_etat_display()
76
77 def has_delete_permission(self, request, obj=None):
78 return False
79
80 def has_add_permission(self, request):
81 return False
82
83 def has_change_permission(self, request, obj=None):
84 return request.user.is_superuser
85
86 def get_form(self, request, obj=None, **kwargs):
87 form = super(DossierStatutAdmin, self).get_form(request,
88 obj, **kwargs)
89 form.user = request.user
90 return form
91
92 admin.site.register(Poste, PosteAdmin)
93 admin.site.register(Dossier, DossierAdmin)
94 admin.site.register(ProxyDossierStatut, DossierStatutAdmin)