Installation de la version modifié de QBE dans src/
[auf_rh_dae.git] / project / rh / lib.py
CommitLineData
6e7c919b
NC
1# -*- encoding: utf-8 -*-
2
babf71ec 3from django.db import models
6e7c919b 4from django.contrib import admin
49449367 5from django.conf import settings
aff1a4c6 6from django.db.models import Q
d6985a3a 7from auf.django.metadata.admin import AUFMetadataAdminMixin, AUFMetadataInlineAdminMixin, AUF_METADATA_READONLY_FIELDS
babf71ec 8from project.rh import models as rh
84cbb4c5 9from forms import DossierForm, ContratForm
aff1a4c6
PP
10from dae.utils import get_employe_from_user
11
12
13class ProtectRegionMixin(object):
14
15 def queryset(self, request):
16 qs = super(ProtectRegionMixin, self).queryset(request)
17
18 if request.user.is_superuser:
19 return qs
20
21 employe = get_employe_from_user(request.user)
22
23 q = Q(**{self.model.prefix_implantation: employe.implantation.region})
24 qs = qs.filter(q).distinct()
25 return qs
26
27 def has_change_permission(self, request, obj=None):
28 if request.user.is_superuser:
29 return True
30
31 if obj:
32 employe = get_employe_from_user(request.user)
33 if employe.implantation.region in obj.get_regions():
34 return True
35 else:
36 return False
37
38 return True
39
6e7c919b 40
d6985a3a 41# Inlines
6e7c919b 42
d6985a3a
OL
43class ReadOnlyInlineMixin(object):
44 def get_readonly_fields(self, request, obj=None):
45 return [f.name for f in self.model._meta.fields if f.name not in AUF_METADATA_READONLY_FIELDS]
6e7c919b
NC
46
47
51ab4c2c 48class AyantDroitInline(AUFMetadataInlineAdminMixin, admin.StackedInline):
babf71ec 49 model = models.Model # à remplacer dans admin.py
51ab4c2c 50 extra = 0
54d04eed 51
d6985a3a
OL
52
53class AyantDroitCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
cf786fb2 54 readonly_fields = ('owner', )
babf71ec 55 model = models.Model # à remplacer dans admin.py
cf786fb2 56 extra = 1
6e7c919b 57
54d04eed 58
d6985a3a 59class ContratInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
84cbb4c5 60 form = ContratForm
babf71ec 61 model = models.Model # à remplacer dans admin.py
1f2979b8 62 extra = 1
babf71ec
NC
63
64
d6985a3a
OL
65class DossierROInline(ReadOnlyInlineMixin, admin.TabularInline):
66 exclude = AUF_METADATA_READONLY_FIELDS
babf71ec 67 model = models.Model # à remplacer dans admin.py
babf71ec 68
d6985a3a
OL
69
70class DossierCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
1f2979b8 71 readonly_fields = ('owner', )
babf71ec 72 model = models.Model # à remplacer dans admin.py
1f2979b8 73 extra = 1
babf71ec 74
d6985a3a 75
babf71ec
NC
76class DossierPieceInline(admin.TabularInline):
77 model = models.Model # à remplacer dans admin.py
988af3fb 78 extra = 4
babf71ec
NC
79
80
81class EmployeInline(admin.TabularInline):
82 model = models.Model # à remplacer dans admin.py
83
d6985a3a 84class EmployeCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
cf786fb2 85 readonly_fields = ('owner', )
babf71ec 86 model = models.Model # à remplacer dans admin.py
cf786fb2 87 extra = 1
babf71ec
NC
88
89
90class EmployePieceInline(admin.TabularInline):
91 model = models.Model # à remplacer dans admin.py
988af3fb 92 extra = 4
babf71ec
NC
93
94
d6985a3a 95class EvenementInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
babf71ec 96 model = models.Model # à remplacer dans admin.py
1f2979b8 97 extra = 1
babf71ec
NC
98
99
d6985a3a 100class EvenementRemunerationInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
babf71ec 101 model = models.Model # à remplacer dans admin.py
1f2979b8 102 extra = 1
babf71ec
NC
103
104
d6985a3a 105class PosteCommentaireInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
70561dc2 106 readonly_fields = ('owner', )
babf71ec 107 model = models.Model # à remplacer dans admin.py
70561dc2 108 extra = 1
babf71ec
NC
109
110
111class PosteFinancementInline(admin.TabularInline):
112 model = models.Model # à remplacer dans admin.py
113
114
115class PostePieceInline(admin.TabularInline):
116 model = models.Model # à remplacer dans admin.py
117
118
d6985a3a 119class RemunerationInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
babf71ec 120 model = models.Model # à remplacer dans admin.py
1f2979b8 121 extra = 1
babf71ec
NC
122
123
d6985a3a 124class RemunerationROInline(ReadOnlyInlineMixin, RemunerationInline):
6e7c919b
NC
125 pass
126
54d04eed 127
26dacccc 128class TypePosteInline(AUFMetadataInlineAdminMixin, admin.TabularInline):
babf71ec
NC
129 model = models.Model # à remplacer dans admin.py
130
131
132# Admins
133
aff1a4c6 134class AyantDroitAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
cf786fb2
OL
135 """
136 L'ajout d'un nouvel ayantdroit se fait dans l'admin de l'employé.
137 """
387ab827 138 alphabet_filter = 'nom'
cf786fb2 139 search_fields = ('nom', 'prenom', 'employe__nom', 'employe__prenom', )
1a89b1f3 140 list_display = ('_employe', 'lien_parente', '_ayantdroit', )
babf71ec 141 inlines = (AyantDroitCommentaireInline,)
d6985a3a
OL
142 readonly_fields = AUFMetadataAdminMixin.readonly_fields + ('employe',)
143 fieldsets = AUFMetadataAdminMixin.fieldsets + (
cf786fb2
OL
144 ("Lien avec l'employé", {
145 'fields': ('employe', 'lien_parente', )
146 }),
147
148 ('Identification', {
149 'fields': ('nom', 'prenom', 'nom_affichage', 'nationalite', 'date_naissance', 'genre', )
150 }),
151 )
152
153 def save_formset(self, request, form, formset, change):
154 instances = formset.save(commit=False)
155 for instance in instances:
156 if instance.__class__ == rh.AyantDroitCommentaire:
157 instance.owner = request.user
158 instance.save()
cf786fb2
OL
159
160 def _ayantdroit(self, obj):
161 return unicode(obj)
162 _ayantdroit.short_description = u'Ayant droit'
babf71ec 163
cf786fb2
OL
164 def _employe(self, obj):
165 return unicode(obj.employe)
166 _employe.short_description = u'Employé'
167
168 def has_add_permission(self, request):
169 return False
babf71ec
NC
170
171class AyantDroitCommentaireAdmin(admin.ModelAdmin):
6e7c919b
NC
172 pass
173
54d04eed 174
324bf312
OL
175class ClassementAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
176 fieldsets = AUFMetadataAdminMixin.fieldsets + (
177 (None, {
178 'fields': ('type', 'echelon', 'degre', 'coefficient', )
179 }),
180 )
181
6e7c919b 182
6e7c919b
NC
183class CommentaireAdmin(admin.ModelAdmin):
184 pass
185
186
84cbb4c5
OL
187#class ContratAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
188# form = ContratForm
189# alphabet_filter = 'dossier__employe__nom'
190# search_fields = ('dossier__employe__nom', 'dossier__employe__prenom', 'dossier__poste__nom', 'dossier__poste__nom_feminin', )
191# list_display = ('id', '_employe', '_poste', 'date_debut', 'date_fin', '_implantation', )
192# fieldsets = AUFMetadataAdminMixin.fieldsets + (
193# (None, {
194# 'fields': ('dossier', 'type_contrat', 'date_debut', 'date_fin', )
195# }),
196# )
197#
198# def lookup_allowed(self, key, value):
199# if key in ('dossier__employe__nom__istartswith', ):
200# return True
201#
202# def _employe(self, obj):
203# return unicode(obj.dossier.employe)
204# _employe.short_description = "Employé"
205#
206# def _poste(self, obj):
207# return obj.dossier.poste.nom
208# _poste.short_description = "Poste"
209#
210# def _implantation(self, obj):
211# return obj.dossier.poste.implantation
212# _poste.short_description = "Implantation"
54d04eed 213
84fc088b
OL
214class DeviseAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
215 fieldsets = AUFMetadataAdminMixin.fieldsets + (
216 (None, {
217 'fields': ('code', 'nom', ),
218 }),
219 )
6e7c919b 220
54d04eed 221
aff1a4c6 222class DossierAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin,):
84cbb4c5 223 form = DossierForm
387ab827 224 alphabet_filter = 'employe__nom'
d8d7985b 225 search_fields = ('employe__nom', 'employe__prenom', 'poste__nom', 'poste__nom_feminin')
49449367 226 list_display = ('_employe', '_poste', 'date_debut', 'date_fin', '_actif')
1f2979b8 227 inlines = (DossierPieceInline, ContratInline,
cf786fb2
OL
228 RemunerationInline,
229 #EvenementInline,
230 DossierCommentaireInline,
1f2979b8 231 )
d6985a3a 232 fieldsets = AUFMetadataAdminMixin.fieldsets + (
1f2979b8
OL
233 ('Identification', {
234 'fields': ('employe', 'poste', 'statut', 'organisme_bstg',)
235 }),
236 ('Recrutement', {
237 'fields': ('remplacement', 'statut_residence', )
238 }),
239 ('Rémunération', {
240 'fields': ('classement', 'regime_travail', 'regime_travail_nb_heure_semaine',)
241 }),
242 ('Occupation du Poste par cet Employe', {
243 'fields': ('date_debut', 'date_fin', )
244 }),
245 )
54d04eed 246
387ab827 247 def lookup_allowed(self, key, value):
49449367 248 if key in ('employe__nom__istartswith', 'actif__exact', ):
387ab827
OL
249 return True
250
49449367
OL
251 def _actif(self, dossier):
252 if dossier.employe.actif:
253 html = """<img alt="True" src="%simg/admin/icon-yes.gif">"""
254 else:
255 html = """<img alt="False" src="%simg/admin/icon-no.gif">"""
256 return html % settings.ADMIN_MEDIA_PREFIX
257 _actif.allow_tags = u'Employé actif'
258 _actif.short_description = u'Employé actif'
259 _actif.admin_order_field = 'employe__actif'
387ab827 260
54d04eed
NC
261 def _poste(self, dossier):
262 return unicode(dossier.poste.nom)
263 _poste.short_description = u'Poste'
d9836879 264 _poste.admin_order_field = 'poste__nom'
54d04eed
NC
265
266 def _employe(self, dossier):
267 return unicode(dossier.employe)
268 _employe.short_description = u'Employé'
d9836879
OL
269 _employe.admin_order_field = 'employe__nom'
270
1f2979b8
OL
271 def save_formset(self, request, form, formset, change):
272 instances = formset.save(commit=False)
273 for instance in instances:
274 if instance.__class__ == rh.DossierCommentaire:
275 instance.owner = request.user
276 instance.save()
6e7c919b
NC
277
278class DossierPieceAdmin(admin.ModelAdmin):
279 pass
280
54d04eed 281
6e7c919b
NC
282class DossierCommentaireAdmin(admin.ModelAdmin):
283 pass
284
54d04eed 285
aff1a4c6 286class EmployeAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
7ffc5aa4 287 alphabet_filter = 'nom'
49449367
OL
288 search_fields = ('id', 'nom', 'prenom', 'nom_affichage', 'actif', )
289 list_filter = ('actif', )
290 ordering = ('nom', )
291 actions = ('desactiver', )
292 list_display = ('nom', 'prenom', 'actif', )
babf71ec
NC
293 inlines = (AyantDroitInline,
294 DossierROInline,
295 EmployePieceInline,
296 EmployeCommentaireInline)
d6985a3a 297 fieldsets = AUFMetadataAdminMixin.fieldsets + (
cf786fb2
OL
298 ('Identification', {
299 'fields': ('nom', 'prenom', 'nom_affichage', 'nationalite', 'date_naissance', 'genre', )
300 }),
301 ('Informations personnelles', {
302 'fields': ('situation_famille', 'date_entree', )
303 }),
304 ('Coordonnées', {
305 'fields': ('tel_domicile', 'tel_cellulaire', 'adresse', 'ville', 'province', 'code_postal', 'pays', )
306 }),
307 )
babf71ec 308
cf786fb2
OL
309 def save_formset(self, request, form, formset, change):
310 instances = formset.save(commit=False)
311 for instance in instances:
312 if instance.__class__ == rh.EmployeCommentaire:
313 instance.owner = request.user
314 instance.save()
babf71ec
NC
315
316class EmployeCommentaireAdmin(admin.ModelAdmin):
6e7c919b
NC
317 pass
318
54d04eed 319
babf71ec 320class EmployePieceAdmin(admin.ModelAdmin):
6e7c919b
NC
321 pass
322
54d04eed 323
6e7c919b 324class EvenementAdmin(admin.ModelAdmin):
babf71ec 325 inlines = (EvenementRemunerationInline,)
6e7c919b 326
54d04eed 327
6e7c919b
NC
328class EvenementRemunerationAdmin(admin.ModelAdmin):
329 pass
330
54d04eed 331
26dacccc 332class FamilleEmploiAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
babf71ec 333 inlines = (TypePosteInline,)
26dacccc
OL
334 fieldsets = AUFMetadataAdminMixin.fieldsets + (
335 (None, {
336 'fields': ('nom', )
337 }),
338 )
6e7c919b 339
54d04eed 340
aff1a4c6 341class OrganismeBstgAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
d8d7985b 342 search_fields = ('nom', )
1c8856ef 343 list_display = ('nom', 'type', 'pays', )
babf71ec 344 inlines = (DossierROInline,)
43f5653a
OL
345 fieldsets = AUFMetadataAdminMixin.fieldsets + (
346 (None, {
347 'fields': ('nom', 'type', 'pays', )
348 }),
349 )
6e7c919b 350
54d04eed 351
aff1a4c6 352class PosteAdmin(AUFMetadataAdminMixin, ProtectRegionMixin, admin.ModelAdmin):
387ab827 353 alphabet_filter = 'nom'
70561dc2
OL
354 search_fields = ('nom', 'implantation__code', 'implantation__nom', 'implantation__region__code', 'implantation__region__nom', )
355 list_display = ('nom', 'implantation', 'service', 'type_poste', 'date_debut', 'date_fin', )
d2cf315a 356 fieldsets = AUFMetadataAdminMixin.fieldsets + (
6e7c919b
NC
357 (None, {
358 'fields': ('nom', 'nom_feminin', 'implantation', 'type_poste',
359 'service', 'responsable')
360 }),
361 ('Contrat', {
362 'fields': ('regime_travail', 'regime_travail_nb_heure_semaine')
363 }),
364 ('Recrutement', {
365 'fields': ('local', 'expatrie', 'mise_a_disposition', 'appel')
366 }),
367 ('Rémunération', {
368 'fields': ('classement_min', 'classement_max', 'valeur_point_min',
369 'valeur_point_max', 'devise_min', 'devise_max',
370 'salaire_min', 'salaire_max', 'indemn_min',
371 'indemn_max', 'autre_min', 'autre_max')
372 }),
373 ('Comparatifs de rémunération', {
374 'fields': ('devise_comparaison', 'comp_locale_min',
375 'comp_locale_max', 'comp_universite_min',
376 'comp_universite_max', 'comp_fonctionpub_min',
377 'comp_fonctionpub_max', 'comp_ong_min', 'comp_ong_max',
378 'comp_autre_min', 'comp_autre_max')
379 }),
380 ('Justification', {
381 'fields': ('justification',)
382 }),
383 ('Autres Metadata', {
384 'fields': ('date_validation', 'date_debut', 'date_fin')
385 }),
386 )
387
babf71ec
NC
388 inlines = (PosteFinancementInline,
389 PostePieceInline,
70561dc2
OL
390 DossierROInline,
391 PosteCommentaireInline, )
6e7c919b 392
70561dc2
OL
393 def save_formset(self, request, form, formset, change):
394 instances = formset.save(commit=False)
395 for instance in instances:
396 if instance.__class__ == rh.PosteCommentaire:
397 instance.owner = request.user
398 instance.save()
399 formset.save_m2m()
6e7c919b 400
aff1a4c6 401
6e7c919b
NC
402class PosteCommentaireAdmin(admin.ModelAdmin):
403 pass
404
6e7c919b 405
babf71ec 406class PosteFinancementAdmin(admin.ModelAdmin):
6e7c919b
NC
407 pass
408
6e7c919b 409
babf71ec 410class PostePieceAdmin(admin.ModelAdmin):
6e7c919b
NC
411 pass
412
6e7c919b 413
babf71ec 414class RemunerationAdmin(admin.ModelAdmin):
6e7c919b
NC
415 pass
416
6e7c919b 417
1eede0db
OL
418class ResponsableImplantationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
419 fieldsets = AUFMetadataAdminMixin.fieldsets + (
420 (None, {
421 'fields': ('employe', 'implantation', ),
422 }),
423 )
424
6e7c919b 425
43f5653a
OL
426class ServiceAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
427 list_display = ('nom', 'actif', )
428 fieldsets = AUFMetadataAdminMixin.fieldsets + (
429 (None, {
430 'fields': ('nom', ),
431 }),
432 )
6e7c919b 433
43f5653a
OL
434class StatutAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
435 fieldsets = AUFMetadataAdminMixin.fieldsets + (
436 (None, {
437 'fields': ('code', 'nom', ),
438 }),
439 )
6e7c919b 440
babf71ec 441class TauxChangeAdmin(admin.ModelAdmin):
24475cf1
OL
442 list_display = ('taux', 'devise', 'annee', )
443 list_filter = ('devise', )
dcbadfad
OL
444 fieldsets = AUFMetadataAdminMixin.fieldsets + (
445 (None, {
446 'fields': ('taux', 'devise', 'annee', ),
447 }),
448 )
6e7c919b 449
6e7c919b 450class TypeContratAdmin(admin.ModelAdmin):
babf71ec 451 inlines = (ContratInline,)
6e7c919b 452
6e7c919b 453
240893cb 454class TypePosteAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
d8d7985b 455 search_fields = ('nom', 'nom_feminin', )
52e05b08
OL
456 list_display = ('nom', 'famille_emploi', )
457 list_filter = ('famille_emploi', )
240893cb
OL
458 fieldsets = AUFMetadataAdminMixin.fieldsets + (
459 (None, {
460 'fields': ('nom', 'nom_feminin', 'is_responsable', 'famille_emploi', )
461 }),
462 )
6e7c919b 463
6e7c919b 464
3f486b41 465class TypeRemunerationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
f7efd29a 466 list_display = ('nom', 'type_paiement', 'nature_remuneration', )
3f486b41
OL
467 #inlines = (RemunerationROInline,) utilité?
468 fieldsets = AUFMetadataAdminMixin.fieldsets + (
469 (None, {
470 'fields': ('nom', 'type_paiement', 'nature_remuneration', )
471 }),
472 )
6e7c919b 473
6e7c919b 474
3f486b41
OL
475class TypeRevalorisationAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
476 #inlines = (RemunerationROInline,) utilité?
477 fieldsets = AUFMetadataAdminMixin.fieldsets + (
478 (None, {
479 'fields': ('nom', )
480 }),
481 )
6e7c919b 482
6e7c919b 483
84fc088b 484class ValeurPointAdmin(AUFMetadataAdminMixin, admin.ModelAdmin):
701f3bea 485 list_display = ('_devise_code', '_devise_nom', 'annee', 'valeur', )
84fc088b
OL
486 fieldsets = AUFMetadataAdminMixin.fieldsets + (
487 (None, {
488 'fields': ('valeur', 'devise', 'implantation', 'annee', )
489 }),
490 )
701f3bea
OL
491
492 def _devise_code(self, obj):
493 return obj.devise.code
494 _devise_code.short_description = "Code de la devise"
495
496 def _devise_nom(self, obj):
497 return obj.devise.nom
498 _devise_nom.short_description = "Nom de la devise"