complété la sauvegarde de l'employé et du dossier
authorNicolas Cadou <ncadou@cadou.ca>
Thu, 7 Apr 2011 02:50:53 +0000 (22:50 -0400)
committerNicolas Cadou <ncadou@cadou.ca>
Thu, 7 Apr 2011 02:50:53 +0000 (22:50 -0400)
project/dae/templates/dae/embauche.html
project/dae/urls.py
project/dae/views.py

index 9b2c4af..1d1e55b 100644 (file)
@@ -38,6 +38,8 @@
       <input type="hidden" id="poste" name="poste" value="dae-{{ poste.id }}" />
       <input type="hidden" id="implantation" name="implantation"
              value="{{ poste.implantation_id }}" />
+      <input type="hidden" id="dossier" name="dossier"
+             value="{{ dossier.id }}" />
 
       <script type="text/javascript">
         function activateEmployeDropDown() {
index 33269f4..072b4fd 100644 (file)
@@ -11,6 +11,8 @@ urlpatterns = patterns(
     url(r'^poste$', 'poste', name='poste'),
     url(r'^poste/(?P<key>.*)$', 'poste', name='poste'),
     url(r'^embauche$', 'embauche', name='embauche'),
+    url(r'^embauche/(?P<key>.*)/(?P<dossier>.*)$', 'embauche',
+        name='embauche'),
     url(r'^embauche/(?P<key>.*)$', 'embauche', name='embauche'),
     url(r'^employe$', 'employe', name='employe'),
     url(r'^employe/(?P<key>.*)$', 'employe', name='employe'),
index 8b5e6b0..8d81ff4 100644 (file)
@@ -1,5 +1,6 @@
 # -*- encoding: utf-8 -*-
 from json import dumps
+import warnings
 
 from django.http import Http404, HttpResponse
 from django.shortcuts import redirect, render_to_response, get_object_or_404
@@ -119,33 +120,60 @@ def embauche(request, key=None, dossier=None):
         if source != 'dae':
             return Http404
         poste = get_object_or_404(dae.Poste, pk=id)
-        employe_form = EmployeForm()
 
         if request.POST:
-            employe_source, id = request.POST['employe'].split('-')
-            if employe_source == 'dae':
-                employe = get_object_or_404(dae.Employe, pk=id)
-            elif employe_source == 'rh':
-                e = get_object_or_404(rh.Employe, pk=id)
-                employe = dae.Employe(id_rh=e, prenom=e.prenom, nom=e.nom,
-                                      genre=e.genre)
+            if request.POST['employe'] == '':
+                # Nouvel employé
+                employe = dae.Employe()
             else:
-                raise Http404
+                employe_source, id = request.POST['employe'].split('-')
+                if employe_source == 'dae':
+                    # Employé DAE
+                    employe = get_object_or_404(dae.Employe, pk=id)
+                elif employe_source == 'rh':
+                    # Employé RH, on le copie dans DAE
+                    e = get_object_or_404(rh.Employe, pk=id)
+                    employe = dae.Employe(id_rh=e, prenom=e.prenom, nom=e.nom,
+                                          genre=e.genre)
+                else:
+                    raise Http404
+
             employe_form = EmployeForm(request.POST, instance=employe)
-            if 'save' in request.POST and employe_form.is_valid():
-                employe_form.save()
+
+            if 'save' in request.POST:
+                if employe_form.is_valid():
+                    data = dict(request.POST.items())
+                    with warnings.catch_warnings():
+                        warnings.simplefilter('ignore')
+                        employe = employe_form.save()
+                    data['employe'] = 'dae-%s' % employe.id
+                    employe_form = EmployeForm(data, instance=employe)
                 if not dossier:
                     dossier = dae.Dossier(poste=poste, employe=employe)
                 else:
                     dossier = get_object_or_404(dae.Dossier, pk=dossier)
                 dossier_form = DossierForm(request.POST, instance=dossier)
+                if dossier_form.is_valid():
+                    dossier = dossier_form.save()
+                    return redirect('embauche', key='dae-%s' % poste.id,
+                                    dossier=dossier.id)
+            else:
+                dossier_form = DossierForm(instance=dossier)
         else:
+            # Initialisation d'un formulaire vide
             dossier_rh = rh.Dossier()
             poste_rh = poste.id_rh
-            dossier = pre_filled_dossier(dossier_rh, 'new', poste_rh)
+            if dossier:
+                dossier = get_object_or_404(dae.Dossier, pk=dossier)
+                employe = dossier.employe
+                data = dict(employe='dae-%s' % employe.id)
+                employe_form = EmployeForm(initial=data, instance=employe)
+            else:
+                dossier = pre_filled_dossier(dossier_rh, 'new', poste_rh)
+                employe_form = EmployeForm()
             dossier_form = DossierForm(instance=dossier)
 
-        vars = dict(step='employe', poste=poste,
+        vars = dict(step='employe', poste=poste, dossier=dossier,
                     forms=dict(employe=employe_form, dossier=dossier_form))
 
     return render_to_response('dae/embauche.html', vars,