poste import + liens dans RH
[auf_rh_dae.git] / project / dae / admin.py
CommitLineData
17353922
OL
1# -*- encoding: utf-8 -*-
2
460771c1 3from django import forms
47d7067b
OL
4from django.core.urlresolvers import reverse
5from django.contrib import admin
6from django.contrib import messages
7from django.shortcuts import redirect
17353922 8from reversion.admin import VersionAdmin
460771c1 9from auf.django.workflow.models import WorkflowCommentaire
a47ed016 10from models import Poste, Dossier, DossierFinalise, PosteFinalise
17353922
OL
11
12class PosteAdmin(VersionAdmin):
b473fbb6 13 list_display = ('nom', 'implantation', 'etat', )
515124ec 14
4be5ead0
OL
15class DossierAdmin(VersionAdmin):
16 list_display = ('_poste', '_implantation', 'employe', 'etat', )
317ce433 17 list_filter = ('etat', )
460771c1 18 exclude = ('etat', )
4be5ead0
OL
19
20 def _poste(self, obj):
21 return obj.poste.nom
22
23 def _implantation(self, obj):
24 return obj.poste.implantation
17353922 25
460771c1
OL
26class ProxyDossierStatut(Dossier):
27 class Meta:
28 proxy = True
29 verbose_name = "Statut du dossier"
30 verbose_name_plural = "Statut des dossiers"
31
32
33class 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
65class 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
a47ed016 95class ImportAdmin(admin.ModelAdmin,):
47d7067b
OL
96
97 def has_delete_permission(self, request, obj=None):
98 return False
99
100 def has_add_permission(self, request):
101 return False
102
103 def has_change_permission(self, request, obj=None):
104 if obj is None:
105 return True
106 else:
107 return False
108
109 def get_actions(self, request):
a47ed016 110 actions = super(ImportAdmin, self).get_actions(request)
47d7067b
OL
111 del actions['delete_selected']
112 return actions
113
a47ed016
OL
114
115class ImportDossierAdmin(ImportAdmin):
116 list_display = ('_poste', '_employe', '_dae', )
117 actions = ['importer_dans_RH', ]
118
119 def _employe(self, obj):
120 return obj.employe
121
122 def _poste(self, obj):
123 rh = obj.poste.rh_importation()
124 if rh is not None:
125 extra = u"<a href='%s' style='color: red;'> (déjà importé)</a>" % \
126 reverse('admin:rh_poste_change', args=(rh.id,))
127 else:
128 extra = u""
129
130 return u"%s%s" % (obj.poste.nom, extra)
131 _poste.allow_tags = True
132
133 def _implantation(self, obj):
134 return obj.poste.implantation
135
136 def _dae(self, obj):
137 url = reverse('embauche_consulter', args=(obj.id, ))
138 return "<a href='%s'>voir la DAE</a>" % url
139 _dae.allow_tags = True
140
47d7067b
OL
141 def importer_dans_RH(self, request, data):
142 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
143 dae = [d for d in Dossier.objects.filter(id__in=selected)]
144 for d in dae:
145 copie = d.importer()
146 messages.add_message(request, messages.SUCCESS, u"%s importé dans\
147 RH" % d)
148 url = reverse('admin:rh_dossier_change', args=(copie.id, ))
149 return redirect(url)
150
a47ed016
OL
151
152class ImportPosteAdmin(ImportAdmin):
153 list_display = ('nom', '_implantation', '_dae', )
154 actions = ['importer_dans_RH', ]
155
156 def _implantation(self, obj):
157 return obj.implantation
158
159 def _dae(self, obj):
160 url = reverse('poste_consulter', args=(obj.id, ))
161 return "<a href='%s'>voir le poste dans DAE</a>" % url
162 _dae.allow_tags = True
163
164 def importer_dans_RH(self, request, data):
165 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
166 dae = [d for d in Poste.objects.filter(id__in=selected)]
167 for p in dae:
168 copie = p.importer()
169 messages.add_message(request, messages.SUCCESS, u"%s importé dans\
170 RH" % d)
171 url = reverse('admin:rh_poste_change', args=(copie.id, ))
172 return redirect(url)
173
b318efda
OL
174admin.site.register(Poste, PosteAdmin)
175admin.site.register(Dossier, DossierAdmin)
460771c1 176admin.site.register(ProxyDossierStatut, DossierStatutAdmin)
47d7067b 177admin.site.register(DossierFinalise, ImportDossierAdmin)
a47ed016 178admin.site.register(PosteFinalise, ImportPosteAdmin)