fix
[auf_rh_dae.git] / project / dae / views.py
index aebd7d6..7277d8b 100644 (file)
@@ -221,12 +221,20 @@ def poste(request, key=None):
             for comparaison in comparaisons:
                 comparaison.poste = poste
                 comparaison.save()
+            saved_cmps = [x.id for x in comparaisons]
 
-            for comparaison in zip(
-                comparaisons,
-                comparaisons_formset.forms):
-                dossier = rh.Dossier.objects.get(
-                    id=comparaison[1].cleaned_data['cmp_poste'])
+            for cmp_f in comparaisons_formset.forms:
+                comparaison = (cmp_f.instance, cmp_f)
+
+                cmp_dossier_id = comparaison[1].cleaned_data.get(
+                    'cmp_poste', None)
+                if (not cmp_dossier_id or
+                    comparaison[0].id not in saved_cmps):
+                    continue
+                cmp_dossier_qs = rh.Dossier.objects.filter(id=cmp_dossier_id)
+                if not cmp_dossier_qs.count() > 0:
+                    continue
+                dossier = rh.Dossier.objects.get(id=cmp_dossier_qs.get().id)
 
                 # Get all remunerations for a period of 1 year,
                 # going back from either: today (if dossier has not
@@ -476,7 +484,7 @@ def embauche(request, key=None, dossier_id=None):
             request.POST,
             queryset=dossier.dae_comparaisons.ma_region_ou_service(
                 request.user
-            )
+            ),
         )
         
         remunForm = RemunForm(request.POST, instance=dossier)
@@ -492,16 +500,26 @@ def embauche(request, key=None, dossier_id=None):
             remunForm.save()
 
             # Ne remplacer que les comparaisons de ma région
+            
             comparaisons = comparaisons_formset.save(commit=False)
             for comparaison in comparaisons:
                 comparaison.dossier = dossier
                 comparaison.save()
+            saved_cmps = [x.id for x in comparaisons]
+
+            for cmp_f in comparaisons_formset.forms:
+                comparaison = (cmp_f.instance, cmp_f)
+
+                cmp_dossier_id = comparaison[1].cleaned_data.get(
+                    'cmp_dossier', None)
+                if (not cmp_dossier_id or
+                    comparaison[0].id not in saved_cmps):
+                    continue
+                cmp_dossier_qs = rh.Dossier.objects.filter(id=cmp_dossier_id)
+                if not cmp_dossier_qs.count() > 0:
+                    continue
+                rhdossier = rh.Dossier.objects.get(id=cmp_dossier_qs.get().id)
 
-            for comparaison in zip(
-                comparaisons,
-                comparaisons_formset.forms):
-                rhdossier = rh.Dossier.objects.get(
-                    id=comparaison[1].cleaned_data['cmp_dossier'])
 
                 # Get all remunerations for a period of 1 year,
                 # going back from either: today (if cdossier has not
@@ -577,6 +595,13 @@ def embauche(request, key=None, dossier_id=None):
     for cmp_form in comparaisons_formset.forms:
         cmp_form.fields['implantation'].queryset = imp_qs
 
+    # Comment out for now.
+    # if dossier and dossier.poste:
+    #     dossier_form.fields['employe_anterieur'].queryset = (
+    #         dossier_form.fields['employe_anterieur'].queryset.filter(
+    #             rh_dossiers__poste=dossier.poste,
+    #             ).distinct()
+    #         )
 
     c = {
         'type_remun': filtered_type_remun(),