import avec project.
[auf_rh_dae.git] / project / dae / admin.py
CommitLineData
17353922
OL
1# -*- encoding: utf-8 -*-
2
460771c1 3from django import forms
47d7067b 4from django.contrib import admin
75f0e87b 5
17353922 6from reversion.admin import VersionAdmin
5a1f75cb 7
75f0e87b
DB
8from auf.django.workflow.models import WorkflowCommentaire
9
de151a1e 10from project.dae.models import Poste, Dossier
5a1f75cb 11
746d94af 12
17353922 13class PosteAdmin(VersionAdmin):
b473fbb6 14 list_display = ('nom', 'implantation', 'etat', )
515124ec 15
a6245d0f 16
4be5ead0
OL
17class DossierAdmin(VersionAdmin):
18 list_display = ('_poste', '_implantation', 'employe', 'etat', )
317ce433 19 list_filter = ('etat', )
460771c1 20 exclude = ('etat', )
4be5ead0
OL
21
22 def _poste(self, obj):
23 return obj.poste.nom
24
25 def _implantation(self, obj):
26 return obj.poste.implantation
17353922 27
a6245d0f 28
460771c1
OL
29class ProxyDossierStatut(Dossier):
30 class Meta:
31 proxy = True
32 verbose_name = "Statut du dossier"
33 verbose_name_plural = "Statut des dossiers"
34
35
a6245d0f
OL
36class ProxyPosteStatut(Poste):
37 class Meta:
38 proxy = True
39 verbose_name = "Statut du poste"
40 verbose_name_plural = "Statut des postes"
41
42
43class StatutForm(forms.ModelForm):
460771c1
OL
44 commentaire = forms.CharField(label="Commentaire",
45 widget=forms.widgets.Textarea())
46
460771c1 47 def __init__(self, *args, **kwargs):
a6245d0f 48 super(StatutForm, self).__init__(*args, **kwargs)
460771c1
OL
49 # On retire le controle check des états pour faire ce qu'on veut
50 self._meta.model.__setattr__ = object.__setattr__
51
52 # on prépare le commentaire
53 self.wf = WorkflowCommentaire()
54 self.wf.etat_initial = self.instance.etat
55 self.wf.owner_id = self.user.id
746d94af 56
460771c1
OL
57 def save(self, *args, **kwargs):
58 # Le dossier se voit attribuée un commentaire WF verbeux
a6245d0f 59 obj = super(StatutForm, self).save(*args, **kwargs)
460771c1
OL
60 self.wf.content_object = obj
61 self.wf.etat_final = obj.etat
df625f22
OL
62 commentaire = u"%s => %s : %s" % (
63 self.wf.get_etat_label(self.wf.etat_initial),
64 self.wf.get_etat_label(self.wf.etat_final),
460771c1
OL
65 self.data['commentaire'])
66 self.wf.texte = commentaire
67 self.wf.save()
68 return obj
460771c1 69
460771c1 70
a6245d0f 71class StatutDossierForm(StatutForm):
460771c1 72
a6245d0f
OL
73 class Meta:
74 model = ProxyDossierStatut
75 fields = ('etat', )
460771c1 76
460771c1 77
a6245d0f 78class StatutPosteForm(StatutForm):
04a96961 79
a6245d0f
OL
80 class Meta:
81 model = ProxyPosteStatut
82 fields = ('etat', )
460771c1 83
a6245d0f
OL
84
85class StatutAdmin(VersionAdmin):
86 action = None
460771c1
OL
87
88 def has_delete_permission(self, request, obj=None):
89 return False
90
91 def has_add_permission(self, request):
92 return False
93
94 def has_change_permission(self, request, obj=None):
95 return request.user.is_superuser
96
97 def get_form(self, request, obj=None, **kwargs):
a6245d0f 98 form = super(StatutAdmin, self).get_form(request,
460771c1
OL
99 obj, **kwargs)
100 form.user = request.user
101 return form
102
a6245d0f
OL
103 def _etat(self, obj):
104 return obj.get_etat_display()
df625f22
OL
105 _etat.short_description = "État"
106 _etat.admin_order_field = "etat"
a6245d0f 107
5a1f75cb 108
a6245d0f
OL
109class DossierStatutAdmin(StatutAdmin):
110 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
111 list_display = ('_poste', '_implantation', 'employe', '_etat', )
df625f22 112 list_filter = ('poste__implantation', )
a6245d0f
OL
113 form = StatutDossierForm
114
115 def _poste(self, obj):
116 return obj.poste.nom
117
118 def _implantation(self, obj):
119 return obj.poste.implantation
df625f22
OL
120 _implantation.short_description = u"Implantation"
121 _implantation.admin_order_field = "poste__implantation"
a6245d0f 122
5a1f75cb 123
a6245d0f
OL
124class PosteStatutAdmin(StatutAdmin):
125 search_fields = ('nom', )
df625f22
OL
126 list_display = ('nom', 'implantation', )
127 list_filter = ('implantation', 'etat', )
a6245d0f
OL
128 form = StatutPosteForm
129
130
b318efda
OL
131admin.site.register(Poste, PosteAdmin)
132admin.site.register(Dossier, DossierAdmin)
460771c1 133admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
a6245d0f 134admin.site.register(ProxyPosteStatut, PosteStatutAdmin)