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