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 django
.template
.defaultfilters
import date
13 from ajax_select
import make_ajax_form
14 from auf
.django
.metadata
.admin
import AUFMetadataAdminMixin
, AUFMetadataInlineAdminMixin
, AUF_METADATA_READONLY_FIELDS
15 from forms
import ContratForm
, AyantDroitForm
, EmployeAdminForm
, AjaxSelect
16 from dae
.utils
import get_employe_from_user
19 # Override of the InlineModelAdmin to support the link in the tabular inline
20 class LinkedInline(admin
.options
.InlineModelAdmin
):
21 template
= "admin/linked.html"
22 admin_model_path
= None
24 def __init__(self
, *args
):
25 super(LinkedInline
, self
).__init__(*args
)
26 if self
.admin_model_path
is None:
27 self
.admin_model_path
= self
.model
.__name__
.lower()
30 class ProtectRegionMixin(object):
32 def queryset(self
, request
):
33 from dae
.workflow
import grp_drh
, grp_correspondants_rh
34 qs
= super(ProtectRegionMixin
, self
).queryset(request
)
36 if request
.user
.is_superuser
:
39 user_groups
= request
.user
.groups
.all()
41 if grp_drh
in user_groups
:
44 if grp_correspondants_rh
in user_groups
:
45 employe
= get_employe_from_user(request
.user
)
46 q
= Q(**{self
.model
.prefix_implantation
: employe
.implantation
.region
})
47 qs
= qs
.filter(q
).distinct()
51 def has_change_permission(self
, request
, obj
=None):
54 ids
= [o
.id for o
in self
.queryset(request
)]
60 class ReadOnlyInlineMixin(object):
61 def get_readonly_fields(self
, request
, obj
=None):
62 return [f
.name
for f
in self
.model
._meta
.fields
if f
.name
not in AUF_METADATA_READONLY_FIELDS
]
65 class AyantDroitInline(AUFMetadataInlineAdminMixin
, admin
.StackedInline
):
72 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', 'lien_parente', )
77 class AyantDroitCommentaireInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
78 readonly_fields
= ('owner', )
79 model
= rh
.AyantDroitCommentaire
83 class ContratInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
89 class DossierROInline(ReadOnlyInlineMixin
, LinkedInline
):
90 template
= "admin/rh/dossier/linked.html"
91 exclude
= AUF_METADATA_READONLY_FIELDS
96 def has_add_permission(self
, request
=None):
99 def has_change_permission(self
, request
, obj
=None):
102 def has_delete_permission(self
, request
, obj
=None):
106 class DossierCommentaireInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
107 readonly_fields
= ('owner', )
108 model
= rh
.DossierCommentaire
112 class DossierPieceInline(admin
.TabularInline
):
113 model
= rh
.DossierPiece
117 class EmployeInline(admin
.TabularInline
):
120 class EmployeCommentaireInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
121 readonly_fields
= ('owner', )
122 model
= rh
.EmployeCommentaire
126 class EmployePieceInline(admin
.TabularInline
):
127 model
= rh
.EmployePiece
131 class PosteCommentaireInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
132 readonly_fields
= ('owner', )
133 model
= rh
.PosteCommentaire
137 class PosteFinancementInline(admin
.TabularInline
):
138 model
= rh
.PosteFinancement
141 class PostePieceInline(admin
.TabularInline
):
142 model
= rh
.PostePiece
145 class RemunerationInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
146 model
= rh
.Remuneration
150 class RemunerationROInline(ReadOnlyInlineMixin
, RemunerationInline
):
154 class TypePosteInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
158 class PosteComparaisonInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
159 model
= rh
.PosteComparaison
161 class AyantDroitAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
):
163 L'ajout d'un nouvel ayantdroit se fait dans l'admin de l'employé.
165 alphabet_filter
= 'nom'
166 search_fields
= ('nom', 'prenom', 'employe__nom', 'employe__prenom', )
167 list_display
= ('_employe', 'lien_parente', '_ayantdroit', )
168 inlines
= (AyantDroitCommentaireInline
,)
169 readonly_fields
= AUFMetadataAdminMixin
.readonly_fields
+ ('employe',)
170 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
171 ("Lien avec l'employé", {
172 'fields': (('employe', 'lien_parente'), )
176 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
180 def save_formset(self
, request
, form
, formset
, change
):
181 instances
= formset
.save(commit
=False)
182 for instance
in instances
:
183 if instance
.__class__
== rh
.AyantDroitCommentaire
:
184 instance
.owner
= request
.user
187 def _ayantdroit(self
, obj
):
189 _ayantdroit
.short_description
= u
'Ayant droit'
191 def _employe(self
, obj
):
192 return unicode(obj
.employe
)
193 _employe
.short_description
= u
'Employé'
195 def has_add_permission(self
, request
):
198 class AyantDroitCommentaireAdmin(admin
.ModelAdmin
):
202 class ClassementAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
203 list_display
= ('_classement', 'date_modification', 'user_modification', )
204 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
206 'fields': ('type', 'echelon', 'degre', 'coefficient', )
210 def _classement(self
, obj
):
212 _classement
.short_description
= u
"Classement"
214 class CommentaireAdmin(admin
.ModelAdmin
):
218 class DeviseAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
219 list_display
= ('code', 'nom', 'date_modification', 'user_modification', 'actif', )
220 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
222 'fields': ('code', 'nom', ),
227 class DossierAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
, AjaxSelect
):
228 alphabet_filter
= 'employe__nom'
229 search_fields
= ('employe__nom', 'employe__prenom', 'poste__nom', 'poste__nom_feminin')
242 'poste__implantation__region',
243 'poste__implantation',
245 'poste__type_poste__famille_emploi',
246 'rh_contrats__type_contrat',
249 inlines
= (DossierPieceInline
, ContratInline
,
251 DossierCommentaireInline
,
253 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
255 'fields': ('employe', 'poste', 'statut', 'organisme_bstg',)
258 'fields': ('statut_residence', 'remplacement', 'remplacement_de', )
261 'fields': ('classement', ('regime_travail', 'regime_travail_nb_heure_semaine'),)
263 ('Occupation du Poste par cet Employe', {
264 'fields': (('date_debut', 'date_fin'), )
267 form
= make_ajax_form(rh
.Dossier
, {
268 'employe' : 'employes',
270 'remplacement_de' : 'dossiers',
273 def lookup_allowed(self
, key
, value
):
275 'employe__nom__istartswith',
277 'poste__implantation__region__id__exact',
278 'poste__implantation__id__exact',
279 'poste__type_poste__id__exact',
280 'poste__type_poste__famille_emploi__id__exact',
281 'rh_contrats__type_contrat__id__exact',
285 def _apercu(self
, d
):
286 link
= u
"""<a title="Aperçu du dossier" onclick="return showAddAnotherPopup(this);" href='%s'><img src="%simg/loupe.png" /></a>""" % \
287 (reverse('dossier_apercu', args
=(d
.id,)),
291 _apercu
.allow_tags
= True
292 _apercu
.short_description
= u
''
293 _apercu
.admin_order_field
= ''
296 link
= u
"""<a href="%s" title="Modifier le dossier"><strong>%s</strong></a>""" % \
297 (reverse('admin:rh_dossier_change', args
=(d
.id,)),
301 _id
.allow_tags
= True
302 _id
.short_description
= u
'#'
303 _id
.admin_order_field
= 'id'
306 def _actif(self
, dossier
):
307 if dossier
.employe
.actif
:
308 html
= """<img alt="True" src="%simg/admin/icon-yes.gif">"""
310 html
= """<img alt="False" src="%simg/admin/icon-no.gif">"""
311 return html
% settings
.ADMIN_MEDIA_PREFIX
312 _actif
.allow_tags
= True
313 _actif
.short_description
= u
'Employé actif'
314 _actif
.admin_order_field
= 'employe__actif'
316 def _date_debut(self
, obj
):
317 return date(obj
.date_debut
)
319 _date_debut
.short_description
= u
'Occupation début'
320 _date_debut
.admin_order_field
= 'date_debut'
322 def _date_fin(self
, obj
):
323 return date(obj
.date_fin
)
324 _date_fin
.short_description
= u
'Occupation fin'
325 _date_fin
.admin_order_field
= 'date_fin'
327 def _poste(self
, dossier
):
328 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>""" % \
329 (reverse('poste_apercu', args
=(dossier
.poste
.id,)),
331 reverse('admin:rh_poste_change', args
=(dossier
.poste
.id,)),
335 _poste
.allow_tags
= True
336 _poste
.short_description
= u
'Poste'
337 _poste
.admin_order_field
= 'poste__nom'
339 def _employe(self
, obj
):
340 employe
= obj
.employe
341 view_link
= reverse('employe_apercu', args
=(employe
.id,))
342 edit_link
= reverse('admin:rh_employe_change', args
=(employe
.id,))
344 if employe
.actif
== False:
345 style
= "color: grey";
349 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
,)
350 return u
"""%s<a href='%s' style="%s;">[%s] %s %s</a>""" % \
351 (view
, edit_link
, style
, employe
.id, employe
.nom
.upper(), employe
.prenom
.title())
352 _employe
.allow_tags
= True
353 _employe
.short_description
= u
"Employé ([code] NOM Prénom)"
354 _employe
.admin_order_field
= "employe__nom"
356 def save_formset(self
, request
, form
, formset
, change
):
357 instances
= formset
.save(commit
=False)
358 for instance
in instances
:
359 if instance
.__class__
== rh
.DossierCommentaire
:
360 instance
.owner
= request
.user
364 class DossierPieceAdmin(admin
.ModelAdmin
):
368 class DossierCommentaireAdmin(admin
.ModelAdmin
):
372 class EmployeAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
):
373 alphabet_filter
= 'nom'
374 DEFAULT_ALPHABET
= u
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
375 search_fields
= ('id', 'nom', 'prenom', 'nom_affichage', )
377 form
= EmployeAdminForm
378 list_display
= ('_apercu', '_nom', '_dossiers', 'date_modification', 'user_modification', 'actif',)
379 list_filter
= ('rh_dossiers__poste__implantation__region', 'rh_dossiers__poste__implantation', 'nb_postes', 'actif', )
380 inlines
= (AyantDroitInline
,
383 EmployeCommentaireInline
)
384 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
386 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
388 ('Informations personnelles', {
389 'fields': ('situation_famille', 'date_entree', )
392 'fields': (('tel_domicile', 'tel_cellulaire'), ('adresse', 'ville'), ('code_postal', 'province'), 'pays', )
396 def _apercu(self
, obj
):
397 return u
"""<a title="Aperçu de l'employé" onclick="return showAddAnotherPopup(this);" href='%s'><img src="%simg/loupe.png" /></a>""" % \
398 (reverse('employe_apercu', args
=(obj
.id,)), settings
.MEDIA_URL
)
399 _apercu
.allow_tags
= True
400 _apercu
.short_description
= u
""
401 _apercu
.admin_order_field
= ""
404 edit_link
= reverse('admin:rh_employe_change', args
=(obj
.id,))
405 return u
"""<a href='%s'><strong>[%s] %s %s</strong></a>""" % \
406 (edit_link
, obj
.id, obj
.nom
.upper(), obj
.prenom
.title(),)
407 _nom
.allow_tags
= True
408 _nom
.short_description
= u
"Employé ([code] NOM Prénom)"
409 _nom
.admin_order_field
= "nom"
411 def _dossiers(self
, obj
):
413 for d
in obj
.rh_dossiers
.all().order_by('-date_debut'):
415 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>""" % \
416 (reverse('dossier_apercu', args
=(d
.id,)), settings
.MEDIA_URL
,)
418 if d
.date_fin
is not None:
420 style
= u
"color: grey";
421 link
= u
"""<li>%s<a style="%s;" href='%s'>%s : %s</a></li>""" % \
424 reverse('admin:rh_dossier_change', args
=(d
.id,)),
429 return "<ul>%s</ul>" % "\n".join(l
)
430 _dossiers
.allow_tags
= True
431 _dossiers
.short_description
= u
"Dossiers"
433 def queryset(self
, request
):
434 qs
= super(EmployeAdmin
, self
).queryset(request
)
435 return qs
.select_related(depth
=1).order_by('nom')
437 def save_formset(self
, request
, form
, formset
, change
):
438 instances
= formset
.save(commit
=False)
439 for instance
in instances
:
440 if instance
.__class__
== rh
.EmployeCommentaire
:
441 instance
.owner
= request
.user
446 class EmployeCommentaireAdmin(admin
.ModelAdmin
):
450 class EmployePieceAdmin(admin
.ModelAdmin
):
454 class FamilleEmploiAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
455 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
456 inlines
= (TypePosteInline
,)
457 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
464 class OrganismeBstgAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
):
465 search_fields
= ('nom',)
466 list_display
= ('nom', 'type', 'pays', 'date_modification', 'user_modification', 'actif', )
467 list_filter
= ('type', )
468 inlines
= (DossierROInline
,)
469 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
471 'fields': ('nom', 'type', 'pays', )
476 class PosteAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
, AjaxSelect
):
477 form
= make_ajax_form(rh
.Poste
, {
478 'implantation' : 'implantations',
479 'type_poste' : 'typepostes',
480 'responsable' : 'postes',
481 'valeur_point_min' : 'valeurpoints',
482 'valeur_point_max' : 'valeurpoints',
484 alphabet_filter
= 'nom'
485 search_fields
= ('nom',
486 'implantation__code',
488 'implantation__region__code',
489 'implantation__region__nom',
503 list_filter
= ('service',
504 'implantation__region',
507 'type_poste__famille_emploi',
511 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
513 'fields': (('nom', 'nom_feminin'), 'implantation', 'type_poste',
514 'service', 'responsable')
517 'fields': (('regime_travail', 'regime_travail_nb_heure_semaine'), )
520 'fields': (('local', 'expatrie', 'mise_a_disposition', 'appel'),)
523 'fields': (('classement_min', 'valeur_point_min', 'devise_min', 'salaire_min', 'indemn_min', 'autre_min', ),
524 ('classement_max', 'valeur_point_max' ,'devise_max', 'salaire_max', 'indemn_max', 'autre_max', ),
527 ('Comparatifs de rémunération', {
528 'fields': ('devise_comparaison',
529 ('comp_locale_min', 'comp_locale_max'),
530 ('comp_universite_min', 'comp_universite_max'),
531 ('comp_fonctionpub_min', 'comp_fonctionpub_max'),
532 ('comp_ong_min', 'comp_ong_max'),
533 ('comp_autre_min', 'comp_autre_max'))
536 'fields': ('justification',)
538 ('Autres Méta-données', {
539 'fields': ('date_debut', 'date_fin')
543 inlines
= (PosteFinancementInline
,
546 PosteComparaisonInline
,
547 PosteCommentaireInline
, )
550 def _apercu(self
, poste
):
551 link
= u
"""<a onclick="return showAddAnotherPopup(this);" title="Aperçu du poste" href='%s'><img src="%simg/loupe.png" /></a>""" % \
552 (reverse('poste_apercu', args
=(poste
.id,)),
556 _apercu
.allow_tags
= True
557 _apercu
.short_description
= u
''
558 _apercu
.admin_order_field
= ''
560 def _service(self
, obj
):
563 def _nom(self
, poste
):
564 link
= u
"""<a href="%s" title="Modifier le poste"><strong>%s</strong></a>""" % \
565 (reverse('admin:rh_poste_change', args
=(poste
.id,)),
569 _nom
.allow_tags
= True
570 _nom
.short_description
= u
'Nom'
571 _nom
.admin_order_field
= 'nom'
573 def _occupe_par(self
, obj
):
574 """Formatte la méthode Poste.occupe_par() pour l'admin"""
576 if obj
.actif
is False:
578 employes
= obj
.occupe_par()
582 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>" % \
583 (reverse('employe_apercu', args
=(e
.id,)),
585 reverse('admin:rh_employe_change', args
=(e
.id,)),
589 output
= "\n<br />".join(l
)
591 _occupe_par
.allow_tags
= True
592 _occupe_par
.short_description
= "Occupé par"
594 def save_formset(self
, request
, form
, formset
, change
):
595 instances
= formset
.save(commit
=False)
596 for instance
in instances
:
597 if instance
.__class__
== rh
.PosteCommentaire
:
598 instance
.owner
= request
.user
603 class PosteCommentaireAdmin(admin
.ModelAdmin
):
607 class PosteFinancementAdmin(admin
.ModelAdmin
):
611 class PostePieceAdmin(admin
.ModelAdmin
):
615 class RemunerationAdmin(admin
.ModelAdmin
):
619 class ResponsableImplantationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
620 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
622 'fields': ('employe', 'implantation', ),
627 class ServiceAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
628 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
629 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
635 class StatutAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
636 list_display
= ('code', 'nom', 'date_modification', 'user_modification', 'actif', )
637 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
639 'fields': ('code', 'nom', ),
643 class TauxChangeAdmin(admin
.ModelAdmin
):
644 list_display
= ('taux', 'devise', 'annee', 'date_modification', 'user_modification', )
645 list_filter
= ('devise', )
646 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
648 'fields': ('taux', 'devise', 'annee', ),
652 class TypeContratAdmin(admin
.ModelAdmin
):
653 list_display
= ('nom', 'nom_long', 'date_modification', 'user_modification', 'actif', )
654 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
656 'fields': ('nom', 'nom_long', ),
661 class TypePosteAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
662 search_fields
= ('nom', 'nom_feminin', )
663 list_display
= ('nom', 'famille_emploi', 'date_modification', 'user_modification', 'actif', )
664 list_filter
= ('famille_emploi', )
665 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
667 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
672 class TypeRemunerationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
673 list_display
= ('nom', 'type_paiement', 'nature_remuneration', 'date_modification', 'user_modification', 'actif', )
674 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
676 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
681 class TypeRevalorisationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
682 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
683 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
690 class ValeurPointAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
691 list_display
= ('_devise_code', '_devise_nom', 'annee', 'valeur', 'date_modification', 'user_modification', )
692 list_filter
= ('annee', 'devise', )
693 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
695 'fields': ('valeur', 'devise', 'implantation', 'annee', )
699 def _devise_code(self
, obj
):
700 return obj
.devise
.code
701 _devise_code
.short_description
= "Code de la devise"
703 def _devise_nom(self
, obj
):
704 return obj
.devise
.nom
705 _devise_nom
.short_description
= "Nom de la devise"
708 admin
.site
.register(rh
.Classement
, ClassementAdmin
)
709 admin
.site
.register(rh
.Devise
, DeviseAdmin
)
710 admin
.site
.register(rh
.Dossier
, DossierAdmin
)
711 admin
.site
.register(rh
.Employe
, EmployeAdmin
)
712 admin
.site
.register(rh
.FamilleEmploi
, FamilleEmploiAdmin
)
713 admin
.site
.register(rh
.OrganismeBstg
, OrganismeBstgAdmin
)
714 admin
.site
.register(rh
.Poste
, PosteAdmin
)
715 admin
.site
.register(rh
.ResponsableImplantation
, ResponsableImplantationAdmin
)
716 admin
.site
.register(rh
.Service
, ServiceAdmin
)
717 admin
.site
.register(rh
.Statut
, StatutAdmin
)
718 admin
.site
.register(rh
.TauxChange
, TauxChangeAdmin
)
719 admin
.site
.register(rh
.TypeContrat
, TypeContratAdmin
)
720 admin
.site
.register(rh
.TypePoste
, TypePosteAdmin
)
721 admin
.site
.register(rh
.TypeRemuneration
, TypeRemunerationAdmin
)
722 admin
.site
.register(rh
.TypeRevalorisation
, TypeRevalorisationAdmin
)
723 admin
.site
.register(rh
.ValeurPoint
, ValeurPointAdmin
)