e832a89988692283cd107c7670b80b33d1d0bb7a
[auf_rh_dae.git] / project / rh / lib.py
1 # -*- encoding: utf-8 -*-
2
3 from django.db import models
4 from django.contrib import admin
5 from django.conf import settings
6 from django.db.models import Q
7 from auf.django.metadata.admin import AUFMetadataAdminMixin, AUFMetadataInlineAdminMixin, AUF_METADATA_READONLY_FIELDS
8 from project.rh import models as rh
9 from forms import DossierForm, ContratForm
10 from dae.utils import get_employe_from_user
11
12
13 class ProtectRegionMixin(object):
14
15 def queryset(self, request):
16 qs = super(ProtectRegionMixin, self).queryset(request)
17
18 if request.user.is_superuser:
19 return qs
20
21 employe = get_employe_from_user(request.user)
22
23 q = Q(**{self.model.prefix_implantation: employe.implantation.region})
24 qs = qs.filter(q).distinct()
25 return qs
26
27 def has_change_permission(self, request, obj=None):
28 if request.user.is_superuser:
29 return True
30
31 if obj:
32 employe = get_employe_from_user(request.user)
33 if employe.implantation.region in obj.get_regions():
34 return True
35 else:
36 return False
37
38 return True
39
40
41 # Inlines
42
43 class ReadOnlyInlineMixin(object):
44 def get_readonly_fields(self, request, obj=None):
45 return [f.name for f in self.model._meta.fields if f.name not in AUF_METADATA_READONLY_FIELDS]
46
47
48 class AyantDroitInline(AUFMetadataInlineAdminMixin, admin.StackedInline):
49 model = models.Model # à remplacer dans admin.py
50 extra = 0
51
52
53 class AyantDroitCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
54 readonly_fields = ('owner', )
55 model = models.Model # à remplacer dans admin.py
56 extra = 1
57
58
59 class ContratInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
60 form = ContratForm
61 model = models.Model # à remplacer dans admin.py
62 extra = 1
63
64
65 class DossierROInline(ReadOnlyInlineMixin, admin.TabularInline):
66 exclude = AUF_METADATA_READONLY_FIELDS
67 model = models.Model # à remplacer dans admin.py
68
69
70 class DossierCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
71 readonly_fields = ('owner', )
72 model = models.Model # à remplacer dans admin.py
73 extra = 1
74
75
76 class DossierPieceInline(admin.TabularInline):
77 model = models.Model # à remplacer dans admin.py
78 extra = 4
79
80
81 class EmployeInline(admin.TabularInline):
82 model = models.Model # à remplacer dans admin.py
83
84 class EmployeCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
85 readonly_fields = ('owner', )
86 model = models.Model # à remplacer dans admin.py
87 extra = 1
88
89
90 class EmployePieceInline(admin.TabularInline):
91 model = models.Model # à remplacer dans admin.py
92 extra = 4
93
94
95 class EvenementInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
96 model = models.Model # à remplacer dans admin.py
97 extra = 1
98
99
100 class EvenementRemunerationInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
101 model = models.Model # à remplacer dans admin.py
102 extra = 1
103
104
105 class PosteCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
106 readonly_fields = ('owner', )
107 model = models.Model # à remplacer dans admin.py
108 extra = 1
109
110
111 class PosteFinancementInline(admin.TabularInline):
112 model = models.Model # à remplacer dans admin.py
113
114
115 class PostePieceInline(admin.TabularInline):
116 model = models.Model # à remplacer dans admin.py
117
118
119 class RemunerationInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
120 model = models.Model # à remplacer dans admin.py
121 extra = 1
122
123
124 class RemunerationROInline(ReadOnlyInlineMixin, RemunerationInline):
125 pass
126
127
128 class TypePosteInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
129 model = models.Model # à remplacer dans admin.py
130
131
132 # Admins
133
134 class AyantDroitAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
135 """
136 L'ajout d'un nouvel ayantdroit se fait dans l'admin de l'employé.
137 """
138 alphabet_filter = 'nom'
139 search_fields = ('nom', 'prenom', 'employe__nom', 'employe__prenom', )
140 list_display = ('_employe', 'lien_parente', '_ayantdroit', )
141 inlines = (AyantDroitCommentaireInline,)
142 readonly_fields = AUFMetadataAdminMixin.readonly_fields + ('employe',)
143 fieldsets = AUFMetadataAdminMixin.fieldsets + (
144 ("Lien avec l'employé", {
145 'fields': ('employe', 'lien_parente', )
146 }),
147
148 ('Identification', {
149 'fields': ('nom', 'prenom', 'nom_affichage', 'nationalite', 'date_naissance', 'genre', )
150 }),
151 )
152
153 def save_formset(self, request, form, formset, change):
154 instances = formset.save(commit=False)
155 for instance in instances:
156 if instance.__class__ == rh.AyantDroitCommentaire:
157 instance.owner = request.user
158 instance.save()
159
160 def _ayantdroit(self, obj):
161 return unicode(obj)
162 _ayantdroit.short_description = u'Ayant droit'
163
164 def _employe(self, obj):
165 return unicode(obj.employe)
166 _employe.short_description = u'Employé'
167
168 def has_add_permission(self, request):
169 return False
170
171 class AyantDroitCommentaireAdmin(admin.ModelAdmin):
172 pass
173
174
175 class ClassementAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
176 fieldsets = AUFMetadataAdminMixin.fieldsets + (
177 (None, {
178 'fields': ('type', 'echelon', 'degre', 'coefficient', )
179 }),
180 )
181
182
183 class CommentaireAdmin(admin.ModelAdmin):
184 pass
185
186
187 #class ContratAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
188 # form = ContratForm
189 # alphabet_filter = 'dossier__employe__nom'
190 # search_fields = ('dossier__employe__nom', 'dossier__employe__prenom', 'dossier__poste__nom', 'dossier__poste__nom_feminin', )
191 # list_display = ('id', '_employe', '_poste', 'date_debut', 'date_fin', '_implantation', )
192 # fieldsets = AUFMetadataAdminMixin.fieldsets + (
193 # (None, {
194 # 'fields': ('dossier', 'type_contrat', 'date_debut', 'date_fin', )
195 # }),
196 # )
197 #
198 # def lookup_allowed(self, key, value):
199 # if key in ('dossier__employe__nom__istartswith', ):
200 # return True
201 #
202 # def _employe(self, obj):
203 # return unicode(obj.dossier.employe)
204 # _employe.short_description = "Employé"
205 #
206 # def _poste(self, obj):
207 # return obj.dossier.poste.nom
208 # _poste.short_description = "Poste"
209 #
210 # def _implantation(self, obj):
211 # return obj.dossier.poste.implantation
212 # _poste.short_description = "Implantation"
213
214 class DeviseAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
215 fieldsets = AUFMetadataAdminMixin.fieldsets + (
216 (None, {
217 'fields': ('code', 'nom', ),
218 }),
219 )
220
221
222 class DossierAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin,):
223 form = DossierForm
224 alphabet_filter = 'employe__nom'
225 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', 'poste__nom_feminin')
226 list_display = ('_employe', '_poste', 'date_debut', 'date_fin', '_actif')
227 inlines = (DossierPieceInline, ContratInline,
228 RemunerationInline,
229 #EvenementInline,
230 DossierCommentaireInline,
231 )
232 fieldsets = AUFMetadataAdminMixin.fieldsets + (
233 ('Identification', {
234 'fields': ('employe', 'poste', 'statut', 'organisme_bstg',)
235 }),
236 ('Recrutement', {
237 'fields': ('remplacement', 'statut_residence', )
238 }),
239 ('Rémunération', {
240 'fields': ('classement', 'regime_travail', 'regime_travail_nb_heure_semaine',)
241 }),
242 ('Occupation du Poste par cet Employe', {
243 'fields': ('date_debut', 'date_fin', )
244 }),
245 )
246
247 def lookup_allowed(self, key, value):
248 if key in ('employe__nom__istartswith', 'actif__exact', ):
249 return True
250
251 def _actif(self, dossier):
252 if dossier.employe.actif:
253 html = """<img alt="True" src="%simg/admin/icon-yes.gif">"""
254 else:
255 html = """<img alt="False" src="%simg/admin/icon-no.gif">"""
256 return html % settings.ADMIN_MEDIA_PREFIX
257 _actif.allow_tags = u'Employé actif'
258 _actif.short_description = u'Employé actif'
259 _actif.admin_order_field = 'employe__actif'
260
261 def _poste(self, dossier):
262 return unicode(dossier.poste.nom)
263 _poste.short_description = u'Poste'
264 _poste.admin_order_field = 'poste__nom'
265
266 def _employe(self, dossier):
267 return unicode(dossier.employe)
268 _employe.short_description = u'Employé'
269 _employe.admin_order_field = 'employe__nom'
270
271 def save_formset(self, request, form, formset, change):
272 instances = formset.save(commit=False)
273 for instance in instances:
274 if instance.__class__ == rh.DossierCommentaire:
275 instance.owner = request.user
276 instance.save()
277
278 class DossierPieceAdmin(admin.ModelAdmin):
279 pass
280
281
282 class DossierCommentaireAdmin(admin.ModelAdmin):
283 pass
284
285
286 class EmployeAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
287 alphabet_filter = 'nom'
288 search_fields = ('id', 'nom', 'prenom', 'nom_affichage', 'actif', )
289 list_filter = ('actif', )
290 ordering = ('nom', )
291 actions = ('desactiver', )
292 list_display = ('nom', 'prenom', 'actif', )
293 inlines = (AyantDroitInline,
294 DossierROInline,
295 EmployePieceInline,
296 EmployeCommentaireInline)
297 fieldsets = AUFMetadataAdminMixin.fieldsets + (
298 ('Identification', {
299 'fields': ('nom', 'prenom', 'nom_affichage', 'nationalite', 'date_naissance', 'genre', )
300 }),
301 ('Informations personnelles', {
302 'fields': ('situation_famille', 'date_entree', )
303 }),
304 ('Coordonnées', {
305 'fields': ('tel_domicile', 'tel_cellulaire', 'adresse', 'ville', 'province', 'code_postal', 'pays', )
306 }),
307 )
308
309 def save_formset(self, request, form, formset, change):
310 instances = formset.save(commit=False)
311 for instance in instances:
312 if instance.__class__ == rh.EmployeCommentaire:
313 instance.owner = request.user
314 instance.save()
315
316 class EmployeCommentaireAdmin(admin.ModelAdmin):
317 pass
318
319
320 class EmployePieceAdmin(admin.ModelAdmin):
321 pass
322
323
324 class EvenementAdmin(admin.ModelAdmin):
325 inlines = (EvenementRemunerationInline,)
326
327
328 class EvenementRemunerationAdmin(admin.ModelAdmin):
329 pass
330
331
332 class FamilleEmploiAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
333 inlines = (TypePosteInline,)
334 fieldsets = AUFMetadataAdminMixin.fieldsets + (
335 (None, {
336 'fields': ('nom', )
337 }),
338 )
339
340
341 class OrganismeBstgAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
342 search_fields = ('nom', )
343 list_display = ('nom', 'type', 'pays', )
344 inlines = (DossierROInline,)
345 fieldsets = AUFMetadataAdminMixin.fieldsets + (
346 (None, {
347 'fields': ('nom', 'type', 'pays', )
348 }),
349 )
350
351
352 class PosteAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
353 alphabet_filter = 'nom'
354 search_fields = ('nom', 'implantation__code', 'implantation__nom', 'implantation__region__code', 'implantation__region__nom', )
355 list_display = ('nom', 'implantation', 'service', 'type_poste', 'date_debut', 'date_fin', )
356 fieldsets = AUFMetadataAdminMixin.fieldsets + (
357 (None, {
358 'fields': ('nom', 'nom_feminin', 'implantation', 'type_poste',
359 'service', 'responsable')
360 }),
361 ('Contrat', {
362 'fields': ('regime_travail', 'regime_travail_nb_heure_semaine')
363 }),
364 ('Recrutement', {
365 'fields': ('local', 'expatrie', 'mise_a_disposition', 'appel')
366 }),
367 ('Rémunération', {
368 'fields': ('classement_min', 'classement_max', 'valeur_point_min',
369 'valeur_point_max', 'devise_min', 'devise_max',
370 'salaire_min', 'salaire_max', 'indemn_min',
371 'indemn_max', 'autre_min', 'autre_max')
372 }),
373 ('Comparatifs de rémunération', {
374 'fields': ('devise_comparaison', 'comp_locale_min',
375 'comp_locale_max', 'comp_universite_min',
376 'comp_universite_max', 'comp_fonctionpub_min',
377 'comp_fonctionpub_max', 'comp_ong_min', 'comp_ong_max',
378 'comp_autre_min', 'comp_autre_max')
379 }),
380 ('Justification', {
381 'fields': ('justification',)
382 }),
383 ('Autres Metadata', {
384 'fields': ('date_validation', 'date_debut', 'date_fin')
385 }),
386 )
387
388 inlines = (PosteFinancementInline,
389 PostePieceInline,
390 DossierROInline,
391 PosteCommentaireInline, )
392
393 def save_formset(self, request, form, formset, change):
394 instances = formset.save(commit=False)
395 for instance in instances:
396 if instance.__class__ == rh.PosteCommentaire:
397 instance.owner = request.user
398 instance.save()
399 formset.save_m2m()
400
401
402 class PosteCommentaireAdmin(admin.ModelAdmin):
403 pass
404
405
406 class PosteFinancementAdmin(admin.ModelAdmin):
407 pass
408
409
410 class PostePieceAdmin(admin.ModelAdmin):
411 pass
412
413
414 class RemunerationAdmin(admin.ModelAdmin):
415 pass
416
417
418 class ResponsableImplantationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
419 fieldsets = AUFMetadataAdminMixin.fieldsets + (
420 (None, {
421 'fields': ('employe', 'implantation', ),
422 }),
423 )
424
425
426 class ServiceAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
427 list_display = ('nom', 'actif', )
428 fieldsets = AUFMetadataAdminMixin.fieldsets + (
429 (None, {
430 'fields': ('nom', ),
431 }),
432 )
433
434 class StatutAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
435 fieldsets = AUFMetadataAdminMixin.fieldsets + (
436 (None, {
437 'fields': ('code', 'nom', ),
438 }),
439 )
440
441 class TauxChangeAdmin(admin.ModelAdmin):
442 list_display = ('taux', 'devise', 'annee', )
443 list_filter = ('devise', )
444 fieldsets = AUFMetadataAdminMixin.fieldsets + (
445 (None, {
446 'fields': ('taux', 'devise', 'annee', ),
447 }),
448 )
449
450 class TypeContratAdmin(admin.ModelAdmin):
451 inlines = (ContratInline,)
452
453
454 class TypePosteAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
455 search_fields = ('nom', 'nom_feminin', )
456 list_display = ('nom', 'famille_emploi', )
457 list_filter = ('famille_emploi', )
458 fieldsets = AUFMetadataAdminMixin.fieldsets + (
459 (None, {
460 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
461 }),
462 )
463
464
465 class TypeRemunerationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
466 list_display = ('nom', 'type_paiement', 'nature_remuneration', )
467 #inlines = (RemunerationROInline,) utilité?
468 fieldsets = AUFMetadataAdminMixin.fieldsets + (
469 (None, {
470 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
471 }),
472 )
473
474
475 class TypeRevalorisationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
476 #inlines = (RemunerationROInline,) utilité?
477 fieldsets = AUFMetadataAdminMixin.fieldsets + (
478 (None, {
479 'fields': ('nom', )
480 }),
481 )
482
483
484 class ValeurPointAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
485 list_display = ('_devise_code', '_devise_nom', 'annee', 'valeur', )
486 fieldsets = AUFMetadataAdminMixin.fieldsets + (
487 (None, {
488 'fields': ('valeur', 'devise', 'implantation', 'annee', )
489 }),
490 )
491
492 def _devise_code(self, obj):
493 return obj.devise.code
494 _devise_code.short_description = "Code de la devise"
495
496 def _devise_nom(self, obj):
497 return obj.devise.nom
498 _devise_nom.short_description = "Nom de la devise"