Merge branch 'hotfix' into olivier
[auf_rh_dae.git] / project / dae / admin.py
1 # -*- encoding: utf-8 -*-
2
3 from django import forms
4 from django.core.urlresolvers import reverse
5 from django.contrib import admin
6 from django.contrib import messages
7 from django.shortcuts import redirect
8 from reversion.admin import VersionAdmin
9 from auf.django.workflow.models import WorkflowCommentaire
10 from models import Poste, Dossier, DossierFinalise
11
12 class PosteAdmin(VersionAdmin):
13 list_display = ('nom', 'implantation', 'etat', )
14
15 class DossierAdmin(VersionAdmin):
16 list_display = ('_poste', '_implantation', 'employe', 'etat', )
17 list_filter = ('etat', )
18 exclude = ('etat', )
19
20 def _poste(self, obj):
21 return obj.poste.nom
22
23 def _implantation(self, obj):
24 return obj.poste.implantation
25
26 class ProxyDossierStatut(Dossier):
27 class Meta:
28 proxy = True
29 verbose_name = "Statut du dossier"
30 verbose_name_plural = "Statut des dossiers"
31
32
33 class StatutDossierForm(forms.ModelForm):
34
35 commentaire = forms.CharField(label="Commentaire",
36 widget=forms.widgets.Textarea())
37
38 class Meta:
39 model = ProxyDossierStatut
40 fields = ('etat', )
41
42 def __init__(self, *args, **kwargs):
43 super(StatutDossierForm, self).__init__(*args, **kwargs)
44
45 # On retire le controle check des états pour faire ce qu'on veut
46 self._meta.model.__setattr__ = object.__setattr__
47
48 # on prépare le commentaire
49 self.wf = WorkflowCommentaire()
50 self.wf.etat_initial = self.instance.etat
51 self.wf.owner_id = self.user.id
52
53 def save(self, *args, **kwargs):
54 # Le dossier se voit attribuée un commentaire WF verbeux
55 obj = super(StatutDossierForm, self).save(*args, **kwargs)
56 self.wf.content_object = obj
57 self.wf.etat_final = obj.etat
58 commentaire = u"%s => %s : %s" % (self.wf.etat_initial, self.wf.etat_final,
59 self.data['commentaire'])
60 self.wf.texte = commentaire
61 self.wf.save()
62 return obj
63
64
65 class DossierStatutAdmin(VersionAdmin):
66 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', )
67 list_display = ('_poste', '_implantation', 'employe', '_etat', )
68 form = StatutDossierForm
69 action = None
70
71 def _poste(self, obj):
72 return obj.poste.nom
73
74 def _implantation(self, obj):
75 return obj.poste.implantation
76
77 def _etat(self, obj):
78 return obj.get_etat_display()
79
80 def has_delete_permission(self, request, obj=None):
81 return False
82
83 def has_add_permission(self, request):
84 return False
85
86 def has_change_permission(self, request, obj=None):
87 return request.user.is_superuser
88
89 def get_form(self, request, obj=None, **kwargs):
90 form = super(DossierStatutAdmin, self).get_form(request,
91 obj, **kwargs)
92 form.user = request.user
93 return form
94
95
96 class ImportDossierAdmin(admin.ModelAdmin):
97 list_display = ('_poste', '_employe', '_dae', )
98 actions = ['importer_dans_RH', ]
99
100 def _employe(self, obj):
101 return obj.employe
102
103 def _poste(self, obj):
104 return obj.poste.nom
105
106 def _implantation(self, obj):
107 return obj.poste.implantation
108
109 def _dae(self, obj):
110 url = reverse('embauche_consulter', args=(obj.id, ))
111 return "<a href='%s'>voir la DAE</a>" % url
112 _dae.allow_tags = True
113
114 def has_delete_permission(self, request, obj=None):
115 return False
116
117 def has_add_permission(self, request):
118 return False
119
120 def has_change_permission(self, request, obj=None):
121 if obj is None:
122 return True
123 else:
124 return False
125
126 def get_actions(self, request):
127 actions = super(ImportDossierAdmin, self).get_actions(request)
128 del actions['delete_selected']
129 return actions
130
131 def importer_dans_RH(self, request, data):
132 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
133 dae = [d for d in Dossier.objects.filter(id__in=selected)]
134 for d in dae:
135 copie = d.importer()
136 messages.add_message(request, messages.SUCCESS, u"%s importé dans\
137 RH" % d)
138 url = reverse('admin:rh_dossier_change', args=(copie.id, ))
139 return redirect(url)
140
141 admin.site.register(Poste, PosteAdmin)
142 admin.site.register(Dossier, DossierAdmin)
143 admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
144 admin.site.register(DossierFinalise, ImportDossierAdmin)