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