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'):
414 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>""" % \
415 (reverse('dossier_apercu', args
=(d
.id,)), settings
.MEDIA_URL
,)
416 link
= u
"""<li>%s<a href='%s'>%s : %s</a></li>""" % \
418 reverse('admin:rh_dossier_change', args
=(d
.id,)),
423 # Dossier terminé en gris non cliquable
424 if d
.date_fin
is not None:
425 link
= u
"""<li style="color: grey">%s : %s</li>""" % \
431 return "<ul>%s</ul>" % "\n".join(l
)
432 _dossiers
.allow_tags
= True
433 _dossiers
.short_description
= u
"Dossiers"
435 def queryset(self
, request
):
436 qs
= super(EmployeAdmin
, self
).queryset(request
)
437 return qs
.select_related(depth
=1).order_by('nom')
439 def save_formset(self
, request
, form
, formset
, change
):
440 instances
= formset
.save(commit
=False)
441 for instance
in instances
:
442 if instance
.__class__
== rh
.EmployeCommentaire
:
443 instance
.owner
= request
.user
448 class EmployeCommentaireAdmin(admin
.ModelAdmin
):
452 class EmployePieceAdmin(admin
.ModelAdmin
):
456 class FamilleEmploiAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
457 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
458 inlines
= (TypePosteInline
,)
459 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
466 class OrganismeBstgAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
):
467 search_fields
= ('nom',)
468 list_display
= ('nom', 'type', 'pays', 'date_modification', 'user_modification', 'actif', )
469 list_filter
= ('type', )
470 inlines
= (DossierROInline
,)
471 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
473 'fields': ('nom', 'type', 'pays', )
478 class PosteAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
, AjaxSelect
):
479 form
= make_ajax_form(rh
.Poste
, {
480 'implantation' : 'implantations',
481 'type_poste' : 'typepostes',
482 'responsable' : 'postes',
483 'valeur_point_min' : 'valeurpoints',
484 'valeur_point_max' : 'valeurpoints',
486 alphabet_filter
= 'nom'
487 search_fields
= ('nom',
488 'implantation__code',
490 'implantation__region__code',
491 'implantation__region__nom',
505 list_filter
= ('service',
506 'implantation__region',
509 'type_poste__famille_emploi',
513 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
515 'fields': (('nom', 'nom_feminin'), 'implantation', 'type_poste',
516 'service', 'responsable')
519 'fields': (('regime_travail', 'regime_travail_nb_heure_semaine'), )
522 'fields': (('local', 'expatrie', 'mise_a_disposition', 'appel'),)
525 'fields': (('classement_min', 'valeur_point_min', 'devise_min', 'salaire_min', 'indemn_min', 'autre_min', ),
526 ('classement_max', 'valeur_point_max' ,'devise_max', 'salaire_max', 'indemn_max', 'autre_max', ),
529 ('Comparatifs de rémunération', {
530 'fields': ('devise_comparaison',
531 ('comp_locale_min', 'comp_locale_max'),
532 ('comp_universite_min', 'comp_universite_max'),
533 ('comp_fonctionpub_min', 'comp_fonctionpub_max'),
534 ('comp_ong_min', 'comp_ong_max'),
535 ('comp_autre_min', 'comp_autre_max'))
538 'fields': ('justification',)
540 ('Autres Méta-données', {
541 'fields': ('date_debut', 'date_fin')
545 inlines
= (PosteFinancementInline
,
548 PosteComparaisonInline
,
549 PosteCommentaireInline
, )
552 def _apercu(self
, poste
):
553 link
= u
"""<a onclick="return showAddAnotherPopup(this);" title="Aperçu du poste" href='%s'><img src="%simg/loupe.png" /></a>""" % \
554 (reverse('poste_apercu', args
=(poste
.id,)),
558 _apercu
.allow_tags
= True
559 _apercu
.short_description
= u
''
560 _apercu
.admin_order_field
= ''
562 def _service(self
, obj
):
565 def _nom(self
, poste
):
566 link
= u
"""<a href="%s" title="Modifier le poste"><strong>%s</strong></a>""" % \
567 (reverse('admin:rh_poste_change', args
=(poste
.id,)),
571 _nom
.allow_tags
= True
572 _nom
.short_description
= u
'Nom'
573 _nom
.admin_order_field
= 'nom'
575 def _occupe_par(self
, obj
):
576 """Formatte la méthode Poste.occupe_par() pour l'admin"""
578 if obj
.actif
is False:
580 employes
= obj
.occupe_par()
584 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>" % \
585 (reverse('employe_apercu', args
=(e
.id,)),
587 reverse('admin:rh_employe_change', args
=(e
.id,)),
591 output
= "\n<br />".join(l
)
593 _occupe_par
.allow_tags
= True
594 _occupe_par
.short_description
= "Occupé par"
596 def save_formset(self
, request
, form
, formset
, change
):
597 instances
= formset
.save(commit
=False)
598 for instance
in instances
:
599 if instance
.__class__
== rh
.PosteCommentaire
:
600 instance
.owner
= request
.user
605 class PosteCommentaireAdmin(admin
.ModelAdmin
):
609 class PosteFinancementAdmin(admin
.ModelAdmin
):
613 class PostePieceAdmin(admin
.ModelAdmin
):
617 class RemunerationAdmin(admin
.ModelAdmin
):
621 class ResponsableImplantationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
622 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
624 'fields': ('employe', 'implantation', ),
629 class ServiceAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
630 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
631 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
637 class StatutAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
638 list_display
= ('code', 'nom', 'date_modification', 'user_modification', 'actif', )
639 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
641 'fields': ('code', 'nom', ),
645 class TauxChangeAdmin(admin
.ModelAdmin
):
646 list_display
= ('taux', 'devise', 'annee', 'date_modification', 'user_modification', )
647 list_filter
= ('devise', )
648 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
650 'fields': ('taux', 'devise', 'annee', ),
654 class TypeContratAdmin(admin
.ModelAdmin
):
655 list_display
= ('nom', 'nom_long', 'date_modification', 'user_modification', 'actif', )
656 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
658 'fields': ('nom', 'nom_long', ),
663 class TypePosteAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
664 search_fields
= ('nom', 'nom_feminin', )
665 list_display
= ('nom', 'famille_emploi', 'date_modification', 'user_modification', 'actif', )
666 list_filter
= ('famille_emploi', )
667 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
669 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
674 class TypeRemunerationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
675 list_display
= ('nom', 'type_paiement', 'nature_remuneration', 'date_modification', 'user_modification', 'actif', )
676 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
678 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
683 class TypeRevalorisationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
684 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
685 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
692 class ValeurPointAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
693 list_display
= ('_devise_code', '_devise_nom', 'annee', 'valeur', 'date_modification', 'user_modification', )
694 list_filter
= ('annee', 'devise', )
695 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
697 'fields': ('valeur', 'devise', 'implantation', 'annee', )
701 def _devise_code(self
, obj
):
702 return obj
.devise
.code
703 _devise_code
.short_description
= "Code de la devise"
705 def _devise_nom(self
, obj
):
706 return obj
.devise
.nom
707 _devise_nom
.short_description
= "Nom de la devise"
710 admin
.site
.register(rh
.Classement
, ClassementAdmin
)
711 admin
.site
.register(rh
.Devise
, DeviseAdmin
)
712 admin
.site
.register(rh
.Dossier
, DossierAdmin
)
713 admin
.site
.register(rh
.Employe
, EmployeAdmin
)
714 admin
.site
.register(rh
.FamilleEmploi
, FamilleEmploiAdmin
)
715 admin
.site
.register(rh
.OrganismeBstg
, OrganismeBstgAdmin
)
716 admin
.site
.register(rh
.Poste
, PosteAdmin
)
717 admin
.site
.register(rh
.ResponsableImplantation
, ResponsableImplantationAdmin
)
718 admin
.site
.register(rh
.Service
, ServiceAdmin
)
719 admin
.site
.register(rh
.Statut
, StatutAdmin
)
720 admin
.site
.register(rh
.TauxChange
, TauxChangeAdmin
)
721 admin
.site
.register(rh
.TypeContrat
, TypeContratAdmin
)
722 admin
.site
.register(rh
.TypePoste
, TypePosteAdmin
)
723 admin
.site
.register(rh
.TypeRemuneration
, TypeRemunerationAdmin
)
724 admin
.site
.register(rh
.TypeRevalorisation
, TypeRevalorisationAdmin
)
725 admin
.site
.register(rh
.ValeurPoint
, ValeurPointAdmin
)