Fix django ajax selects
[auf_rh_dae.git] / project / dae / admin.py
CommitLineData
17353922
OL
1# -*- encoding: utf-8 -*-
2
460771c1 3from django import forms
47d7067b 4from django.contrib import admin
75f0e87b 5
17353922 6from reversion.admin import VersionAdmin
5a1f75cb 7
75f0e87b
DB
8from auf.django.workflow.models import WorkflowCommentaire
9
de151a1e 10from project.dae.models import Poste, Dossier
5a1f75cb 11
746d94af 12
08a9b6fc
EMS
13class BaseAdmin(admin.ModelAdmin):
14
15 class Media:
c05a135e
EMS
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 )
08a9b6fc
EMS
24
25
26class PosteAdmin(BaseAdmin, VersionAdmin):
b473fbb6 27 list_display = ('nom', 'implantation', 'etat', )
515124ec 28
a6245d0f 29
08a9b6fc 30class DossierAdmin(BaseAdmin, VersionAdmin):
4be5ead0 31 list_display = ('_poste', '_implantation', 'employe', 'etat', )
317ce433 32 list_filter = ('etat', )
460771c1 33 exclude = ('etat', )
4be5ead0
OL
34
35 def _poste(self, obj):
36 return obj.poste.nom
37
38 def _implantation(self, obj):
39 return obj.poste.implantation
17353922 40
a6245d0f 41
460771c1
OL
42class ProxyDossierStatut(Dossier):
43 class Meta:
44 proxy = True
45 verbose_name = "Statut du dossier"
46 verbose_name_plural = "Statut des dossiers"
47
48
a6245d0f
OL
49class ProxyPosteStatut(Poste):
50 class Meta:
51 proxy = True
52 verbose_name = "Statut du poste"
53 verbose_name_plural = "Statut des postes"
54
55
56class StatutForm(forms.ModelForm):
460771c1
OL
57 commentaire = forms.CharField(label="Commentaire",
58 widget=forms.widgets.Textarea())
59
460771c1 60 def __init__(self, *args, **kwargs):
a6245d0f 61 super(StatutForm, self).__init__(*args, **kwargs)
460771c1
OL
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
746d94af 69
460771c1
OL
70 def save(self, *args, **kwargs):
71 # Le dossier se voit attribuée un commentaire WF verbeux
a6245d0f 72 obj = super(StatutForm, self).save(*args, **kwargs)
460771c1
OL
73 self.wf.content_object = obj
74 self.wf.etat_final = obj.etat
df625f22
OL
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),
460771c1
OL
78 self.data['commentaire'])
79 self.wf.texte = commentaire
80 self.wf.save()
81 return obj
460771c1 82
460771c1 83
a6245d0f 84class StatutDossierForm(StatutForm):
460771c1 85
a6245d0f
OL
86 class Meta:
87 model = ProxyDossierStatut
88 fields = ('etat', )
460771c1 89
460771c1 90
a6245d0f 91class StatutPosteForm(StatutForm):
04a96961 92
a6245d0f
OL
93 class Meta:
94 model = ProxyPosteStatut
95 fields = ('etat', )
460771c1 96
a6245d0f 97
08a9b6fc 98class StatutAdmin(BaseAdmin, VersionAdmin):
a6245d0f 99 action = None
460771c1
OL
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):
a6245d0f 111 form = super(StatutAdmin, self).get_form(request,
460771c1
OL
112 obj, **kwargs)
113 form.user = request.user
114 return form
115
a6245d0f
OL
116 def _etat(self, obj):
117 return obj.get_etat_display()
df625f22
OL
118 _etat.short_description = "État"
119 _etat.admin_order_field = "etat"
a6245d0f 120
5a1f75cb 121
a6245d0f
OL
122class DossierStatutAdmin(StatutAdmin):
123 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
124 list_display = ('_poste', '_implantation', 'employe', '_etat', )
df625f22 125 list_filter = ('poste__implantation', )
a6245d0f
OL
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
df625f22
OL
133 _implantation.short_description = u"Implantation"
134 _implantation.admin_order_field = "poste__implantation"
a6245d0f 135
5a1f75cb 136
a6245d0f
OL
137class PosteStatutAdmin(StatutAdmin):
138 search_fields = ('nom', )
df625f22
OL
139 list_display = ('nom', 'implantation', )
140 list_filter = ('implantation', 'etat', )
a6245d0f
OL
141 form = StatutPosteForm
142
143
b318efda
OL
144admin.site.register(Poste, PosteAdmin)
145admin.site.register(Dossier, DossierAdmin)
460771c1 146admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
a6245d0f 147admin.site.register(ProxyPosteStatut, PosteStatutAdmin)