From 4e61ffb4d5ff26957aaeb54a37deb383d1a4a5cf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Schneider?= <jschneider@entrouvert.com>
Date: Tue, 22 Jul 2014 19:18:36 +0200
Subject: [PATCH] dossiers: merge administrative forms into one form

Closes #2739
---
 calebasse/dossiers/forms.py                        | 59 ++++++--------
 .../dossiers/patientrecord_tab2_fiche_adm.html     | 92 +++++++++-------------
 calebasse/dossiers/templatetags/dossiers.py        | 29 +++++++
 calebasse/dossiers/views.py                        | 12 +--
 4 files changed, 92 insertions(+), 100 deletions(-)

diff --git a/calebasse/dossiers/forms.py b/calebasse/dossiers/forms.py
index 48e088a..0fbc495 100644
--- a/calebasse/dossiers/forms.py
+++ b/calebasse/dossiers/forms.py
@@ -106,10 +106,30 @@ class GeneralForm(ModelForm):
                 'pause_comment': forms.Textarea(attrs={'cols': 50, 'rows': 3}),
                 }
 
-class CivilStatusForm(ModelForm):
+class AdministrativeForm(ModelForm):
+    coordinators = make_ajax_field(PatientRecord, 'coordinators', 'worker', True)
     class Meta:
         model = PatientRecord
-        fields = ('first_name', 'last_name', 'birthdate', 'birthplace', 'gender', 'nationality')
+        # ID
+        fields = ('first_name', 'last_name', 'birthdate', 'birthplace',
+                'gender', 'nationality',)
+        # inscription
+        fields += ('analysemotive', 'familymotive', 'provenance',
+            'advicegiver', 'provenanceplace',)
+        # out
+        fields += ('outmotive', 'outto',)
+        # family
+        fields += ('sibship_place', 'nb_children_family', 'parental_authority',
+                'family_situation', 'child_custody', 'job_mother', 'job_father',
+                'rm_mother', 'rm_father', 'family_comment',)
+        # transport
+        fields += ('transporttype', 'transportcompany',
+                  'simple_appointment_transport', 'periodic_appointment_transport',)
+        # Follow up
+        fields += ('coordinators', 'externaldoctor', 'externalintervener',)
+        widgets = {
+                'family_comment': forms.Textarea(attrs={'cols': 50, 'rows': 1}),
+                }
 
 
 class PhysiologyForm(ModelForm):
@@ -148,35 +168,6 @@ class PhysiologyForm(ModelForm):
                 CodeCFTMEA.objects.filter(axe=3)
 
 
-class InscriptionForm(ModelForm):
-    class Meta:
-        model = PatientRecord
-        fields = ('analysemotive', 'familymotive', 'provenance',
-            'advicegiver', 'provenanceplace')
-        widgets = {}
-
-class OutForm(ModelForm):
-    class Meta:
-        model = PatientRecord
-        fields = ('outmotive', 'outto')
-        widgets = {}
-
-class FamilyForm(ModelForm):
-    class Meta:
-        model = PatientRecord
-        fields = ('sibship_place', 'nb_children_family', 'parental_authority',
-                'family_situation', 'child_custody', 'job_mother', 'job_father',
-                'rm_mother', 'rm_father', 'family_comment')
-        widgets = {
-                'family_comment': forms.Textarea(attrs={'cols': 50, 'rows': 1}),
-                }
-
-class TransportFrom(ModelForm):
-    class Meta:
-        model = PatientRecord
-        fields = ('transporttype', 'transportcompany',
-                  'simple_appointment_transport', 'periodic_appointment_transport')
-
 class PaperIDForm(ModelForm):
     class Meta:
         model = PatientRecord
@@ -191,12 +182,6 @@ class PolicyHolderForm(ModelForm):
                 'contact_comment': forms.Textarea(attrs={'cols': 50, 'rows': 2}),
                 }
 
-class FollowUpForm(ModelForm):
-    coordinators = make_ajax_field(PatientRecord, 'coordinators', 'worker', True)
-    class Meta:
-        model = PatientRecord
-        fields = ('coordinators', 'externaldoctor', 'externalintervener')
-
 class PatientContactForm(ModelForm):
     health_org = forms.CharField(label=u"Numéro de l'organisme destinataire", required=False)
 
diff --git a/calebasse/dossiers/templates/dossiers/patientrecord_tab2_fiche_adm.html b/calebasse/dossiers/templates/dossiers/patientrecord_tab2_fiche_adm.html
index 5c33e3e..c8a67e5 100644
--- a/calebasse/dossiers/templates/dossiers/patientrecord_tab2_fiche_adm.html
+++ b/calebasse/dossiers/templates/dossiers/patientrecord_tab2_fiche_adm.html
@@ -1,31 +1,29 @@
+{% load dossiers %}
 <div id="tabs-2">
-  <form method="post" action="tab2" id="id-form" class="patientrecordform">{% csrf_token %}
+  <form method="post" action="tab2" id="administrative-form" class="patientrecordform">{% csrf_token %}
+  <button class="save enable-on-change">Enregistrer</button>
     <div class="frame inline">
       <h3>État civil</h3>
-      <button class="save">✔</button>
       <table class="basic">
         <tbody>
-        <tr><td class="width-limited">{{ forms.id.last_name.label_tag }}</td><td><span class="lastname">{{ forms.id.last_name }}</span></td><td>{{ forms.id.last_name.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.id.first_name.label_tag }}</td><td>{{ forms.id.first_name }}</td><td>{{ forms.id.first_name.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.id.birthplace.label_tag }}</td><td>{{ forms.id.birthplace }}</td><td>{{ forms.id.birthplace.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.id.birthdate.label_tag }}</td><td>{{ forms.id.birthdate }}</td><td>{{ forms.id.birthdate.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.id.gender.label_tag }}</td><td>{{ forms.id.gender }}</td><td>{{ forms.id.gender.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.id.nationality.label_tag }}</td><td>{{ forms.id.nationality }}</td><td>{{ forms.id.nationality.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.last_name.label_tag }}</td><td><span class="lastname">{{ form.last_name }}</span></td><td>{{ form.last_name.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.first_name.label_tag }}</td><td>{{ form.first_name }}</td><td>{{ form.first_name.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.birthplace.label_tag }}</td><td>{{ form.birthplace }}</td><td>{{ form.birthplace.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.birthdate.label_tag }}</td><td>{{ form.birthdate }}</td><td>{{ form.birthdate.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.gender.label_tag }}</td><td>{{ form.gender }}</td><td>{{ form.gender.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.nationality.label_tag }}</td><td>{{ form.nationality }}</td><td>{{ form.nationality.errors }}</td></tr>
         </tbody>
       </table>
       <input type="hidden"  name="tab"  value="1">
     </div>
-  </form>
 
-  <form method="post" action="tab2" id="inscription-form" class="patientrecordform">
-    {% csrf_token %}
     <div class="frame inline">
       <h3>Inscription</h3>
-      <button class="save">✔</button>
       <table class="basic">
         <tbody>
-        {% for field in forms.inscription %}
-        <tr><td class="width-limited">{{ field.label_tag  }}</td><td>{{ field }}
+        {% get_fieldset analysemotive,familymotive,provenance,advicegiver,provenanceplace as inscription_fields from form %}
+        {% for field in inscription_fields %}
+        <tr><td class="width-limited">{{ field.label_tag }}</td><td>{{ field }}
             <button type="button"
               class="dialog-button"
               data-url="../../ressources/{{ field.name }}/new/ #form-content"
@@ -39,16 +37,13 @@
       </table>
       <input type="hidden"  name="tab"  value="1">
     </div>
-  </form>
 
-  <form method="post" action="tab2" id="out-form" class="patientrecordform">
-    {% csrf_token %}
     <div class="frame inline">
       <h3>Sortie</h3>
-      <button class="save">✔</button>
       <table class="basic">
         <tbody>
-        {% for field in forms.out %}
+        {% get_fieldset outmotive,outto as out_fields from form %}
+        {% for field in out_fields %}
         <tr><td class="width-limited">{{ field.label_tag  }}</td><td>{{ field }}
             <button type="button"
               class="dialog-button"
@@ -63,89 +58,82 @@
       </table>
       <input type="hidden"  name="tab"  value="1">
     </div>
-  </form>
 
-  <form method="post" action="tab2" id="family-form" class="patientrecordform">
-    {% csrf_token %}
     <div class="frame inline">
       <h3>Famille</h3>
-      <button class="save">✔</button>
       <table class="basic">
         <tbody>
-        <tr><td class="width-limited">{{ forms.family.sibship_place.label_tag  }}</td><td>{{ forms.family.sibship_place }}</td><td>{{ forms.family.sibship_place.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.family.nb_children_family.label_tag  }}</td><td>{{ forms.family.nb_children_family }}</td><td>{{ forms.family.nb_children_family.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.sibship_place.label_tag  }}</td><td>{{ form.sibship_place }}</td><td>{{ form.sibship_place.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.nb_children_family.label_tag  }}</td><td>{{ form.nb_children_family }}</td><td>{{ form.nb_children_family.errors }}</td></tr>
         <tr><td class="width-limited"><label>Rang (gémellité) :  </label></td><td>{{ object.twinning_rank|default_if_none:"Aucun" }}</td><td></td></tr>
-        <tr><td class="width-limited">{{ forms.family.parental_authority.label_tag  }}</td><td>{{ forms.family.parental_authority }}
+        <tr><td class="width-limited">{{ form.parental_authority.label_tag  }}</td><td>{{ form.parental_authority }}
             <button type="button"
               class="dialog-button"
               data-url="../../ressources/parentalauthoritytype/new/ #form-content"
               data-default-button="Ajouter"
               data-next-url="{{ request.get_full_path }}#tab=1"
               data-add-select="#id_family-parental_authority"
-              title="Ajouter un type d'autorité parentale">+</button></td><td>{{ forms.family.parental_authority.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.family.family_situation.label_tag  }}</td><td>{{ forms.family.family_situation }}
+              title="Ajouter un type d'autorité parentale">+</button></td><td>{{ form.parental_authority.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.family_situation.label_tag  }}</td><td>{{ form.family_situation }}
             <button type="button"
               class="dialog-button"
               data-url="../../ressources/familysituationtype/new/ #form-content"
               data-default-button="Ajouter"
               data-next-url="{{ request.get_full_path }}#tab=1"
               data-add-select="#id_family-family_situation"
-              title="Ajouter un type de situation familiale">+</button></td><td>{{ forms.family.family_situation.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.family.child_custody.label_tag  }}</td><td>{{ forms.family.child_custody }}
+              title="Ajouter un type de situation familiale">+</button></td><td>{{ form.family_situation.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.child_custody.label_tag  }}</td><td>{{ form.child_custody }}
             <button type="button"
               class="dialog-button"
               data-url="../../ressources/parentalcustodytype/new/ #form-content"
               data-default-button="Ajouter"
               data-next-url="{{ request.get_full_path }}#tab=1"
               data-add-select="#id_family-child_custody"
-              title="Ajouter un type de garde parentale">+</button></td><td>{{ forms.family.child_custody.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.family.job_mother.label_tag  }}</td><td>{{ forms.family.job_mother }}
+              title="Ajouter un type de garde parentale">+</button></td><td>{{ form.child_custody.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.job_mother.label_tag  }}</td><td>{{ form.job_mother }}
             <button type="button"
               class="dialog-button"
               data-url="../../ressources/job/new/ #form-content"
               data-default-button="Ajouter"
               data-next-url="{{ request.get_full_path }}#tab=1"
               data-add-select="#id_family-job_mother"
-              title="Ajouter un type de profession">+</button></td><td>{{ forms.family.job_mother.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.family.job_father.label_tag  }}</td><td>{{ forms.family.job_father }}
+              title="Ajouter un type de profession">+</button></td><td>{{ form.job_mother.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.job_father.label_tag  }}</td><td>{{ form.job_father }}
             <button type="button"
               class="dialog-button"
               data-url="../../ressources/job/new/ #form-content"
               data-default-button="Ajouter"
               data-next-url="{{ request.get_full_path }}#tab=1"
               data-add-select="#id_family-job_father"
-              title="Ajouter un type de profession">+</button></td><td>{{ forms.family.job_father.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.family.rm_mother.label_tag  }}</td><td>{{ forms.family.rm_mother }}
+              title="Ajouter un type de profession">+</button></td><td>{{ form.job_father.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.rm_mother.label_tag  }}</td><td>{{ form.rm_mother }}
             <button type="button"
               class="dialog-button"
               data-url="../../ressources/maritalstatustype/new/ #form-content"
               data-default-button="Ajouter"
               data-next-url="{{ request.get_full_path }}#tab=1"
               data-add-select="#id_family-rm_mother"
-              title="Ajouter un type de régime">+</button></td><td>{{ forms.family.rm_mother.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.family.rm_father.label_tag  }}</td><td>{{ forms.family.rm_father }}
+              title="Ajouter un type de régime">+</button></td><td>{{ form.rm_mother.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.rm_father.label_tag  }}</td><td>{{ form.rm_father }}
             <button type="button"
               class="dialog-button"
               data-url="../../ressources/maritalstatustype/new/ #form-content"
               data-default-button="Ajouter"
               data-next-url="{{ request.get_full_path }}#tab=1"
               data-add-select="#id_family-rm_father"
-              title="Ajouter un type de régime">+</button></td><td>{{ forms.family.rm_father.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.family.family_comment.label_tag  }}</td><td>{{ forms.family.family_comment }}</td><td>{{ forms.family.family_comment.errors }}</td></tr>
+              title="Ajouter un type de régime">+</button></td><td>{{ form.rm_father.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.family_comment.label_tag  }}</td><td>{{ form.family_comment }}</td><td>{{ form.family_comment.errors }}</td></tr>
         </tbody>
       </table>
       <input type="hidden"  name="tab"  value="1">
     </div>
-  </form>
 
-  <form method="post" action="tab2" id="transport-form" class="patientrecordform">
-    {% csrf_token %}
     <div class="frame inline">
       <h3>Transport</h3>
-      <button class="save">✔</button>
       <table class="basic">
         <tbody>
-        {% for field in forms.transport %}
+        {% get_fieldset transporttype,transportcompany,simple_appointment_transport,periodic_appointment_transport as transport_fields from form %}
+        {% for field in transport_fields %}
         <tr><td class="width-limited">{{ field.label_tag }}</td><td>{{ field }}
             <button type="button"
               class="dialog-button"
@@ -168,35 +156,33 @@
         </tr>
       </table>
     </div>
-  </form>
 
-  <form method="post" action="tab2" id="followup-form" class="patientrecordform">{% csrf_token %}
     <div class="frame inline">
       <h3>Suivi du patient</h3>
-      <button class="save">✔</button>
       <table class="basic">
         <tbody>
-        <tr><td class="width-limited">{{ forms.followup.coordinators.label_tag  }}</td><td>{{ forms.followup.coordinators }}</td><td>{{ forms.followup.coordinators.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.followup.externaldoctor.label_tag  }}</td><td>{{ forms.followup.externaldoctor }}
+        <tr><td class="width-limited">{{ form.coordinators.label_tag  }}</td><td>{{ form.coordinators }}</td><td>{{ form.coordinators.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.externaldoctor.label_tag  }}</td><td>{{ form.externaldoctor }}
             <button type="button"
               class="dialog-button"
               data-url="../../personnes/externaltherapist/new/ #form-content"
               data-default-button="Ajouter"
               data-next-url="{{ request.get_full_path }}#tab=1"
               data-add-select="#id_followup-externaldoctor"
-              title="Ajouter un médecin extérieur">+</button></td><td>{{ forms.followup.externaldoctor.errors }}</td></tr>
-        <tr><td class="width-limited">{{ forms.followup.externalintervener.label_tag  }}</td><td>{{ forms.followup.externalintervener }}
+              title="Ajouter un médecin extérieur">+</button></td><td>{{ form.externaldoctor.errors }}</td></tr>
+        <tr><td class="width-limited">{{ form.externalintervener.label_tag  }}</td><td>{{ form.externalintervener }}
             <button type="button"
               class="dialog-button"
               data-url="../../personnes/externalworker/new/ #form-content"
               data-default-button="Ajouter"
               data-next-url="{{ request.get_full_path }}#tab=1"
               data-add-select="#id_followup-externalintervener"
-              title="Ajouter un intervenant extérieur">+</button></td><td>{{ forms.followup.externalintervener.errors }}</td></tr>
+              title="Ajouter un intervenant extérieur">+</button></td><td>{{ form.externalintervener.errors }}</td></tr>
         </tbody>
       </table>
     </div>
     <input type="hidden"  name="tab"  value="1">
+  <button class="save enable-on-change">Enregistrer</button>
   </form>
 
   <div class="frame">
diff --git a/calebasse/dossiers/templatetags/dossiers.py b/calebasse/dossiers/templatetags/dossiers.py
index fbbe17c..c894ed4 100644
--- a/calebasse/dossiers/templatetags/dossiers.py
+++ b/calebasse/dossiers/templatetags/dossiers.py
@@ -1,7 +1,36 @@
+import copy
+
 from django import template
+from django import forms
+from django.utils.datastructures import SortedDict
 
 register = template.Library()
 
+def get_fieldset(parser, token):
+    try:
+        name, fields, as_, variable_name, from_, form = token.split_contents()
+    except ValueError:
+        raise template.TemplateSyntaxError('bad arguments for %r'  % token.split_contents()[0])
+
+    return FieldSetNode(fields.split(','), variable_name, form)
+
+get_fieldset = register.tag(get_fieldset)
+
+
+class FieldSetNode(template.Node):
+    def __init__(self, fields, variable_name, form_variable):
+        self.fields = fields
+        self.variable_name = variable_name
+        self.form_variable = form_variable
+
+    def render(self, context):
+        form = template.Variable(self.form_variable).resolve(context)
+        new_form = copy.copy(form)
+        new_form.fields = SortedDict([(key, value) for key, value in form.fields.items() if key in self.fields])
+        context[self.variable_name] = new_form
+
+        return u''
+
 def phone(value):
     result = ""
     if len(value) == 10:
diff --git a/calebasse/dossiers/views.py b/calebasse/dossiers/views.py
index f338663..94fda70 100644
--- a/calebasse/dossiers/views.py
+++ b/calebasse/dossiers/views.py
@@ -290,20 +290,12 @@ class PatientRecordGeneralView(cbv.UpdateView):
 
 tab1_general = PatientRecordGeneralView.as_view()
 
-class PatientRecordAdmView(cbv.ServiceViewMixin, cbv.MultiUpdateView):
+class PatientRecordAdmView(cbv.UpdateView):
     model = PatientRecord
-    forms_classes = {
-            'id': forms.CivilStatusForm,
-            'inscription': forms.InscriptionForm,
-            'out': forms.OutForm,
-            'family': forms.FamilyForm,
-            'transport': forms.TransportFrom,
-            'followup': forms.FollowUpForm,
-            }
+    form_class = forms.AdministrativeForm
     template_name = 'dossiers/patientrecord_tab2_fiche_adm.html'
     success_url = './view#tab=1'
 
-
     def get_context_data(self, **kwargs):
         ctx = super(PatientRecordAdmView, self).get_context_data(**kwargs)
         try:
-- 
2.0.1

