DAE reactivation modfication de statut dans l admin pour superuser
[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, ProxyDossierStatut, \
11 ProxyPosteStatut
12
13
14 class BaseAdmin(admin.ModelAdmin):
15
16 class Media:
17 css = {'screen': (
18 'css/admin_custom.css',
19 'jquery-autocomplete/jquery.autocomplete.css',
20 )}
21 js = (
22 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js',
23 'jquery-autocomplete/jquery.autocomplete.min.js',
24 )
25
26
27 class PosteAdmin(BaseAdmin, VersionAdmin):
28 list_display = ('nom', 'implantation', 'etat', )
29
30
31 class DossierAdmin(BaseAdmin, VersionAdmin):
32 list_display = ('_poste', '_implantation', 'employe', 'etat', )
33 list_filter = ('etat', )
34 exclude = ('etat', )
35
36 def _poste(self, obj):
37 return obj.poste.nom
38
39 def _implantation(self, obj):
40 return obj.poste.implantation
41
42
43 class StatutForm(forms.ModelForm):
44 commentaire = forms.CharField(label="Commentaire",
45 widget=forms.widgets.Textarea())
46
47 def __init__(self, *args, **kwargs):
48 super(StatutForm, self).__init__(*args, **kwargs)
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
56
57 def save(self, *args, **kwargs):
58 # Le dossier se voit attribuée un commentaire WF verbeux
59 obj = super(StatutForm, self).save(*args, **kwargs)
60 self.wf.content_object = obj
61 self.wf.etat_final = obj.etat
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),
65 self.data['commentaire'])
66 self.wf.texte = commentaire
67 self.wf.save()
68 return obj
69
70
71 class StatutDossierForm(StatutForm):
72
73 class Meta:
74 model = ProxyDossierStatut
75 fields = ('etat', )
76
77
78 class StatutPosteForm(StatutForm):
79
80 class Meta:
81 model = ProxyPosteStatut
82 fields = ('etat', )
83
84
85 class StatutAdmin(BaseAdmin, VersionAdmin):
86 action = None
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):
98 form = super(StatutAdmin, self).get_form(request,
99 obj, **kwargs)
100 form.user = request.user
101 return form
102
103 def _etat(self, obj):
104 return obj.get_etat_display()
105 _etat.short_description = "État"
106 _etat.admin_order_field = "etat"
107
108
109 class DossierStatutAdmin(StatutAdmin):
110 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
111 list_display = ('_poste', '_implantation', 'employe', '_etat', )
112 list_filter = ('poste__implantation', )
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
120 _implantation.short_description = u"Implantation"
121 _implantation.admin_order_field = "poste__implantation"
122
123
124 class PosteStatutAdmin(StatutAdmin):
125 search_fields = ('nom', )
126 list_display = ('nom', 'implantation', )
127 list_filter = ('implantation', 'etat', )
128 form = StatutPosteForm
129
130
131 admin.site.register(Poste, PosteAdmin)
132 admin.site.register(Dossier, DossierAdmin)
133 admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
134 admin.site.register(ProxyPosteStatut, PosteStatutAdmin)