272fecbb170b0e39ca04b9b941042e2682e03adb
[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 AjaxSelect(object):
15
16 class Media:
17 css = {
18 'all': (
19 'jquery-autocomplete/jquery.autocomplete.css',
20 'css/select.css',
21 )
22 }
23 js = (
24 'js/jquery-1.5.1.min.js',
25 'jquery-autocomplete/jquery.autocomplete.js',
26 )
27
28
29 class FormDate(object):
30
31 def clean_date_fin(self):
32 date_fin = self.cleaned_data['date_fin']
33 if date_fin is None:
34 return date_fin
35 date_debut = self.cleaned_data['date_debut']
36 if date_fin < date_debut:
37 raise forms.ValidationError(
38 u"La date de fin est antérieure à la date de début"
39 )
40 return date_fin
41
42
43 class DossierForm(forms.ModelForm, FormDate):
44
45 class Model:
46 model = rh.Dossier
47
48
49 class ContratForm(forms.ModelForm, FormDate):
50
51 class Model:
52 model = rh.Contrat
53
54
55 class AyantDroitForm(forms.ModelForm, AjaxSelect):
56
57 # ne fonctionne pas dans un inline
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:
64 model = rh.AyantDroit
65
66
67 class EmployeAdminForm(forms.ModelForm, AjaxSelect):
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 )
74
75 class Meta:
76 model = rh.Employe
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
83 class ResponsableInlineForm(forms.ModelForm):
84 employes_actifs = rh.Employe.objects.actifs()
85 employe = forms.ModelChoiceField(queryset=employes_actifs)
86
87 class Meta:
88 model = rh.ResponsableImplantation
89
90
91 class 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 )
113 if is_user_dans_region(user):
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 )