Routeur: Ajout temporaire des modules workflow et dae en écriture
[auf_rh_dae.git] / project / rh / forms.py
1 # -*- encoding: utf-8 -*-
2
3 from datetime import date
4
5 from ajax_select.fields import AutoCompleteSelectField
6 from auf.django.references import models as ref
7 from django import forms
8 from django.db.models import Min
9
10 from project.groups import get_employe_from_user, is_user_dans_region
11 from project.rh import models as rh
12
13
14 class ClassementHistoriqueForm(forms.ModelForm):
15
16 class Meta:
17 model = rh.RHDossierClassementRecord
18
19
20 class AjaxSelect(object):
21
22 class Media:
23 css = {
24 'all': (
25 'jquery-autocomplete/jquery.autocomplete.css',
26 'css/select.css',
27 )
28 }
29 js = (
30 'js/jquery-1.5.1.min.js',
31 'jquery-autocomplete/jquery.autocomplete.js',
32 )
33
34
35 class FormDate(object):
36
37 def clean_date_fin(self):
38 date_fin = self.cleaned_data['date_fin']
39 if date_fin is None:
40 return date_fin
41 date_debut = self.cleaned_data['date_debut']
42 if date_fin < date_debut:
43 raise forms.ValidationError(
44 u"La date de fin est antérieure à la date de début"
45 )
46 return date_fin
47
48
49 class DossierForm(forms.ModelForm, FormDate):
50
51 class Model:
52 model = rh.Dossier
53
54
55 class ContratForm(forms.ModelForm, FormDate):
56
57 class Model:
58 model = rh.Contrat
59
60
61 class AyantDroitForm(forms.ModelForm, AjaxSelect):
62
63 # ne fonctionne pas dans un inline
64
65 def __init__(self, *args, **kwargs):
66 super(AyantDroitForm, self).__init__(*args, **kwargs)
67 self.fields['date_naissance'].widget = forms.widgets.DateInput()
68
69 class Meta:
70 model = rh.AyantDroit
71
72
73 class EmployeAdminForm(forms.ModelForm, AjaxSelect):
74 nationalite = AutoCompleteSelectField(
75 'pays', help_text="Taper le nom ou le code du pays", required=False
76 )
77 pays = AutoCompleteSelectField(
78 'pays', help_text="Taper le nom ou le code du pays", required=False
79 )
80
81 class Meta:
82 model = rh.Employe
83
84 def __init__(self, *args, **kwargs):
85 super(EmployeAdminForm, self).__init__(*args, **kwargs)
86 self.fields['date_naissance'].widget = forms.widgets.DateInput()
87
88
89 class ResponsableInlineForm(forms.ModelForm):
90 employes_actifs = rh.Employe.objects.actifs().distinct()
91 employe = forms.ModelChoiceField(queryset=employes_actifs)
92
93 class Meta:
94 model = rh.ResponsableImplantation
95
96
97 class MasseSalarialeForm(forms.Form):
98 zone_administrative = forms.ModelChoiceField(
99 label=u'Région', queryset=ref.ZoneAdministrative.objects.all(),
100 required=False
101 )
102 implantation = forms.ModelChoiceField(
103 label=u'Implantation', queryset=ref.Implantation.objects.all(),
104 required=False
105 )
106
107 def __init__(self, user, *args, **kwargs):
108 super(MasseSalarialeForm, self).__init__(*args, **kwargs)
109 min_date = rh.Remuneration.objects \
110 .aggregate(Min('date_debut'))['date_debut__min']
111 years = range(
112 date.today().year,
113 min_date.year if min_date else 1997,
114 -1
115 )
116 self.fields['annee'] = forms.TypedChoiceField(
117 label='Année', choices=zip(years, years), coerce=int,
118 required=False
119 )
120 if is_user_dans_region(user):
121 employe = get_employe_from_user(user)
122 self.fields['zone_administrative'].queryset = \
123 ref.ZoneAdministrative.objects.filter(
124 code=employe.implantation.zone_administrative.code
125 )
126 self.fields['implantation'].queryset = \
127 ref.Implantation.objects.filter(
128 zone_administrative=(
129 employe.implantation.zone_administrative
130 )
131 )