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 PosteComparaisonInline(AUFMetadataInlineAdminMixin
, admin
.TabularInline
):
158 model
= rh
.PosteComparaison
160 class AyantDroitAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
):
162 L'ajout d'un nouvel ayantdroit se fait dans l'admin de l'employé.
164 alphabet_filter
= 'nom'
165 search_fields
= ('nom', 'prenom', 'employe__nom', 'employe__prenom', )
166 list_display
= ('_employe', 'lien_parente', '_ayantdroit', )
167 inlines
= (AyantDroitCommentaireInline
,)
168 readonly_fields
= AUFMetadataAdminMixin
.readonly_fields
+ ('employe',)
169 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
170 ("Lien avec l'employé", {
171 'fields': (('employe', 'lien_parente'), )
175 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
179 def save_formset(self
, request
, form
, formset
, change
):
180 instances
= formset
.save(commit
=False)
181 for instance
in instances
:
182 if instance
.__class__
== rh
.AyantDroitCommentaire
:
183 instance
.owner
= request
.user
186 def _ayantdroit(self
, obj
):
188 _ayantdroit
.short_description
= u
'Ayant droit'
190 def _employe(self
, obj
):
191 return unicode(obj
.employe
)
192 _employe
.short_description
= u
'Employé'
194 def has_add_permission(self
, request
):
197 class AyantDroitCommentaireAdmin(admin
.ModelAdmin
):
201 class ClassementAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
202 list_display
= ('_classement', 'date_modification', 'user_modification', )
203 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
205 'fields': ('type', 'echelon', 'degre', 'coefficient', )
209 def _classement(self
, obj
):
211 _classement
.short_description
= u
"Classement"
213 class CommentaireAdmin(admin
.ModelAdmin
):
217 class DeviseAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
218 list_display
= ('code', 'nom', 'date_modification', 'user_modification', 'actif', )
219 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
221 'fields': ('code', 'nom', ),
226 class DossierAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
, AjaxSelect
):
227 alphabet_filter
= 'employe__nom'
228 search_fields
= ('employe__nom', 'employe__prenom', 'poste__nom', 'poste__nom_feminin')
241 'poste__implantation__region',
242 'poste__implantation',
244 'poste__type_poste__famille_emploi',
245 'rh_contrats__type_contrat',
248 inlines
= (DossierPieceInline
, ContratInline
,
250 DossierCommentaireInline
,
252 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
254 'fields': ('employe', 'poste', 'statut', 'organisme_bstg',)
257 'fields': ('statut_residence', 'remplacement', 'remplacement_de', )
260 'fields': ('classement', ('regime_travail', 'regime_travail_nb_heure_semaine'),)
262 ('Occupation du Poste par cet Employe', {
263 'fields': (('date_debut', 'date_fin'), )
266 form
= make_ajax_form(rh
.Dossier
, {
267 'employe' : 'employes',
269 'remplacement_de' : 'dossiers',
272 def lookup_allowed(self
, key
, value
):
274 'employe__nom__istartswith',
276 'poste__implantation__region__id__exact',
277 'poste__implantation__id__exact',
278 'poste__type_poste__id__exact',
279 'poste__type_poste__famille_emploi__id__exact',
280 'rh_contrats__type_contrat__id__exact',
284 def _apercu(self
, d
):
285 link
= u
"""<a title="Aperçu du dossier" onclick="return showAddAnotherPopup(this);" href='%s'><img src="%simg/loupe.png" /></a>""" % \
286 (reverse('dossier_apercu', args
=(d
.id,)),
290 _apercu
.allow_tags
= True
291 _apercu
.short_description
= u
''
292 _apercu
.admin_order_field
= ''
295 link
= u
"""<a href="%s" title="Modifier le dossier"><strong>%s</strong></a>""" % \
296 (reverse('admin:rh_dossier_change', args
=(d
.id,)),
300 _id
.allow_tags
= True
301 _id
.short_description
= u
'#'
302 _id
.admin_order_field
= 'id'
305 def _actif(self
, dossier
):
306 if dossier
.employe
.actif
:
307 html
= """<img alt="True" src="%simg/admin/icon-yes.gif">"""
309 html
= """<img alt="False" src="%simg/admin/icon-no.gif">"""
310 return html
% settings
.ADMIN_MEDIA_PREFIX
311 _actif
.allow_tags
= True
312 _actif
.short_description
= u
'Employé actif'
313 _actif
.admin_order_field
= 'employe__actif'
315 def _date_debut(self
, obj
):
316 return obj
.date_debut
317 _date_debut
.short_description
= u
'Occupation début'
318 _date_debut
.admin_order_field
= 'date_debut'
320 def _date_fin(self
, obj
):
322 _date_fin
.short_description
= u
'Occupation fin'
323 _date_fin
.admin_order_field
= 'date_fin'
325 def _poste(self
, dossier
):
326 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>""" % \
327 (reverse('poste_apercu', args
=(dossier
.poste
.id,)),
329 reverse('admin:rh_poste_change', args
=(dossier
.poste
.id,)),
333 _poste
.allow_tags
= True
334 _poste
.short_description
= u
'Poste'
335 _poste
.admin_order_field
= 'poste__nom'
337 def _employe(self
, obj
):
338 employe
= obj
.employe
339 view_link
= reverse('employe_apercu', args
=(employe
.id,))
340 edit_link
= reverse('admin:rh_employe_change', args
=(employe
.id,))
342 if employe
.actif
== False:
343 style
= "color: grey";
347 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
,)
348 return u
"""%s<a href='%s' style="%s;">[%s] %s %s</a>""" % \
349 (view
, edit_link
, style
, employe
.id, employe
.nom
.upper(), employe
.prenom
.title())
350 _employe
.allow_tags
= True
351 _employe
.short_description
= u
"Employé ([code] NOM Prénom)"
352 _employe
.admin_order_field
= "employe__nom"
354 def save_formset(self
, request
, form
, formset
, change
):
355 instances
= formset
.save(commit
=False)
356 for instance
in instances
:
357 if instance
.__class__
== rh
.DossierCommentaire
:
358 instance
.owner
= request
.user
362 class DossierPieceAdmin(admin
.ModelAdmin
):
366 class DossierCommentaireAdmin(admin
.ModelAdmin
):
370 class EmployeAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
):
371 alphabet_filter
= 'nom'
372 DEFAULT_ALPHABET
= u
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
373 search_fields
= ('id', 'nom', 'prenom', 'nom_affichage', )
375 form
= EmployeAdminForm
376 list_display
= ('_apercu', '_nom', '_dossiers', 'date_modification', 'user_modification', 'actif',)
377 list_filter
= ('rh_dossiers__poste__implantation__region', 'rh_dossiers__poste__implantation', 'nb_postes', 'actif', )
378 inlines
= (AyantDroitInline
,
381 EmployeCommentaireInline
)
382 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
384 'fields': (('nom', 'prenom'), ('nom_affichage', 'genre'), 'nationalite', 'date_naissance', )
386 ('Informations personnelles', {
387 'fields': ('situation_famille', 'date_entree', )
390 'fields': (('tel_domicile', 'tel_cellulaire'), ('adresse', 'ville'), ('code_postal', 'province'), 'pays', )
394 def _apercu(self
, obj
):
395 return u
"""<a title="Aperçu de l'employé" onclick="return showAddAnotherPopup(this);" href='%s'><img src="%simg/loupe.png" /></a>""" % \
396 (reverse('employe_apercu', args
=(obj
.id,)), settings
.MEDIA_URL
)
397 _apercu
.allow_tags
= True
398 _apercu
.short_description
= u
""
399 _apercu
.admin_order_field
= ""
402 edit_link
= reverse('admin:rh_employe_change', args
=(obj
.id,))
403 return u
"""<a href='%s'><strong>[%s] %s %s</strong></a>""" % \
404 (edit_link
, obj
.id, obj
.nom
.upper(), obj
.prenom
.title(),)
405 _nom
.allow_tags
= True
406 _nom
.short_description
= u
"Employé ([code] NOM Prénom)"
407 _nom
.admin_order_field
= "nom"
409 def _dossiers(self
, obj
):
411 for d
in obj
.rh_dossiers
.all().order_by('-date_debut'):
413 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>""" % \
414 (reverse('dossier_apercu', args
=(d
.id,)), settings
.MEDIA_URL
,)
416 if d
.date_fin
is not None:
418 style
= u
"color: grey";
419 link
= u
"""<li>%s<a style="%s;" href='%s'>%s : %s</a></li>""" % \
422 reverse('admin:rh_dossier_change', args
=(d
.id,)),
427 return "<ul>%s</ul>" % "\n".join(l
)
428 _dossiers
.allow_tags
= True
429 _dossiers
.short_description
= u
"Dossiers"
431 def queryset(self
, request
):
432 qs
= super(EmployeAdmin
, self
).queryset(request
)
433 return qs
.select_related(depth
=1).order_by('nom')
435 def save_formset(self
, request
, form
, formset
, change
):
436 instances
= formset
.save(commit
=False)
437 for instance
in instances
:
438 if instance
.__class__
== rh
.EmployeCommentaire
:
439 instance
.owner
= request
.user
444 class EmployeCommentaireAdmin(admin
.ModelAdmin
):
448 class EmployePieceAdmin(admin
.ModelAdmin
):
452 class FamilleEmploiAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
453 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
454 inlines
= (TypePosteInline
,)
455 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
462 class OrganismeBstgAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
):
463 search_fields
= ('nom',)
464 list_display
= ('nom', 'type', 'pays', 'date_modification', 'user_modification', 'actif', )
465 list_filter
= ('type', )
466 inlines
= (DossierROInline
,)
467 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
469 'fields': ('nom', 'type', 'pays', )
474 class PosteAdmin(AUFMetadataAdminMixin
, ProtectRegionMixin
, admin
.ModelAdmin
, AjaxSelect
):
475 form
= make_ajax_form(rh
.Poste
, {
476 'implantation' : 'implantations',
477 'type_poste' : 'typepostes',
478 'responsable' : 'postes',
479 'valeur_point_min' : 'valeurpoints',
480 'valeur_point_max' : 'valeurpoints',
482 alphabet_filter
= 'nom'
483 search_fields
= ('nom',
484 'implantation__code',
486 'implantation__region__code',
487 'implantation__region__nom',
501 list_filter
= ('service',
502 'implantation__region',
505 'type_poste__famille_emploi',
509 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
511 'fields': (('nom', 'nom_feminin'), 'implantation', 'type_poste',
512 'service', 'responsable')
515 'fields': (('regime_travail', 'regime_travail_nb_heure_semaine'), )
518 'fields': (('local', 'expatrie', 'mise_a_disposition', 'appel'),)
521 'fields': (('classement_min', 'valeur_point_min', 'devise_min', 'salaire_min', 'indemn_min', 'autre_min', ),
522 ('classement_max', 'valeur_point_max' ,'devise_max', 'salaire_max', 'indemn_max', 'autre_max', ),
525 ('Comparatifs de rémunération', {
526 'fields': ('devise_comparaison',
527 ('comp_locale_min', 'comp_locale_max'),
528 ('comp_universite_min', 'comp_universite_max'),
529 ('comp_fonctionpub_min', 'comp_fonctionpub_max'),
530 ('comp_ong_min', 'comp_ong_max'),
531 ('comp_autre_min', 'comp_autre_max'))
534 'fields': ('justification',)
536 ('Autres Méta-données', {
537 'fields': ('date_debut', 'date_fin')
541 inlines
= (PosteFinancementInline
,
544 PosteComparaisonInline
,
545 PosteCommentaireInline
, )
548 def _apercu(self
, poste
):
549 link
= u
"""<a onclick="return showAddAnotherPopup(this);" title="Aperçu du poste" href='%s'><img src="%simg/loupe.png" /></a>""" % \
550 (reverse('poste_apercu', args
=(poste
.id,)),
554 _apercu
.allow_tags
= True
555 _apercu
.short_description
= u
''
556 _apercu
.admin_order_field
= ''
558 def _service(self
, obj
):
561 def _nom(self
, poste
):
562 link
= u
"""<a href="%s" title="Modifier le poste"><strong>%s</strong></a>""" % \
563 (reverse('admin:rh_poste_change', args
=(poste
.id,)),
567 _nom
.allow_tags
= True
568 _nom
.short_description
= u
'Nom'
569 _nom
.admin_order_field
= 'nom'
571 def _occupe_par(self
, obj
):
572 """Formatte la méthode Poste.occupe_par() pour l'admin"""
574 if obj
.actif
is False:
576 employes
= obj
.occupe_par()
580 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>" % \
581 (reverse('employe_apercu', args
=(e
.id,)),
583 reverse('admin:rh_employe_change', args
=(e
.id,)),
587 output
= "\n<br />".join(l
)
589 _occupe_par
.allow_tags
= True
590 _occupe_par
.short_description
= "Occupé par"
592 def save_formset(self
, request
, form
, formset
, change
):
593 instances
= formset
.save(commit
=False)
594 for instance
in instances
:
595 if instance
.__class__
== rh
.PosteCommentaire
:
596 instance
.owner
= request
.user
601 class PosteCommentaireAdmin(admin
.ModelAdmin
):
605 class PosteFinancementAdmin(admin
.ModelAdmin
):
609 class PostePieceAdmin(admin
.ModelAdmin
):
613 class RemunerationAdmin(admin
.ModelAdmin
):
617 class ResponsableImplantationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
618 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
620 'fields': ('employe', 'implantation', ),
625 class ServiceAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
626 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
627 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
633 class StatutAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
634 list_display
= ('code', 'nom', 'date_modification', 'user_modification', 'actif', )
635 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
637 'fields': ('code', 'nom', ),
641 class TauxChangeAdmin(admin
.ModelAdmin
):
642 list_display
= ('taux', 'devise', 'annee', 'date_modification', 'user_modification', )
643 list_filter
= ('devise', )
644 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
646 'fields': ('taux', 'devise', 'annee', ),
650 class TypeContratAdmin(admin
.ModelAdmin
):
651 list_display
= ('nom', 'nom_long', 'date_modification', 'user_modification', 'actif', )
652 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
654 'fields': ('nom', 'nom_long', ),
659 class TypePosteAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
660 search_fields
= ('nom', 'nom_feminin', )
661 list_display
= ('nom', 'famille_emploi', 'date_modification', 'user_modification', 'actif', )
662 list_filter
= ('famille_emploi', )
663 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
665 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
670 class TypeRemunerationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
671 list_display
= ('nom', 'type_paiement', 'nature_remuneration', 'date_modification', 'user_modification', 'actif', )
672 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
674 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
679 class TypeRevalorisationAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
680 list_display
= ('nom', 'date_modification', 'user_modification', 'actif', )
681 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
688 class ValeurPointAdmin(AUFMetadataAdminMixin
, admin
.ModelAdmin
):
689 list_display
= ('_devise_code', '_devise_nom', 'annee', 'valeur', 'date_modification', 'user_modification', )
690 list_filter
= ('annee', 'devise', )
691 fieldsets
= AUFMetadataAdminMixin
.fieldsets
+ (
693 'fields': ('valeur', 'devise', 'implantation', 'annee', )
697 def _devise_code(self
, obj
):
698 return obj
.devise
.code
699 _devise_code
.short_description
= "Code de la devise"
701 def _devise_nom(self
, obj
):
702 return obj
.devise
.nom
703 _devise_nom
.short_description
= "Nom de la devise"
706 admin
.site
.register(rh
.Classement
, ClassementAdmin
)
707 admin
.site
.register(rh
.Devise
, DeviseAdmin
)
708 admin
.site
.register(rh
.Dossier
, DossierAdmin
)
709 admin
.site
.register(rh
.Employe
, EmployeAdmin
)
710 admin
.site
.register(rh
.FamilleEmploi
, FamilleEmploiAdmin
)
711 admin
.site
.register(rh
.OrganismeBstg
, OrganismeBstgAdmin
)
712 admin
.site
.register(rh
.Poste
, PosteAdmin
)
713 admin
.site
.register(rh
.ResponsableImplantation
, ResponsableImplantationAdmin
)
714 admin
.site
.register(rh
.Service
, ServiceAdmin
)
715 admin
.site
.register(rh
.Statut
, StatutAdmin
)
716 admin
.site
.register(rh
.TauxChange
, TauxChangeAdmin
)
717 admin
.site
.register(rh
.TypeContrat
, TypeContratAdmin
)
718 admin
.site
.register(rh
.TypePoste
, TypePosteAdmin
)
719 admin
.site
.register(rh
.TypeRemuneration
, TypeRemunerationAdmin
)
720 admin
.site
.register(rh
.TypeRevalorisation
, TypeRevalorisationAdmin
)
721 admin
.site
.register(rh
.ValeurPoint
, ValeurPointAdmin
)