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'>%s</a> <a href="%s" title="Modifier le poste"><img src="%simg/page_edit.png" /></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 edit
= u
"""<a href="%s" title="Modifier l'employé"><img src="%simg/user_edit.png" /></a>""" % (edit_link
, settings
.MEDIA_URL
,)
345 return u
"""<a onclick="return showAddAnotherPopup(this);" href='%s' style="%s;">[%s] %s %s</a>%s
347 (view_link
, style
, employe
.id, employe
.nom
.upper(), employe
.prenom
.title(), edit
)
348 _employe
.allow_tags
= True
349 _employe
.short_description
= u
"Employé ([code] NOM Prénom)"
350 _employe
.admin_order_field
= "employe__nom"
352 def save_formset(self
, request
, form
, formset
, change
):
353 instances
= formset
.save(commit
=False)
354 for instance
in instances
:
355 if instance
.__class__
== rh
.DossierCommentaire
:
356 instance
.owner
= request
.user
360 class DossierPieceAdmin(admin
.ModelAdmin
):
364 class DossierCommentaireAdmin(admin
.ModelAdmin
):
368 class EmployeAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
):
369 alphabet_filter
= 'nom'
370 DEFAULT_ALPHABET
= u
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
371 search_fields
= ('id', 'nom', 'prenom', 'nom_affichage', )
373 form
= EmployeAdminForm
374 list_display
= ('_apercu', '_nom', '_dossiers', 'date_modification', 'user_modification', 'actif',)
375 list_filter
= ('rh_dossiers__poste__implantation__region', 'rh_dossiers__poste__implantation', 'actif', )
376 inlines
= (AyantDroitInline
,
379 EmployeCommentaireInline
)
380 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
382 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
384 ('Informations personnelles', {
385 'fields': ('situation_famille', 'date_entree', )
388 'fields': (('tel_domicile', 'tel_cellulaire'), ('adresse', 'ville'), ('code_postal', 'province'), 'pays', )
392 def _apercu(self
, obj
):
393 return u
"""<a title="Aperçu de l'employé" onclick="return showAddAnotherPopup(this);" href='%s'><img src="%simg/loupe.png" /></a>""" % \
394 (reverse('employe_apercu', args
=(obj
.id,)), settings
.MEDIA_URL
)
395 _apercu
.allow_tags
= True
396 _apercu
.short_description
= u
""
397 _apercu
.admin_order_field
= ""
400 edit_link
= reverse('admin:rh_employe_change', args
=(obj
.id,))
401 return u
"""<a href='%s'><strong>[%s] %s %s</strong></a>""" % \
402 (edit_link
, obj
.id, obj
.nom
.upper(), obj
.prenom
.title(),)
403 _nom
.allow_tags
= True
404 _nom
.short_description
= u
"Employé ([code] NOM Prénom)"
405 _nom
.admin_order_field
= "nom"
407 def _dossiers(self
, obj
):
409 for d
in obj
.rh_dossiers
.all().order_by('-date_debut'):
411 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>""" % \
412 (reverse('dossier_apercu', args
=(d
.id,)), settings
.MEDIA_URL
,)
414 if d
.date_fin
is not None:
416 style
= u
"color: grey";
417 link
= u
"""<li>%s<a style="%s;" href='%s'>%s : %s</a></li>""" % \
420 reverse('admin:rh_dossier_change', args
=(d
.id,)),
425 return "<ul>%s</ul>" % "\n".join(l
)
426 _dossiers
.allow_tags
= True
427 _dossiers
.short_description
= u
"Dossiers"
429 def queryset(self
, request
):
430 qs
= super(EmployeAdmin
, self
).queryset(request
)
431 return qs
.select_related(depth
=1).order_by('nom')
433 def save_formset(self
, request
, form
, formset
, change
):
434 instances
= formset
.save(commit
=False)
435 for instance
in instances
:
436 if instance
.__class__
== rh
.EmployeCommentaire
:
437 instance
.owner
= request
.user
442 class EmployeCommentaireAdmin(admin
.ModelAdmin
):
446 class EmployePieceAdmin(admin
.ModelAdmin
):
450 class FamilleEmploiAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
451 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
452 inlines
= (TypePosteInline
,)
453 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
460 class OrganismeBstgAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
):
461 search_fields
= ('nom',)
462 list_display
= ('nom', 'type', 'pays', 'date_modification', 'user_modification', 'actif', )
463 list_filter
= ('type', )
464 inlines
= (DossierROInline
,)
465 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
467 'fields': ('nom', 'type', 'pays', )
472 class PosteAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
, AjaxSelect
):
473 form
= make_ajax_form(rh
.Poste
, {
474 'implantation' : 'implantations',
475 'type_poste' : 'typepostes',
476 'responsable' : 'postes',
477 'valeur_point_min' : 'valeurpoints',
478 'valeur_point_max' : 'valeurpoints',
480 alphabet_filter
= 'nom'
481 search_fields
= ('nom',
482 'implantation__code',
484 'implantation__region__code',
485 'implantation__region__nom',
499 list_filter
= ('service',
500 'implantation__region',
503 'type_poste__famille_emploi',
507 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
509 'fields': (('nom', 'nom_feminin'), 'implantation', 'type_poste',
510 'service', 'responsable')
513 'fields': (('regime_travail', 'regime_travail_nb_heure_semaine'), )
516 'fields': (('local', 'expatrie', 'mise_a_disposition', 'appel'),)
519 'fields': (('classement_min', 'valeur_point_min', 'devise_min', 'salaire_min', 'indemn_min', 'autre_min', ),
520 ('classement_max', 'valeur_point_max' ,'devise_max', 'salaire_max', 'indemn_max', 'autre_max', ),
523 ('Comparatifs de rémunération', {
524 'fields': ('devise_comparaison',
525 ('comp_locale_min', 'comp_locale_max'),
526 ('comp_universite_min', 'comp_universite_max'),
527 ('comp_fonctionpub_min', 'comp_fonctionpub_max'),
528 ('comp_ong_min', 'comp_ong_max'),
529 ('comp_autre_min', 'comp_autre_max'))
532 'fields': ('justification',)
534 ('Autres Méta-données', {
535 'fields': ('date_debut', 'date_fin')
539 inlines
= (PosteFinancementInline
,
542 PosteCommentaireInline
, )
545 def _apercu(self
, poste
):
546 link
= u
"""<a onclick="return showAddAnotherPopup(this);" title="Aperçu du poste" href='%s'><img src="%simg/loupe.png" /></a>""" % \
547 (reverse('poste_apercu', args
=(poste
.id,)),
551 _apercu
.allow_tags
= True
552 _apercu
.short_description
= u
''
553 _apercu
.admin_order_field
= ''
555 def _service(self
, obj
):
558 def _nom(self
, poste
):
559 link
= u
"""<a href="%s" title="Modifier le poste"><strong>%s</strong></a>""" % \
560 (reverse('admin:rh_poste_change', args
=(poste
.id,)),
564 _nom
.allow_tags
= True
565 _nom
.short_description
= u
'Nom'
566 _nom
.admin_order_field
= 'nom'
568 def _occupe_par(self
, obj
):
569 """Formatte la méthode Poste.occupe_par() pour l'admin"""
571 if obj
.actif
is False:
573 employes
= obj
.occupe_par()
577 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>" % \
578 (reverse('employe_apercu', args
=(e
.id,)),
580 reverse('admin:rh_employe_change', args
=(e
.id,)),
584 output
= "\n<br />".join(l
)
586 _occupe_par
.allow_tags
= True
587 _occupe_par
.short_description
= "Occupé par"
589 def save_formset(self
, request
, form
, formset
, change
):
590 instances
= formset
.save(commit
=False)
591 for instance
in instances
:
592 if instance
.__class__
== rh
.PosteCommentaire
:
593 instance
.owner
= request
.user
598 class PosteCommentaireAdmin(admin
.ModelAdmin
):
602 class PosteFinancementAdmin(admin
.ModelAdmin
):
606 class PostePieceAdmin(admin
.ModelAdmin
):
610 class RemunerationAdmin(admin
.ModelAdmin
):
614 class ResponsableImplantationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
615 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
617 'fields': ('employe', 'implantation', ),
622 class ServiceAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
623 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
624 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
630 class StatutAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
631 list_display
= ('code', 'nom', 'date_modification', 'user_modification', 'actif', )
632 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
634 'fields': ('code', 'nom', ),
638 class TauxChangeAdmin(admin
.ModelAdmin
):
639 list_display
= ('taux', 'devise', 'annee', 'date_modification', 'user_modification', )
640 list_filter
= ('devise', )
641 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
643 'fields': ('taux', 'devise', 'annee', ),
647 class TypeContratAdmin(admin
.ModelAdmin
):
648 list_display
= ('nom', 'nom_long', 'date_modification', 'user_modification', 'actif', )
649 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
651 'fields': ('nom', 'nom_long', ),
656 class TypePosteAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
657 search_fields
= ('nom', 'nom_feminin', )
658 list_display
= ('nom', 'famille_emploi', 'date_modification', 'user_modification', 'actif', )
659 list_filter
= ('famille_emploi', )
660 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
662 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
667 class TypeRemunerationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
668 list_display
= ('nom', 'type_paiement', 'nature_remuneration', 'date_modification', 'user_modification', 'actif', )
669 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
671 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
676 class TypeRevalorisationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
677 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
678 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
685 class ValeurPointAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
686 list_display
= ('_devise_code', '_devise_nom', 'annee', 'valeur', 'date_modification', 'user_modification', )
687 list_filter
= ('annee', 'devise', )
688 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
690 'fields': ('valeur', 'devise', 'implantation', 'annee', )
694 def _devise_code(self
, obj
):
695 return obj
.devise
.code
696 _devise_code
.short_description
= "Code de la devise"
698 def _devise_nom(self
, obj
):
699 return obj
.devise
.nom
700 _devise_nom
.short_description
= "Nom de la devise"
703 admin
.site
.register(rh
.Classement
, ClassementAdmin
)
704 admin
.site
.register(rh
.Devise
, DeviseAdmin
)
705 admin
.site
.register(rh
.Dossier
, DossierAdmin
)
706 admin
.site
.register(rh
.Employe
, EmployeAdmin
)
707 admin
.site
.register(rh
.FamilleEmploi
, FamilleEmploiAdmin
)
708 admin
.site
.register(rh
.OrganismeBstg
, OrganismeBstgAdmin
)
709 admin
.site
.register(rh
.Poste
, PosteAdmin
)
710 admin
.site
.register(rh
.ResponsableImplantation
, ResponsableImplantationAdmin
)
711 admin
.site
.register(rh
.Service
, ServiceAdmin
)
712 admin
.site
.register(rh
.Statut
, StatutAdmin
)
713 admin
.site
.register(rh
.TauxChange
, TauxChangeAdmin
)
714 admin
.site
.register(rh
.TypeContrat
, TypeContratAdmin
)
715 admin
.site
.register(rh
.TypePoste
, TypePosteAdmin
)
716 admin
.site
.register(rh
.TypeRemuneration
, TypeRemunerationAdmin
)
717 admin
.site
.register(rh
.TypeRevalorisation
, TypeRevalorisationAdmin
)
718 admin
.site
.register(rh
.ValeurPoint
, ValeurPointAdmin
)