1 # -*- encoding: utf-8 -*-
3 from django
import forms
4 from django
.forms
.models
import inlineformset_factory
5 from django
.contrib
.admin
import widgets
as admin_widgets
6 from auf
.django
.workflow
.forms
import WorkflowFormMixin
7 from datamaster_modeles
import models
as ref
9 from dae
import models
as dae
10 from rh_v1
import models
as rh
12 def label_poste_display(poste
):
13 """Formate un visuel pour un poste dans une liste déroulante"""
14 label
= u
"%s - %s [%s]" %(poste
.type_poste
, poste
.type_poste
.famille_emploi
.nom
, poste
.id)
17 class PostePieceForm(inlineformset_factory(dae
.Poste
, dae
.PostePiece
)):
20 class DossierPieceForm(inlineformset_factory(dae
.Dossier
, dae
.DossierPiece
)):
23 class FinancementForm(inlineformset_factory(dae
.Poste
, dae
.PosteFinancement
, extra
=1)):
26 class JustificationNouvelEmployeForm(inlineformset_factory(dae
.Dossier
,
27 dae
.JustificationNouvelEmploye
,
30 exclude
=('question',))):
32 Formulaire de justification d'un nouvel employé.
33 Le dossier a besoin d'être enregistré une première fois afin de prépopuler les questions.
35 def __init__(self
, *args
, **kwargs
):
36 instance
= kwargs
['instance']
38 q_ids
= [j
.question
.id for j
in instance
.justificationnouvelemploye_set
.filter(dossier
=instance
)]
39 for q
in dae
.JustificationQuestion
.objects
.filter(type="N"):
42 j
= dae
.JustificationNouvelEmploye()
46 super(self
.__class__
, self
).__init__(*args
, **kwargs
)
48 class JustificationAutreEmployeForm(inlineformset_factory(dae
.Dossier
,
49 dae
.JustificationAutreEmploye
,
52 exclude
=('question',))):
54 Formulaire de justification d'un nouvel employé.
55 Le dossier a besoin d'être enregistré une première fois afin de prépopuler les questions.
57 def __init__(self
, *args
, **kwargs
):
58 instance
= kwargs
['instance']
60 q_ids
= [j
.question
.id for j
in instance
.justificationautreemploye_set
.filter(dossier
=instance
)]
61 for q
in dae
.JustificationQuestion
.objects
.filter(type="R"):
64 j
= dae
.JustificationAutreEmploye()
68 super(self
.__class__
, self
).__init__(*args
, **kwargs
)
70 class PosteValidationForm(forms
.ModelForm
):
71 """ Validation d'un poste"""
75 'validation_bureau_regional',
77 'validation_secretaire_general',
81 from ajax_select
.fields
import AutoCompleteSelectField
82 class PosteForm(WorkflowFormMixin
):
83 """ Formulaire des postes. """
89 fields
= ('poste', 'implantation', 'type_poste', 'service', 'nom',
90 'responsable', 'statut_residence', 'mise_a_disposition',
91 'appel', 'date_debut', 'date_fin', 'actif',
92 'regime_travail', 'regime_travail_nb_heure_semaine',
93 'classement_min', 'classement_max',
94 'coefficient_min', 'coefficient_max',
95 'valeur_point_min', 'valeur_point_max',
96 'devise_min', 'devise_max',
97 'salaire_min', 'salaire_max', 'indemn_min', 'indemn_max',
98 'autre_min', 'autre_max', 'devise_comparaison',
99 'comp_locale_min', 'comp_locale_max',
100 'comp_universite_min', 'comp_universite_max',
101 'comp_fonctionpub_min', 'comp_fonctionpub_max',
102 'comp_ong_min', 'comp_ong_max',
103 'comp_autre_min', 'comp_autre_max',
107 widgets
= dict(statut_residence
=forms
.RadioSelect(),
108 appel
=forms
.RadioSelect(),
109 nom
=forms
.TextInput(attrs
={'size': 60},),
110 date_debut
=admin_widgets
.AdminDateWidget(),
111 date_fin
=admin_widgets
.AdminDateWidget(),
112 justification
=forms
.Textarea(attrs
={'cols': 80},),
113 #devise_min=forms.Select(attrs={'disabled':'disabled'}),
114 #devise_max=forms.Select(attrs={'disabled':'disabled'}),
117 responsable
=AutoCompleteSelectField('responsables', required
=True)
118 #responsable = forms.ModelChoiceField(
119 # queryset=rh.Poste.objects.select_related(depth=1))
121 # La liste des choix est laissée vide. Voir __init__ pour la raison.
122 poste
= forms
.ChoiceField(label
="Nouveau poste ou évolution du poste",
123 choices
=(), required
=False)
125 valeur_point_min
= forms
.ModelChoiceField(queryset
=rh
.ValeurPoint
.actuelles
.all(), required
=False)
126 valeur_point_max
= forms
.ModelChoiceField(queryset
=rh
.ValeurPoint
.actuelles
.all(), required
=False)
128 def __init__(self
, *args
, **kwargs
):
129 """ Mise à jour dynamique du contenu du menu des postes.
131 Si on ne met le menu à jour de cette façon, à chaque instantiation du
132 formulaire, son contenu est mis en cache par le système et il ne
133 reflète pas les changements apportés par les ajouts, modifications,
136 Aussi, dans ce cas-ci, on ne peut pas utiliser un ModelChoiceField
137 car le "id" de chaque choix est spécial (voir _poste_choices).
140 super(PosteForm
, self
).__init__(*args
, **kwargs
)
141 self
.fields
['poste'].choices
= self
._poste_choices()
143 # Quand le dae.Poste n'existe pas, on recherche dans les dossiers rhv1
144 if self
.instance
and self
.instance
.id is None:
145 dossiers
= self
.instance
.get_dossiers()
146 if len(dossiers
) > 0:
147 self
.initial
['service'] = dossiers
[0].service_id
148 self
.initial
['nom'] = "%s %s" % (self
.initial
['nom'], self
.instance
.get_complement_nom())
151 def _poste_choices(self
):
152 """ Menu déroulant pour les postes.
154 Constitué des postes de dae et des postes de rh_v1 qui n'ont pas
155 d'équivalent dans dae.
158 dae_
= dae
.Poste
.objects
.filter(actif
=True, id_rh__isnull
=True)
159 copies
= dae
.Poste
.objects
.exclude(id_rh__isnull
=True)
160 id_copies
= [p
.id_rh_id
for p
in copies
.all()]
161 rhv1
= rh
.Poste
.objects
.filter(actif
=True).exclude(id__in
=id_copies
)
162 # Optimisation de la requête
163 rhv1
= rhv1
.select_related(depth
=1)
165 return [('', 'Nouveau poste')] + \
166 sorted([('dae-%s' % p
.id, label_poste_display(p
)) for p
in dae_ | copies
] +
167 [('rh-%s' % p
.id, label_poste_display(p
)) for p
in rhv1
],
172 Validation conditionnelles de certains champs.
174 cleaned_data
= self
.cleaned_data
176 # Gestion de la mise à disposition
177 mise_a_disposition
= cleaned_data
.get("mise_a_disposition")
178 valeur_point_min
= cleaned_data
.get("valeur_point_min")
179 valeur_point_max
= cleaned_data
.get("valeur_point_max")
180 if mise_a_disposition
is False and (valeur_point_min
is None or valeur_point_max
is None):
181 msg
= u
"Ce champ est obligatoire."
182 self
._errors
["valeur_point_min"] = self
.error_class([msg
])
183 self
._errors
["valeur_point_max"] = self
.error_class([msg
])
184 raise forms
.ValidationError("Les valeurs de point sont vides")
190 def save(self
, *args
, **kwargs
):
191 kwargs2
= kwargs
.copy()
192 kwargs2
['commit'] = False
193 poste
= super(PosteForm
, self
).save(*args
, **kwargs2
)
195 if 'commit' not in kwargs
or kwargs
['commit']:
200 class ChoosePosteForm(forms
.ModelForm
):
205 # La liste des choix est laissée vide. Voir PosteForm.__init__.
206 poste
= forms
.ChoiceField(choices
=(), required
=False)
208 def __init__(self
, *args
, **kwargs
):
209 super(ChoosePosteForm
, self
).__init__(*args
, **kwargs
)
210 self
.fields
['poste'].choices
= self
._poste_choices()
212 def _poste_choices(self
):
213 """ Menu déroulant pour les postes. """
214 dae_
= dae
.Poste
.objects
.filter(id_rh__isnull
=True)
215 copies
= dae
.Poste
.objects
.exclude(id_rh__isnull
=True)
216 id_copies
= [p
.id_rh_id
for p
in copies
.all()]
218 return [('', '----------')] + \
219 sorted([('dae-%s' % p
.id, unicode(p
)) for p
in dae_ | copies
],
223 class EmployeForm(forms
.ModelForm
):
224 """ Formulaire des employés. """
227 fields
= ('employe', 'nom', 'prenom', 'genre')
229 # La liste des choix est laissée vide. Voir Poste.__init__ pour la raison.
230 employe
= forms
.ChoiceField(choices
=(), required
=False)
232 def __init__(self
, *args
, **kwargs
):
233 """ Mise à jour dynamique du contenu du menu des employés. """
234 super(EmployeForm
, self
).__init__(*args
, **kwargs
)
235 self
.fields
['employe'].choices
= self
._employe_choices()
237 def _employe_choices(self
):
238 """ Menu déroulant pour les employés. """
239 dae_
= dae
.Employe
.objects
.filter(id_rh__isnull
=True)
240 copies
= dae
.Employe
.objects
.exclude(id_rh__isnull
=True)
241 id_copies
= [p
.id_rh_id
for p
in copies
.all()]
242 rhv1
= rh
.Employe
.objects
.exclude(id__in
=id_copies
)
244 return [('', 'Nouvel employé')] + \
245 sorted([('dae-%s' % p
.id, unicode(p
)) for p
in dae_ | copies
] +
246 [('rh-%s' % p
.id, unicode(p
)) for p
in rhv1
],
250 class DossierForm(forms
.ModelForm
):
251 """ Formulaire des dossiers. """
254 widgets
= dict(statut_residence
=forms
.RadioSelect(),
255 contrat_date_debut
=admin_widgets
.AdminDateWidget(),
256 contrat_date_fin
=admin_widgets
.AdminDateWidget(),