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