statut postE
[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
13 class DossierAdmin(VersionAdmin):
14 list_display = ('_poste', '_implantation', 'employe', 'etat', )
15 list_filter = ('etat', )
16 exclude = ('etat', )
17
18 def _poste(self, obj):
19 return obj.poste.nom
20
21 def _implantation(self, obj):
22 return obj.poste.implantation
23
24
25 class ProxyDossierStatut(Dossier):
26 class Meta:
27 proxy = True
28 verbose_name = "Statut du dossier"
29 verbose_name_plural = "Statut des dossiers"
30
31
32 class ProxyPosteStatut(Poste):
33 class Meta:
34 proxy = True
35 verbose_name = "Statut du poste"
36 verbose_name_plural = "Statut des postes"
37
38
39 class StatutForm(forms.ModelForm):
40
41 commentaire = forms.CharField(label="Commentaire",
42 widget=forms.widgets.Textarea())
43
44 def __init__(self, *args, **kwargs):
45 super(StatutForm, 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(StatutForm, 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 class StatutDossierForm(StatutForm):
67
68 class Meta:
69 model = ProxyDossierStatut
70 fields = ('etat', )
71
72
73 class StatutPosteForm(StatutForm):
74
75 class Meta:
76 model = ProxyPosteStatut
77 fields = ('etat', )
78
79
80 class StatutAdmin(VersionAdmin):
81 action = None
82
83 def has_delete_permission(self, request, obj=None):
84 return False
85
86 def has_add_permission(self, request):
87 return False
88
89 def has_change_permission(self, request, obj=None):
90 return request.user.is_superuser
91
92 def get_form(self, request, obj=None, **kwargs):
93 form = super(StatutAdmin, self).get_form(request,
94 obj, **kwargs)
95 form.user = request.user
96 return form
97
98 def _etat(self, obj):
99 return obj.get_etat_display()
100
101
102 class DossierStatutAdmin(StatutAdmin):
103 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
104 list_display = ('_poste', '_implantation', 'employe', '_etat', )
105 form = StatutDossierForm
106
107 def _poste(self, obj):
108 return obj.poste.nom
109
110 def _implantation(self, obj):
111 return obj.poste.implantation
112
113
114
115 class PosteStatutAdmin(StatutAdmin):
116 search_fields = ('nom', )
117 list_display = ('nom', 'implantation', '_etat', )
118 form = StatutPosteForm
119
120
121 admin.site.register(Poste, PosteAdmin)
122 admin.site.register(Dossier, DossierAdmin)
123 admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
124 admin.site.register(ProxyPosteStatut, PosteStatutAdmin)