#2783
[auf_rh_dae.git] / project / rh / forms.py
CommitLineData
84cbb4c5
OL
1# -*- encoding: utf-8 -*-
2
3from django import forms
105dd778 4from ajax_select.fields import AutoCompleteSelectField
ce8f351f 5from models import Dossier, Contrat, AyantDroit, Employe
105dd778
OL
6
7
8class AjaxSelect(object):
9
10 class Media:
11 css = {
12 'all': ('jquery-autocomplete/jquery.autocomplete.css', 'css/select.css', )
13 }
14 js = ('js/jquery-1.5.1.min.js', 'jquery-autocomplete/jquery.autocomplete.js', )
84cbb4c5
OL
15
16
17class FormDate(object):
18
19 def clean_date_fin(self):
20 date_fin = self.cleaned_data['date_fin']
21 if date_fin is None:
22 return date_fin
23 date_debut = self.cleaned_data['date_debut']
24 if date_fin < date_debut:
3f5f3898 25 raise forms.ValidationError(u"La date de fin est antérieure à la date de début")
84cbb4c5
OL
26 return date_fin
27
105dd778
OL
28class DossierForm(forms.ModelForm, FormDate):
29
84cbb4c5
OL
30 class Model:
31 model = Dossier
32
6bee05ff
OL
33 def clean_principal(self):
34 employe = self.cleaned_data['employe']
35 principal = self.cleaned_data['principal']
36 dossiers_encours = employe.dossiers_encours()
37 dossiers_principaux = [dossier for dossier in dossiers_encours if \
38 dossier.principal and dossier != self.instance]
39 if principal and len(dossiers_principaux) > 0:
40 raise forms.ValidationError(u"Il y a déjà un dossier principal \
41 actuellement en vigueur %s" % dossiers_principaux)
42 if not principal and len(dossiers_principaux) == 0:
43 raise forms.ValidationError(u"Il y n'a aucun dossier principal actuellement en vigueur")
44 return principal
45
46
84cbb4c5
OL
47class ContratForm(forms.ModelForm, FormDate):
48
49 class Model:
50 model = Contrat
cf022e27 51
105dd778
OL
52class AyantDroitForm(forms.ModelForm, AjaxSelect):
53
ed4fbe26
OL
54 # ne fonctionne pas dans un inline
55 #nationalite = AutoCompleteSelectField('pays', help_text="Taper le nom ou le code du pays", required=False)
cf022e27
OL
56
57 def __init__(self, *args, **kwargs):
58 super(AyantDroitForm, self).__init__(*args, **kwargs)
59 self.fields['date_naissance'].widget = forms.widgets.DateInput()
60
61 class Meta:
62 model = AyantDroit
63
64
105dd778
OL
65class EmployeAdminForm(forms.ModelForm, AjaxSelect):
66
ed4fbe26
OL
67 nationalite = AutoCompleteSelectField('pays', help_text="Taper le nom ou le code du pays", required=False)
68 pays = AutoCompleteSelectField('pays', help_text="Taper le nom ou le code du pays", required=False)
105dd778
OL
69
70 class Meta:
71 model = Employe
72
73 def __init__(self, *args, **kwargs):
74 super(EmployeAdminForm, self).__init__(*args, **kwargs)
75 self.fields['date_naissance'].widget = forms.widgets.DateInput()
76
77