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