1 # -*- encoding: utf-8 -*-
3 from collections
import defaultdict
6 from django
.db
import models
7 from django
import forms
8 from django
.core
.urlresolvers
import reverse
9 from django
.contrib
import admin
10 from django
.conf
import settings
11 from django
.db
.models
import Q
12 from ajax_select
import make_ajax_form
13 from auf
.django
.metadata
.admin
import AUFMetadataAdminMixin
, AUFMetadataInlineAdminMixin
, AUF_METADATA_READONLY_FIELDS
14 from forms
import ContratForm
, AyantDroitForm
, EmployeAdminForm
, AjaxSelect
15 from dae
.utils
import get_employe_from_user
18 # Override of the InlineModelAdmin to support the link in the tabular inline
19 class LinkedInline(admin
.options
.InlineModelAdmin
):
20 template
= "admin/linked.html"
21 admin_model_path
= None
23 def __init__(self
, *args
):
24 super(LinkedInline
, self
).__init__(*args
)
25 if self
.admin_model_path
is None:
26 self
.admin_model_path
= self
.model
.__name__
.lower()
29 class ProtectRegionMixin(object):
31 def queryset(self
, request
):
32 from dae
.workflow
import grp_drh
, grp_correspondants_rh
33 qs
= super(ProtectRegionMixin
, self
).queryset(request
)
35 if request
.user
.is_superuser
:
38 user_groups
= request
.user
.groups
.all()
40 if grp_drh
in user_groups
:
43 if grp_correspondants_rh
in user_groups
:
44 employe
= get_employe_from_user(request
.user
)
45 q
= Q(**{self
.model
.prefix_implantation
: employe
.implantation
.region
})
46 qs
= qs
.filter(q
).distinct()
50 def has_change_permission(self
, request
, obj
=None):
53 ids
= [o
.id for o
in self
.queryset(request
)]
59 class ReadOnlyInlineMixin(object):
60 def get_readonly_fields(self
, request
, obj
=None):
61 return [f
.name
for f
in self
.model
._meta
.fields
if f
.name
not in AUF_METADATA_READONLY_FIELDS
]
64 class AyantDroitInline(AUFMetadataInlineAdminMixin
, admin
.StackedInline
):
71 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', 'lien_parente', )
76 class AyantDroitCommentaireInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
77 readonly_fields
= ('owner', )
78 model
= rh
.AyantDroitCommentaire
82 class ContratInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
88 class DossierROInline(ReadOnlyInlineMixin
, LinkedInline
):
89 template
= "admin/rh/dossier/linked.html"
90 exclude
= AUF_METADATA_READONLY_FIELDS
95 def has_add_permission(self
, request
=None):
98 def has_change_permission(self
, request
, obj
=None):
101 def has_delete_permission(self
, request
, obj
=None):
105 class DossierCommentaireInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
106 readonly_fields
= ('owner', )
107 model
= rh
.DossierCommentaire
111 class DossierPieceInline(admin
.TabularInline
):
112 model
= rh
.DossierPiece
116 class EmployeInline(admin
.TabularInline
):
119 class EmployeCommentaireInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
120 readonly_fields
= ('owner', )
121 model
= rh
.EmployeCommentaire
125 class EmployePieceInline(admin
.TabularInline
):
126 model
= rh
.EmployePiece
130 class PosteCommentaireInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
131 readonly_fields
= ('owner', )
132 model
= rh
.PosteCommentaire
136 class PosteFinancementInline(admin
.TabularInline
):
137 model
= rh
.PosteFinancement
140 class PostePieceInline(admin
.TabularInline
):
141 model
= rh
.PostePiece
144 class RemunerationInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
145 model
= rh
.Remuneration
149 class RemunerationROInline(ReadOnlyInlineMixin
, RemunerationInline
):
153 class TypePosteInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
157 class AyantDroitAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
):
159 L'ajout d'un nouvel ayantdroit se fait dans l'admin de l'employé.
161 alphabet_filter
= 'nom'
162 search_fields
= ('nom', 'prenom', 'employe__nom', 'employe__prenom', )
163 list_display
= ('_employe', 'lien_parente', '_ayantdroit', )
164 inlines
= (AyantDroitCommentaireInline
,)
165 readonly_fields
= AUFMetadataAdminMixin
.readonly_fields
+ ('employe',)
166 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
167 ("Lien avec l'employé", {
168 'fields': (('employe', 'lien_parente'), )
172 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
176 def save_formset(self
, request
, form
, formset
, change
):
177 instances
= formset
.save(commit
=False)
178 for instance
in instances
:
179 if instance
.__class__
== rh
.AyantDroitCommentaire
:
180 instance
.owner
= request
.user
183 def _ayantdroit(self
, obj
):
185 _ayantdroit
.short_description
= u
'Ayant droit'
187 def _employe(self
, obj
):
188 return unicode(obj
.employe
)
189 _employe
.short_description
= u
'Employé'
191 def has_add_permission(self
, request
):
194 class AyantDroitCommentaireAdmin(admin
.ModelAdmin
):
198 class ClassementAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
199 list_display
= ('_classement', 'date_modification', 'user_modification', )
200 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
202 'fields': ('type', 'echelon', 'degre', 'coefficient', )
206 def _classement(self
, obj
):
208 _classement
.short_description
= u
"Classement"
210 class CommentaireAdmin(admin
.ModelAdmin
):
214 class DeviseAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
215 list_display
= ('code', 'nom', 'date_modification', 'user_modification', 'actif', )
216 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
218 'fields': ('code', 'nom', ),
223 class DossierAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
, AjaxSelect
):
224 alphabet_filter
= 'employe__nom'
225 search_fields
= ('employe__nom', 'employe__prenom', 'poste__nom', 'poste__nom_feminin')
238 'poste__implantation__region',
239 'poste__implantation',
241 'poste__type_poste__famille_emploi',
242 'rh_contrats__type_contrat',
245 inlines
= (DossierPieceInline
, ContratInline
,
247 DossierCommentaireInline
,
249 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
251 'fields': ('employe', 'poste', 'statut', 'organisme_bstg',)
254 'fields': ('statut_residence', 'remplacement', 'remplacement_de', )
257 'fields': ('classement', ('regime_travail', 'regime_travail_nb_heure_semaine'),)
259 ('Occupation du Poste par cet Employe', {
260 'fields': (('date_debut', 'date_fin'), )
263 form
= make_ajax_form(rh
.Dossier
, {
264 'employe' : 'employes',
266 'remplacement_de' : 'dossiers',
269 def lookup_allowed(self
, key
, value
):
271 'employe__nom__istartswith',
273 'poste__implantation__region__id__exact',
274 'poste__implantation__id__exact',
275 'poste__type_poste__id__exact',
276 'poste__type_poste__famille_emploi__id__exact',
277 'rh_contrats__type_contrat__id__exact',
281 def _apercu(self
, d
):
282 link
= u
"""<a title="Aperçu du dossier" onclick="return showAddAnotherPopup(this);" href='%s'><img src="%simg/loupe.png" /></a>""" % \
283 (reverse('dossier_apercu', args
=(d
.id,)),
287 _apercu
.allow_tags
= True
288 _apercu
.short_description
= u
''
289 _apercu
.admin_order_field
= ''
292 link
= u
"""<a href="%s" title="Modifier le dossier"><strong>%s</strong></a>""" % \
293 (reverse('admin:rh_dossier_change', args
=(d
.id,)),
297 _id
.allow_tags
= True
298 _id
.short_description
= u
'#'
299 _id
.admin_order_field
= 'id'
302 def _actif(self
, dossier
):
303 if dossier
.employe
.actif
:
304 html
= """<img alt="True" src="%simg/admin/icon-yes.gif">"""
306 html
= """<img alt="False" src="%simg/admin/icon-no.gif">"""
307 return html
% settings
.ADMIN_MEDIA_PREFIX
308 _actif
.allow_tags
= True
309 _actif
.short_description
= u
'Employé actif'
310 _actif
.admin_order_field
= 'employe__actif'
312 def _date_debut(self
, obj
):
313 return obj
.date_debut
314 _date_debut
.short_description
= u
'Occupation début'
315 _date_debut
.admin_order_field
= 'date_debut'
317 def _date_fin(self
, obj
):
319 _date_fin
.short_description
= u
'Occupation fin'
320 _date_fin
.admin_order_field
= 'date_fin'
322 def _poste(self
, dossier
):
323 link
= u
"""<a title="Aperçu du poste" onclick="return showAddAnotherPopup(this);" href='%s'><img src="%simg/loupe.png" /></a> <a href="%s" title="Modifier le poste">%s</a>""" % \
324 (reverse('poste_apercu', args
=(dossier
.poste
.id,)),
326 reverse('admin:rh_poste_change', args
=(dossier
.poste
.id,)),
330 _poste
.allow_tags
= True
331 _poste
.short_description
= u
'Poste'
332 _poste
.admin_order_field
= 'poste__nom'
334 def _employe(self
, obj
):
335 employe
= obj
.employe
336 view_link
= reverse('employe_apercu', args
=(employe
.id,))
337 edit_link
= reverse('admin:rh_employe_change', args
=(employe
.id,))
339 if employe
.actif
== False:
340 style
= "color: grey";
344 view
= u
"""<a href="%s" title="Aperçu l'employé" onclick="return showAddAnotherPopup(this);"><img src="%simg/loupe.png" /></a>""" % (view_link
, settings
.MEDIA_URL
,)
345 return u
"""%s<a href='%s' style="%s;">[%s] %s %s</a>""" % \
346 (view
, edit_link
, style
, employe
.id, employe
.nom
.upper(), employe
.prenom
.title())
347 _employe
.allow_tags
= True
348 _employe
.short_description
= u
"Employé ([code] NOM Prénom)"
349 _employe
.admin_order_field
= "employe__nom"
351 def save_formset(self
, request
, form
, formset
, change
):
352 instances
= formset
.save(commit
=False)
353 for instance
in instances
:
354 if instance
.__class__
== rh
.DossierCommentaire
:
355 instance
.owner
= request
.user
359 class DossierPieceAdmin(admin
.ModelAdmin
):
363 class DossierCommentaireAdmin(admin
.ModelAdmin
):
367 class EmployeAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
):
368 alphabet_filter
= 'nom'
369 DEFAULT_ALPHABET
= u
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
370 search_fields
= ('id', 'nom', 'prenom', 'nom_affichage', )
372 form
= EmployeAdminForm
373 list_display
= ('_apercu', '_nom', '_dossiers', 'date_modification', 'user_modification', 'actif',)
374 list_filter
= ('rh_dossiers__poste__implantation__region', 'rh_dossiers__poste__implantation', 'actif', )
375 inlines
= (AyantDroitInline
,
378 EmployeCommentaireInline
)
379 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
381 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
383 ('Informations personnelles', {
384 'fields': ('situation_famille', 'date_entree', )
387 'fields': (('tel_domicile', 'tel_cellulaire'), ('adresse', 'ville'), ('code_postal', 'province'), 'pays', )
391 def _apercu(self
, obj
):
392 return u
"""<a title="Aperçu de l'employé" onclick="return showAddAnotherPopup(this);" href='%s'><img src="%simg/loupe.png" /></a>""" % \
393 (reverse('employe_apercu', args
=(obj
.id,)), settings
.MEDIA_URL
)
394 _apercu
.allow_tags
= True
395 _apercu
.short_description
= u
""
396 _apercu
.admin_order_field
= ""
399 edit_link
= reverse('admin:rh_employe_change', args
=(obj
.id,))
400 return u
"""<a href='%s'><strong>[%s] %s %s</strong></a>""" % \
401 (edit_link
, obj
.id, obj
.nom
.upper(), obj
.prenom
.title(),)
402 _nom
.allow_tags
= True
403 _nom
.short_description
= u
"Employé ([code] NOM Prénom)"
404 _nom
.admin_order_field
= "nom"
406 def _dossiers(self
, obj
):
408 for d
in obj
.rh_dossiers
.all().order_by('-date_debut'):
410 apercu
= u
"""<a title="Aperçu du dossier" href="%s" onclick="return showAddAnotherPopup(this);" title="Aperçu du dossier"><img src="%simg/loupe.png" /></a>""" % \
411 (reverse('dossier_apercu', args
=(d
.id,)), settings
.MEDIA_URL
,)
413 if d
.date_fin
is not None:
415 style
= u
"color: grey";
416 link
= u
"""<li>%s<a style="%s;" href='%s'>%s : %s</a></li>""" % \
419 reverse('admin:rh_dossier_change', args
=(d
.id,)),
424 return "<ul>%s</ul>" % "\n".join(l
)
425 _dossiers
.allow_tags
= True
426 _dossiers
.short_description
= u
"Dossiers"
428 def queryset(self
, request
):
429 qs
= super(EmployeAdmin
, self
).queryset(request
)
430 return qs
.select_related(depth
=1).order_by('nom')
432 def save_formset(self
, request
, form
, formset
, change
):
433 instances
= formset
.save(commit
=False)
434 for instance
in instances
:
435 if instance
.__class__
== rh
.EmployeCommentaire
:
436 instance
.owner
= request
.user
441 class EmployeCommentaireAdmin(admin
.ModelAdmin
):
445 class EmployePieceAdmin(admin
.ModelAdmin
):
449 class FamilleEmploiAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
450 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
451 inlines
= (TypePosteInline
,)
452 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
459 class OrganismeBstgAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
):
460 search_fields
= ('nom',)
461 list_display
= ('nom', 'type', 'pays', 'date_modification', 'user_modification', 'actif', )
462 list_filter
= ('type', )
463 inlines
= (DossierROInline
,)
464 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
466 'fields': ('nom', 'type', 'pays', )
471 class PosteAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
, AjaxSelect
):
472 form
= make_ajax_form(rh
.Poste
, {
473 'implantation' : 'implantations',
474 'type_poste' : 'typepostes',
475 'responsable' : 'postes',
476 'valeur_point_min' : 'valeurpoints',
477 'valeur_point_max' : 'valeurpoints',
479 alphabet_filter
= 'nom'
480 search_fields
= ('nom',
481 'implantation__code',
483 'implantation__region__code',
484 'implantation__region__nom',
498 list_filter
= ('service',
499 'implantation__region',
502 'type_poste__famille_emploi',
506 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
508 'fields': (('nom', 'nom_feminin'), 'implantation', 'type_poste',
509 'service', 'responsable')
512 'fields': (('regime_travail', 'regime_travail_nb_heure_semaine'), )
515 'fields': (('local', 'expatrie', 'mise_a_disposition', 'appel'),)
518 'fields': (('classement_min', 'valeur_point_min', 'devise_min', 'salaire_min', 'indemn_min', 'autre_min', ),
519 ('classement_max', 'valeur_point_max' ,'devise_max', 'salaire_max', 'indemn_max', 'autre_max', ),
522 ('Comparatifs de rémunération', {
523 'fields': ('devise_comparaison',
524 ('comp_locale_min', 'comp_locale_max'),
525 ('comp_universite_min', 'comp_universite_max'),
526 ('comp_fonctionpub_min', 'comp_fonctionpub_max'),
527 ('comp_ong_min', 'comp_ong_max'),
528 ('comp_autre_min', 'comp_autre_max'))
531 'fields': ('justification',)
533 ('Autres Méta-données', {
534 'fields': ('date_debut', 'date_fin')
538 inlines
= (PosteFinancementInline
,
541 PosteCommentaireInline
, )
544 def _apercu(self
, poste
):
545 link
= u
"""<a onclick="return showAddAnotherPopup(this);" title="Aperçu du poste" href='%s'><img src="%simg/loupe.png" /></a>""" % \
546 (reverse('poste_apercu', args
=(poste
.id,)),
550 _apercu
.allow_tags
= True
551 _apercu
.short_description
= u
''
552 _apercu
.admin_order_field
= ''
554 def _service(self
, obj
):
557 def _nom(self
, poste
):
558 link
= u
"""<a href="%s" title="Modifier le poste"><strong>%s</strong></a>""" % \
559 (reverse('admin:rh_poste_change', args
=(poste
.id,)),
563 _nom
.allow_tags
= True
564 _nom
.short_description
= u
'Nom'
565 _nom
.admin_order_field
= 'nom'
567 def _occupe_par(self
, obj
):
568 """Formatte la méthode Poste.occupe_par() pour l'admin"""
570 if obj
.actif
is False:
572 employes
= obj
.occupe_par()
576 link
= "<a href='%s' title='Aperçu de l\'employer' onclick='return showAddAnotherPopup(this)'><img src='%simg/loupe.png' /></a> <a href='%s'>%s</a>" % \
577 (reverse('employe_apercu', args
=(e
.id,)),
579 reverse('admin:rh_employe_change', args
=(e
.id,)),
583 output
= "\n<br />".join(l
)
585 _occupe_par
.allow_tags
= True
586 _occupe_par
.short_description
= "Occupé par"
588 def save_formset(self
, request
, form
, formset
, change
):
589 instances
= formset
.save(commit
=False)
590 for instance
in instances
:
591 if instance
.__class__
== rh
.PosteCommentaire
:
592 instance
.owner
= request
.user
597 class PosteCommentaireAdmin(admin
.ModelAdmin
):
601 class PosteFinancementAdmin(admin
.ModelAdmin
):
605 class PostePieceAdmin(admin
.ModelAdmin
):
609 class RemunerationAdmin(admin
.ModelAdmin
):
613 class ResponsableImplantationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
614 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
616 'fields': ('employe', 'implantation', ),
621 class ServiceAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
622 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
623 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
629 class StatutAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
630 list_display
= ('code', 'nom', 'date_modification', 'user_modification', 'actif', )
631 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
633 'fields': ('code', 'nom', ),
637 class TauxChangeAdmin(admin
.ModelAdmin
):
638 list_display
= ('taux', 'devise', 'annee', 'date_modification', 'user_modification', )
639 list_filter
= ('devise', )
640 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
642 'fields': ('taux', 'devise', 'annee', ),
646 class TypeContratAdmin(admin
.ModelAdmin
):
647 list_display
= ('nom', 'nom_long', 'date_modification', 'user_modification', 'actif', )
648 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
650 'fields': ('nom', 'nom_long', ),
655 class TypePosteAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
656 search_fields
= ('nom', 'nom_feminin', )
657 list_display
= ('nom', 'famille_emploi', 'date_modification', 'user_modification', 'actif', )
658 list_filter
= ('famille_emploi', )
659 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
661 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
666 class TypeRemunerationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
667 list_display
= ('nom', 'type_paiement', 'nature_remuneration', 'date_modification', 'user_modification', 'actif', )
668 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
670 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
675 class TypeRevalorisationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
676 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
677 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
684 class ValeurPointAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
685 list_display
= ('_devise_code', '_devise_nom', 'annee', 'valeur', 'date_modification', 'user_modification', )
686 list_filter
= ('annee', 'devise', )
687 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
689 'fields': ('valeur', 'devise', 'implantation', 'annee', )
693 def _devise_code(self
, obj
):
694 return obj
.devise
.code
695 _devise_code
.short_description
= "Code de la devise"
697 def _devise_nom(self
, obj
):
698 return obj
.devise
.nom
699 _devise_nom
.short_description
= "Nom de la devise"
702 admin
.site
.register(rh
.Classement
, ClassementAdmin
)
703 admin
.site
.register(rh
.Devise
, DeviseAdmin
)
704 admin
.site
.register(rh
.Dossier
, DossierAdmin
)
705 admin
.site
.register(rh
.Employe
, EmployeAdmin
)
706 admin
.site
.register(rh
.FamilleEmploi
, FamilleEmploiAdmin
)
707 admin
.site
.register(rh
.OrganismeBstg
, OrganismeBstgAdmin
)
708 admin
.site
.register(rh
.Poste
, PosteAdmin
)
709 admin
.site
.register(rh
.ResponsableImplantation
, ResponsableImplantationAdmin
)
710 admin
.site
.register(rh
.Service
, ServiceAdmin
)
711 admin
.site
.register(rh
.Statut
, StatutAdmin
)
712 admin
.site
.register(rh
.TauxChange
, TauxChangeAdmin
)
713 admin
.site
.register(rh
.TypeContrat
, TypeContratAdmin
)
714 admin
.site
.register(rh
.TypePoste
, TypePosteAdmin
)
715 admin
.site
.register(rh
.TypeRemuneration
, TypeRemunerationAdmin
)
716 admin
.site
.register(rh
.TypeRevalorisation
, TypeRevalorisationAdmin
)
717 admin
.site
.register(rh
.ValeurPoint
, ValeurPointAdmin
)