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