fix
[auf_rh_dae.git] / project / dae / views.py
index a57b2f2..7277d8b 100644 (file)
@@ -4,6 +4,7 @@ from datetime import date, datetime
 from dateutil.relativedelta import relativedelta
 
 from auf.django.permissions.decorators import get_object
+from auf.django.references import models as ref
 from django.contrib import messages
 from django.contrib.auth.decorators import login_required, user_passes_test
 from django.contrib.contenttypes.models import ContentType
@@ -44,7 +45,7 @@ from project.decorators import redirect_interdiction,\
 from project.rh import models as rh
 from project import groups
 
-
 # Helpers
 
 def devises():
@@ -220,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 cmp_f in comparaisons_formset.forms:
+                comparaison = (cmp_f.instance, cmp_f)
 
-            for comparaison in zip(
-                comparaisons,
-                comparaisons_formset.forms):
-                dossier = rh.Dossier.objects.get(
-                    id=comparaison[1].cleaned_data['cmp_poste'])
+                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
@@ -288,6 +297,14 @@ def poste(request, key=None):
             financementForm = FinancementForm(instance=poste)
             comparaisons_formset = PosteComparaisonForm(instance=poste)
 
+
+    # Modify queryset so that it is limited to users' rights:
+    imp_qs = dae.ProxyImplantation.dae_manager.ma_region_ou_service(
+        request.user)
+        
+    for cmp_form in comparaisons_formset.forms:
+        cmp_form.fields['implantation'].queryset = imp_qs
+
     vars.update(dict(
         form=form, poste=poste, poste_key=key, piecesForm=piecesForm,
         financementForm=financementForm,
@@ -467,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)
@@ -483,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
@@ -517,7 +544,6 @@ def embauche(request, key=None, dossier_id=None):
                 request, "Le dossier %s a été sauvegardé." % dossier
             )
             if 'save' in request.POST:
-                import pdb; pdb.set_trace()
                 return redirect('embauche_consulter', dossier_id=dossier.id)
             else:
                 return redirect(
@@ -561,6 +587,22 @@ def embauche(request, key=None, dossier_id=None):
                 )
     except dae.Poste.DoesNotExist:
         comparaisons_internes = []
+
+    # Modify queryset so that it is limited to users' rights:
+    imp_qs = dae.ProxyImplantation.dae_manager.ma_region_ou_service(
+        request.user)
+        
+    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(),
         'devises': devises(),