[#2658] Intégré reversion dans DAE, mais pas dans l'admin
[auf_rh_dae.git] / project / dae / admin.py
1 # -*- encoding: utf-8 -*-
2
3 from django import forms
4 from django.contrib import admin
5
6 from auf.django.workflow.models import WorkflowCommentaire
7
8 from project.dae.models import Poste, Dossier
9
10
11 class BaseAdmin(admin.ModelAdmin):
12
13 class Media:
14 css = {'screen': (
15 'css/admin_custom.css',
16 'jquery-autocomplete/jquery.autocomplete.css',
17 )}
18 js = (
19 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js',
20 'jquery-autocomplete/jquery.autocomplete.min.js',
21 )
22
23
24 class PosteAdmin(BaseAdmin):
25 list_display = ('nom', 'implantation', 'etat', )
26
27
28 class DossierAdmin(BaseAdmin):
29 list_display = ('_poste', '_implantation', 'employe', 'etat', )
30 list_filter = ('etat', )
31 exclude = ('etat', )
32
33 def _poste(self, obj):
34 return obj.poste.nom
35
36 def _implantation(self, obj):
37 return obj.poste.implantation
38
39
40 class ProxyDossierStatut(Dossier):
41 class Meta:
42 proxy = True
43 verbose_name = "Statut du dossier"
44 verbose_name_plural = "Statut des dossiers"
45
46
47 class ProxyPosteStatut(Poste):
48 class Meta:
49 proxy = True
50 verbose_name = "Statut du poste"
51 verbose_name_plural = "Statut des postes"
52
53
54 class StatutForm(forms.ModelForm):
55 commentaire = forms.CharField(label="Commentaire",
56 widget=forms.widgets.Textarea())
57
58 def __init__(self, *args, **kwargs):
59 super(StatutForm, self).__init__(*args, **kwargs)
60 # On retire le controle check des états pour faire ce qu'on veut
61 self._meta.model.__setattr__ = object.__setattr__
62
63 # on prépare le commentaire
64 self.wf = WorkflowCommentaire()
65 self.wf.etat_initial = self.instance.etat
66 self.wf.owner_id = self.user.id
67
68 def save(self, *args, **kwargs):
69 # Le dossier se voit attribuée un commentaire WF verbeux
70 obj = super(StatutForm, self).save(*args, **kwargs)
71 self.wf.content_object = obj
72 self.wf.etat_final = obj.etat
73 commentaire = u"%s => %s : %s" % (
74 self.wf.get_etat_label(self.wf.etat_initial),
75 self.wf.get_etat_label(self.wf.etat_final),
76 self.data['commentaire'])
77 self.wf.texte = commentaire
78 self.wf.save()
79 return obj
80
81
82 class StatutDossierForm(StatutForm):
83
84 class Meta:
85 model = ProxyDossierStatut
86 fields = ('etat', )
87
88
89 class StatutPosteForm(StatutForm):
90
91 class Meta:
92 model = ProxyPosteStatut
93 fields = ('etat', )
94
95
96 class StatutAdmin(BaseAdmin):
97 action = None
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 return request.user.is_superuser
107
108 def get_form(self, request, obj=None, **kwargs):
109 form = super(StatutAdmin, self).get_form(request,
110 obj, **kwargs)
111 form.user = request.user
112 return form
113
114 def _etat(self, obj):
115 return obj.get_etat_display()
116 _etat.short_description = "État"
117 _etat.admin_order_field = "etat"
118
119
120 class DossierStatutAdmin(StatutAdmin):
121 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
122 list_display = ('_poste', '_implantation', 'employe', '_etat', )
123 list_filter = ('poste__implantation', )
124 form = StatutDossierForm
125
126 def _poste(self, obj):
127 return obj.poste.nom
128
129 def _implantation(self, obj):
130 return obj.poste.implantation
131 _implantation.short_description = u"Implantation"
132 _implantation.admin_order_field = "poste__implantation"
133
134
135 class PosteStatutAdmin(StatutAdmin):
136 search_fields = ('nom', )
137 list_display = ('nom', 'implantation', )
138 list_filter = ('implantation', 'etat', )
139 form = StatutPosteForm
140
141
142 admin.site.register(Poste, PosteAdmin)
143 admin.site.register(Dossier, DossierAdmin)
144 admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
145 admin.site.register(ProxyPosteStatut, PosteStatutAdmin)