Refactoring de la vue d'édition de DAE
authorEric Mc Sween <eric.mcsween@auf.org>
Tue, 1 Nov 2011 19:09:50 +0000 (15:09 -0400)
committerEric Mc Sween <eric.mcsween@auf.org>
Tue, 1 Nov 2011 19:17:56 +0000 (15:17 -0400)
project/dae/forms.py
project/dae/templates/dae/embauche-choisir-poste.html [new file with mode: 0644]
project/dae/templates/dae/embauche.html
project/dae/urls.py
project/dae/views.py

index 159ec77..ee96d58 100644 (file)
@@ -295,7 +295,6 @@ class EmployeForm(forms.ModelForm):
         self.fields['employe'].choices = _employe_choices(self, request)
 
 
-
 class DossierForm(forms.ModelForm):
     """ Formulaire des dossiers. """
     class Meta:
diff --git a/project/dae/templates/dae/embauche-choisir-poste.html b/project/dae/templates/dae/embauche-choisir-poste.html
new file mode 100644 (file)
index 0000000..482b1b9
--- /dev/null
@@ -0,0 +1,27 @@
+{% extends 'base.html' %}
+{% load adminmedia %}
+
+{% block title %}RH - DAE - Embauche{% endblock %}
+{% block titre %}Ressources humaines{% endblock %}
+{% block sous_titre %}Demande d'autorisation d'engagement{% endblock %}
+
+{% block extrahead %}
+  <script src="{{ MEDIA_URL }}js/jquery-1.5.1.min.js" type="text/javascript"></script>
+  <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/dae.css" />
+  <script src="{{ MEDIA_URL }}js/dae.js" type="text/javascript"></script>
+  <script src="{{ MEDIA_URL }}js/dae-embauche.js" type="text/javascript"></script>
+{% endblock %}
+
+{% block main %}
+<h1>Demande d'autorisation d'engagement de personnel</h1>
+<form action="" method="post" enctype="multipart/form-data">
+  <h2 class="section">SECTION 1 - POSTE</h2>
+  <fieldset>
+    <h2>Poste visé</h2>
+
+    <table cellspacing="0">
+      {{ form.as_table }}
+    </table>
+  </fieldset>
+</form>
+{% endblock %}
index 5c75d4e..df4a3d6 100644 (file)
 
 <h1>Demande d'autorisation d'engagement de personnel</h1>
 <form action="" method="post" enctype="multipart/form-data">
-{% if step == 'poste' %}
-    <h2 class="section">SECTION 1 - POSTE</h2>
-    <fieldset>
-    <h2>Poste visé</h2>
-
-    <table cellspacing="0">
-        {{ form.as_table }}
-    </table>
-    </fieldset>
-{% else %}
     <h2 class="section">SECTION 1 - POSTE</h2>
     {% include 'dae/poste_resume.html' %}
     
       <input type="submit" name="continue" value="Sauvegarder (et ajouter des lignes)" />
       <input type="submit" name="save" value="Sauvegarder" />
     </form>
-  {% endif %}
 {% endblock %}
 
index d92dcdd..c25c55e 100644 (file)
@@ -14,17 +14,17 @@ urlpatterns = patterns(
 
     # embauche
     url(r'^embauches$', 'embauches_liste', name='dae_embauches_liste'),
-    url(r'^embauche$', 'embauche', name='embauche'),
+    url(r'^embauche$', 'embauche_choisir_poste', name='embauche'),
     url(r'^embauche/consulter/(?P<dossier_id>.*)$', 'embauche_consulter', name='embauche_consulter'),
     url(r'^embauche/(?P<key>.*)/(?P<dossier_id>.*)$', 'embauche', name='embauche'),
     url(r'^embauche/(?P<key>.*)$', 'embauche', name='embauche'),
 
-    # ajax    
+    # ajax
     url(r'^employe$', 'employe', name='employe'),
     url(r'^employe/(?P<key>.*)$', 'employe', name='employe'),
     url(r'^dossier$', 'dossier', name='dossier'),
     url(r'^dossier/(?P<poste_key>.*)/(?P<employe_key>.*)$', 'dossier', name='dossier'),
-    url(r'^pieces-dossiers/(\d+)$', 'dossier_piece', name='dossier_piece'), 
+    url(r'^pieces-dossiers/(\d+)$', 'dossier_piece', name='dossier_piece'),
     url(r'^dossier_resume/(?P<dossier_id>.*)$', 'dossier_resume', name='dossier_resume'),
     url(r'^poste_resume/(?P<dossier_id>.*)$', 'poste_resume', name='poste_resume'),
     url(r'^salaire$', 'salaire', name='salaire'),
index 53ec503..5d4320e 100644 (file)
@@ -225,119 +225,104 @@ def embauche_consulter(request, dossier_id):
         return render_to_response('dae/embauche_pdf.html', vars, RequestContext(request))
 
 @dae_groupe_requis
+def embauche_choisir_poste(request):
+    return render_to_response('dae/embauche-choisir-poste.html', {
+        'form': ChoosePosteForm(request=request)
+    }, RequestContext(request))
+
+@dae_groupe_requis
 @dossier_dans_ma_region_ou_service
 @dossier_est_modifiable
 def embauche(request, key=None, dossier_id=None):
     """ Formulaire d'autorisation d'embauche. """
-    if not key:
-        vars = dict(step='poste', form=ChoosePosteForm(request=request))
-    else:
-        type_remun = filtered_type_remun()
-        vars = dict(type_remun=type_remun)
-        source, id = key.split('-')
-        if source != 'dae':
-            return Http404
-        poste = get_object_or_404(dae.Poste, pk=id)
-        if not dossier_id:
-            vars['new'] = True
 
-        if request.POST:
-            if request.POST['employe'] == '':
-                # Nouvel employé
-                employe = dae.Employe()
+    # Récupérer ou créer un poste et un dossier
+    source, id = key.split('-')
+    if source != 'dae':
+        return Http404
+    poste = get_object_or_404(dae.Poste, pk=id)
+
+    if request.POST:
+        if request.POST['employe'] == '':
+            # Nouvel employé
+            employe = dae.Employe()
+        else:
+            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:
-                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, request=request)
-
-            if 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, request=request)
-
-                if not dossier_id:
-                    dossier = dae.Dossier(poste=poste, employe=employe)
-                else:
-                    dossier = get_object_or_404(dae.Dossier, pk=dossier_id)
-
-                dossier_form = DossierForm(request.POST, instance=dossier)
-                piecesForm = DossierPieceForm(request.POST, request.FILES, instance=dossier)
-                dossiersComparaisonsForm = DossierComparaisonForm(request.POST, instance=dossier)
-                remunForm = RemunForm(request.POST, instance=dossier)
-
-                if dossier_form.is_valid() and \
-                   piecesForm.is_valid() and \
-                   dossiersComparaisonsForm.is_valid() and \
-                   remunForm.is_valid():
-                    employe.save()
-                    dossier = dossier_form.save()
-                    piecesForm.instance = dossier
-                    piecesForm.save()
-                    dossiersComparaisonsForm.instance = dossier
-                    dossiersComparaisonsForm.save()
-                    remunForm.instance = dossier
-                    remunForm.save()
-
-                    messages.add_message(request, messages.SUCCESS, "Le dossier %s a été sauvegardé." % dossier)
-                    if request.POST.has_key('save'):
-                        return redirect('embauche_consulter', dossier_id=dossier.id)
-                    else:
-                        return redirect('embauche', key=dossier.poste.key, dossier_id=dossier.id)
-                        
-                else:
-                    messages.add_message(request, messages.ERROR, 'Il y a des erreurs dans le formulaire.')
-                    
+                raise Http404
+
+        employe_form = EmployeForm(request.POST, instance=employe, request=request)
+
+        if employe_form.is_valid():
+            data = dict(request.POST.items())
+            employe = employe_form.save()
+            data['employe'] = 'dae-%s' % employe.id
+            employe_form = EmployeForm(data, instance=employe, request=request)
+
+        if not dossier_id:
+            dossier = dae.Dossier(poste=poste, employe=employe)
+        else:
+            dossier = get_object_or_404(dae.Dossier, pk=dossier_id)
+
+        dossier_form = DossierForm(request.POST, instance=dossier)
+        piecesForm = DossierPieceForm(request.POST, request.FILES, instance=dossier)
+        dossiersComparaisonsForm = DossierComparaisonForm(request.POST, instance=dossier)
+        remunForm = RemunForm(request.POST, instance=dossier)
+
+        if dossier_form.is_valid() and \
+           piecesForm.is_valid() and \
+           dossiersComparaisonsForm.is_valid() and \
+           remunForm.is_valid():
+            employe.save()
+            piecesForm.save()
+            dossiersComparaisonsForm.save()
+            remunForm.save()
+            messages.success(request, "Le dossier %s a été sauvegardé." % dossier)
+            if request.POST.has_key('save'):
+                return redirect('embauche_consulter', dossier_id=dossier.id)
             else:
-                dossier_form = DossierForm(instance=dossier)
-                piecesForm = DossierPieceForm(instance=dossier)
-                dossiersComparaisonsForm = DossierComparaisonForm(instance=dossier)
-                remunForm = RemunForm(instance=dossier)
+                return redirect('embauche', key=dossier.poste.key, dossier_id=dossier.id)
+                
+        else:
+            messages.add_message(request, messages.ERROR, 'Il y a des erreurs dans le formulaire.')
+            
+    else:
+        # Initialisation d'un formulaire vide
+        if dossier_id:
+            dossier = get_object_or_404(dae.Dossier, pk=dossier_id)
+            employe = dossier.employe
+            data = dict(employe='dae-%s' % employe.id)
+            employe_form = EmployeForm(initial=data, instance=employe, request=request)
         else:
-            # Initialisation d'un formulaire vide
             dossier_rh = rh.Dossier()
             poste_rh = poste.id_rh
-            if dossier_id:
-                dossier = get_object_or_404(dae.Dossier, pk=dossier_id)
-                employe = dossier.employe
-                data = dict(employe='dae-%s' % employe.id)
-                employe_form = EmployeForm(initial=data, instance=employe, request=request)
-            else:
-                dossier = pre_filled_dossier(dossier_rh, 'new', poste_rh)
-                employe_form = EmployeForm(request=request)
-
-            dossier_form = DossierForm(instance=dossier)
-            piecesForm = DossierPieceForm(instance=dossier)
-            dossiersComparaisonsForm = DossierComparaisonForm(instance=dossier)
-            remunForm = RemunForm(instance=dossier)
-
-        vars = dict(step='employe',
-                type_remun=type_remun,
-                devises=devises(),
-                poste=poste,
-                dossier=dossier,
-                piecesForm=piecesForm,
-                remunForm=remunForm,
-                dossiersComparaisonsForm=dossiersComparaisonsForm,
-                forms=dict(employe=employe_form, dossier=dossier_form, )
-                )
-    
+            dossier = pre_filled_dossier(dossier_rh, 'new', poste_rh)
+            employe_form = EmployeForm(request=request)
+
+        dossier_form = DossierForm(instance=dossier)
+        piecesForm = DossierPieceForm(instance=dossier)
+        dossiersComparaisonsForm = DossierComparaisonForm(instance=dossier)
+        remunForm = RemunForm(instance=dossier)
+
+    return render_to_response('dae/embauche.html', {
+        'type_remun': filtered_type_remun(),
+        'devises': devises(),
+        'poste': poste,
+        'dossier': dossier,
+        'piecesForm': piecesForm,
+        'remunForm': remunForm,
+        'dossiersComparaisonsForm': dossiersComparaisonsForm,
+        'forms': dict(employe=employe_form, dossier=dossier_form, )
+    }, RequestContext(request))
 
-    return render_to_response('dae/embauche.html', vars,
-                              RequestContext(request))
 @dae_groupe_requis
 @dossier_dans_ma_region_ou_service
 def embauches_liste(request):