Commit | Line | Data |
---|---|---|
5d680e84 | 1 | # -*- encoding: utf-8 -*- |
ce110fb9 | 2 | |
5d680e84 | 3 | from django import forms |
36341125 | 4 | from django.forms.models import inlineformset_factory |
e88caaf0 | 5 | from django.contrib.admin import widgets as admin_widgets |
8fa94e8b | 6 | from auf.django.workflow.forms import WorkflowFormMixin |
5d680e84 NC |
7 | from datamaster_modeles import models as ref |
8 | ||
9 | from dae import models as dae | |
10 | from rh_v1 import models as rh | |
11 | ||
9cb4de55 OL |
12 | |
13 | class PostePieceForm(inlineformset_factory(dae.Poste, dae.PostePiece)): | |
14 | pass | |
15 | ||
16 | class DossierPieceForm(inlineformset_factory(dae.Dossier, dae.DossierPiece)): | |
17 | pass | |
18 | ||
151e7bd0 OL |
19 | class FinancementForm(inlineformset_factory(dae.Poste, dae.PosteFinancement, extra=1)): |
20 | pass | |
21 | ||
5d680e84 | 22 | |
a05cc82d OL |
23 | class PosteValidationForm(forms.ModelForm): |
24 | """ Validation d'un poste""" | |
25 | class Meta: | |
26 | model = dae.Poste | |
27 | fields = ( | |
28 | 'validation_bureau_regional', | |
29 | 'validation_drh', | |
30 | 'validation_secretaire_general', | |
31 | 'validation_recteur', | |
32 | ) | |
33 | ||
8fa94e8b | 34 | class PosteForm(WorkflowFormMixin): |
5d680e84 NC |
35 | """ Formulaire des postes. """ |
36 | class Meta: | |
37 | model = dae.Poste | |
ce110fb9 | 38 | |
5d680e84 NC |
39 | fields = ('poste', 'implantation', 'type_poste', 'service', 'nom', |
40 | 'responsable', 'statut_residence', 'mise_a_disposition', | |
41 | 'appel', 'date_debut', 'date_fin', 'actif', | |
42 | 'regime_travail', 'regime_travail_nb_heure_semaine', | |
43 | 'classement_min', 'classement_max', | |
96d32304 | 44 | 'coefficient_min', 'coefficient_max', |
5d680e84 | 45 | 'valeur_point_min', 'valeur_point_max', |
3d627bfd | 46 | 'devise_min', 'devise_max', |
5d680e84 NC |
47 | 'salaire_min', 'salaire_max', 'indemn_min', 'indemn_max', |
48 | 'autre_min', 'autre_max', 'devise_comparaison', | |
49 | 'comp_locale_min', 'comp_locale_max', | |
50 | 'comp_universite_min', 'comp_universite_max', | |
51 | 'comp_fonctionpub_min', 'comp_fonctionpub_max', | |
52 | 'comp_ong_min', 'comp_ong_max', | |
8fa94e8b | 53 | 'comp_autre_min', 'comp_autre_max', |
2e092e0c | 54 | 'justification', |
8fa94e8b OL |
55 | 'etat', |
56 | ) | |
5d680e84 | 57 | widgets = dict(statut_residence=forms.RadioSelect(), |
ce110fb9 | 58 | appel=forms.RadioSelect(), |
3d627bfd | 59 | nom=forms.TextInput(attrs={'size': 60},), |
e88caaf0 OL |
60 | date_debut=admin_widgets.AdminDateWidget(), |
61 | date_fin=admin_widgets.AdminDateWidget(), | |
2e092e0c | 62 | justification=forms.Textarea(attrs={'cols': 80},), |
3d627bfd | 63 | #devise_min=forms.Select(attrs={'disabled':'disabled'}), |
64 | #devise_max=forms.Select(attrs={'disabled':'disabled'}), | |
65 | ) | |
5d680e84 | 66 | |
98d51b59 | 67 | responsable = forms.ModelChoiceField( |
139686f2 NC |
68 | queryset=rh.Poste.objects.select_related(depth=1)) |
69 | ||
5d680e84 | 70 | # La liste des choix est laissée vide. Voir __init__ pour la raison. |
5efcd48e | 71 | poste = forms.ChoiceField(label="Nouveau poste ou évolution du poste", |
d949462d | 72 | choices=(), required=False) |
5d680e84 | 73 | |
4dd75e7b OL |
74 | valeur_point_min = forms.ModelChoiceField(queryset=rh.ValeurPoint.actuelles.all(), required=False) |
75 | valeur_point_max = forms.ModelChoiceField(queryset=rh.ValeurPoint.actuelles.all(), required=False) | |
6301bd59 | 76 | |
5d680e84 NC |
77 | def __init__(self, *args, **kwargs): |
78 | """ Mise à jour dynamique du contenu du menu des postes. | |
79 | ||
80 | Si on ne met le menu à jour de cette façon, à chaque instantiation du | |
81 | formulaire, son contenu est mis en cache par le système et il ne | |
82 | reflète pas les changements apportés par les ajouts, modifications, | |
83 | etc... | |
84 | ||
139686f2 NC |
85 | Aussi, dans ce cas-ci, on ne peut pas utiliser un ModelChoiceField |
86 | car le "id" de chaque choix est spécial (voir _poste_choices). | |
87 | ||
5d680e84 NC |
88 | """ |
89 | super(PosteForm, self).__init__(*args, **kwargs) | |
90 | self.fields['poste'].choices = self._poste_choices() | |
91 | ||
cc3098d0 OL |
92 | # Quand le dae.Poste n'existe pas, on recherche dans les dossiers rhv1 |
93 | if self.instance and self.instance.id is None: | |
94 | dossiers = self.instance.get_dossiers() | |
95 | if len(dossiers) > 0: | |
96 | self.initial['service'] = dossiers[0].service_id | |
9508a5b8 OL |
97 | self.initial['nom'] = "%s %s" % (self.initial['nom'], self.instance.get_complement_nom()) |
98 | ||
cc3098d0 | 99 | |
5d680e84 NC |
100 | def _poste_choices(self): |
101 | """ Menu déroulant pour les postes. | |
102 | ||
103 | Constitué des postes de dae et des postes de rh_v1 qui n'ont pas | |
104 | d'équivalent dans dae. | |
105 | ||
106 | """ | |
6d704629 | 107 | dae_ = dae.Poste.objects.filter(actif=True, id_rh__isnull=True) |
5d680e84 NC |
108 | copies = dae.Poste.objects.exclude(id_rh__isnull=True) |
109 | id_copies = [p.id_rh_id for p in copies.all()] | |
6d704629 | 110 | rhv1 = rh.Poste.objects.filter(actif=True).exclude(id__in=id_copies) |
139686f2 NC |
111 | # Optimisation de la requête |
112 | rhv1 = rhv1.select_related(depth=1) | |
5d680e84 | 113 | |
98d51b59 | 114 | return [('', 'Nouveau poste')] + \ |
5d680e84 NC |
115 | sorted([('dae-%s' % p.id, unicode(p)) for p in dae_ | copies] + |
116 | [('rh-%s' % p.id, unicode(p)) for p in rhv1], | |
117 | key=lambda t: t[1]) | |
3ed49093 | 118 | |
4dd75e7b OL |
119 | def clean(self): |
120 | """ | |
121 | Validation conditionnelles de certains champs. | |
122 | """ | |
123 | cleaned_data = self.cleaned_data | |
124 | ||
125 | # Gestion de la mise à disposition | |
126 | mise_a_disposition = cleaned_data.get("mise_a_disposition") | |
127 | valeur_point_min = cleaned_data.get("valeur_point_min") | |
128 | valeur_point_max = cleaned_data.get("valeur_point_max") | |
129 | if mise_a_disposition is False and (valeur_point_min is None or valeur_point_max is None): | |
130 | msg = u"Ce champ est obligatoire." | |
131 | self._errors["valeur_point_min"] = self.error_class([msg]) | |
132 | self._errors["valeur_point_max"] = self.error_class([msg]) | |
133 | raise forms.ValidationError("Les valeurs de point sont vides") | |
134 | ||
135 | return cleaned_data | |
136 | ||
137 | ||
138 | ||
494ff2be NC |
139 | def save(self, *args, **kwargs): |
140 | kwargs2 = kwargs.copy() | |
141 | kwargs2['commit'] = False | |
142 | poste = super(PosteForm, self).save(*args, **kwargs2) | |
143 | # id_rh | |
144 | if 'commit' not in kwargs or kwargs['commit']: | |
145 | poste.save() | |
146 | return poste | |
147 | ||
3ed49093 | 148 | |
139686f2 NC |
149 | class ChoosePosteForm(forms.ModelForm): |
150 | class Meta: | |
151 | model = dae.Poste | |
152 | fields = ('poste',) | |
153 | ||
154 | # La liste des choix est laissée vide. Voir PosteForm.__init__. | |
155 | poste = forms.ChoiceField(choices=(), required=False) | |
156 | ||
157 | def __init__(self, *args, **kwargs): | |
158 | super(ChoosePosteForm, self).__init__(*args, **kwargs) | |
159 | self.fields['poste'].choices = self._poste_choices() | |
160 | ||
161 | def _poste_choices(self): | |
162 | """ Menu déroulant pour les postes. """ | |
163 | dae_ = dae.Poste.objects.filter(id_rh__isnull=True) | |
164 | copies = dae.Poste.objects.exclude(id_rh__isnull=True) | |
165 | id_copies = [p.id_rh_id for p in copies.all()] | |
166 | ||
98d51b59 | 167 | return [('', '----------')] + \ |
139686f2 NC |
168 | sorted([('dae-%s' % p.id, unicode(p)) for p in dae_ | copies], |
169 | key=lambda t: t[1]) | |
170 | ||
171 | ||
139686f2 NC |
172 | class EmployeForm(forms.ModelForm): |
173 | """ Formulaire des employés. """ | |
174 | class Meta: | |
175 | model = dae.Employe | |
176 | fields = ('employe', 'nom', 'prenom', 'genre') | |
177 | ||
178 | # La liste des choix est laissée vide. Voir Poste.__init__ pour la raison. | |
179 | employe = forms.ChoiceField(choices=(), required=False) | |
180 | ||
181 | def __init__(self, *args, **kwargs): | |
182 | """ Mise à jour dynamique du contenu du menu des employés. """ | |
183 | super(EmployeForm, self).__init__(*args, **kwargs) | |
184 | self.fields['employe'].choices = self._employe_choices() | |
185 | ||
186 | def _employe_choices(self): | |
187 | """ Menu déroulant pour les employés. """ | |
188 | dae_ = dae.Employe.objects.filter(id_rh__isnull=True) | |
189 | copies = dae.Employe.objects.exclude(id_rh__isnull=True) | |
190 | id_copies = [p.id_rh_id for p in copies.all()] | |
191 | rhv1 = rh.Employe.objects.exclude(id__in=id_copies) | |
192 | ||
193 | return [('', 'Nouvel employé')] + \ | |
194 | sorted([('dae-%s' % p.id, unicode(p)) for p in dae_ | copies] + | |
195 | [('rh-%s' % p.id, unicode(p)) for p in rhv1], | |
196 | key=lambda t: t[1]) | |
197 | ||
198 | ||
199 | class DossierForm(forms.ModelForm): | |
200 | """ Formulaire des dossiers. """ | |
201 | class Meta: | |
202 | model = dae.Dossier | |
4d25e2ba | 203 | widgets = dict(statut_residence=forms.RadioSelect(), |
0e0aeb7e OL |
204 | contrat_date_debut=admin_widgets.AdminDateWidget(), |
205 | contrat_date_fin=admin_widgets.AdminDateWidget(), | |
4d25e2ba | 206 | ) |