Browse Source

Show the entered values when the form is displayed a second time and use bottle templates system.

Jocelyn Delande 10 years ago
parent
commit
2e02aa3ef5
2 changed files with 105 additions and 66 deletions
  1. 35 21
      backend.py
  2. 70 45
      index.html

+ 35 - 21
backend.py

@@ -3,53 +3,67 @@
 import os
 from os.path import join, dirname
 
-from bottle import route, run, static_file, request
-
-form_names = {
-    'name'        : 'Nom/Pseudo',
-    'contrib-type': 'Type de participation',
-    'latitude'    : 'Localisation',
-    'longitude'   : 'Localisation',
-    'phone'       : 'Téléphone',
-    'email'       : 'Email',
-    'access-type' : 'Type de connexion',
-    'bandwidth'   : 'Bande passante',
-    'share-part'  : 'Débit partagé',
-}
+from bottle import route, run, static_file, request, template, FormsDict
+
+ORIENTATIONS = (
+    ('N', 'Nord'),
+    ('NO', 'Nord-Ouest'),
+    ('O', 'Ouest'),
+    ('SO', 'Sud-Ouest'),
+    ('S', 'Sud'),
+    ('SE', 'Sud-Est'),
+    ('E', 'Est'),
+    ('NE', 'Nord-Est'),
+)
 
 @route('/wifi-form')
 def show_wifi_form():
-    return open('index.html').read()
+    return template('wifi-form', errors=None, data = FormsDict(),
+                    orientations=ORIENTATIONS)
 
 @route('/wifi-form', method='POST')
 def submit_wifi_form():
     required = ('name', 'contrib-type',
-                'latitude', 'longitude', 'orientation')
+                'latitude', 'longitude')
     required_or = (('email', 'phone'),)
     required_if = (
         ('contrib-type', 'share',('access-type', 'bandwidth',
                                     'share-part')),
     )
 
-    errors = []
+    field_names = {
+        'name'        : 'Nom/Pseudo',
+        'contrib-type': 'Type de participation',
+        'latitude'    : 'Localisation',
+        'longitude'   : 'Localisation',
+        'phone'       : 'Téléphone',
+        'email'       : 'Email',
+        'access-type' : 'Type de connexion',
+        'bandwidth'   : 'Bande passante',
+        'share-part'  : 'Débit partagé',
+    }
 
+    errors = []
     for name in required:
         if (not request.forms.get(name)):
-            errors.append((name, 'Ce champ est requis'))
+            errors.append((field_names[name], 'ce champ est requis'))
 
     for name_list in required_or:
         filleds = [True for name in name_list if request.forms.get(name)]
         if len(filleds) <= 0:
-            errors.append((name_list,
-                           'Au moins un des de ces champs est requis'))
+            errors.append((
+                    ' ou '.join([field_names[i] for i in name_list]),
+                    'au moins un des de ces champs est requis'))
 
     for key, value, fields  in required_if:
         if request.forms.get('key') == value:
             for name in fields:
                 if not request.forms.get(name):
-                    errors.append((name, 'Ce champ est requis'))
+                    errors.append(
+                        (field_names[name], 'ce champ est requis'))
     if errors:
-        return str(errors)
+        return template('wifi-form', errors=errors, data=request.forms,
+                        orientations=ORIENTATIONS)
     else:
         return 'OK'
 

+ 70 - 45
index.html

@@ -23,7 +23,20 @@
 <body>
   <header class="jumbotron">
     <div class="container">
-    <h1>Réseau wifi expérimental </h1>
+    <h1>Réseau wifi expérimental</h1>
+%if errors:
+
+<p>
+  Veuillez corriger les <span class="label label-danger">erreurs</span>
+  suivantes :
+</p>
+<ul class="bg-warning">
+%for field, err in set(errors):
+  <li><strong>{{field}}</strong> : {{err}}</li>
+%end
+</ul>
+
+%else:
     <p>
 L'association <a href="http://faimaison.net">FAIMaison</a> expérimente à
 grande échelle (Nantes et environs) la création d'un réseau sans-fil à
@@ -42,7 +55,7 @@ Renseigner ce formulaire nous permet de définir quelles <strong>zones
 d'expérimentations</strong> (avec une grande densité de volontaires)
 pourraient-être intéressantes.
       </p>
-
+%end
     </div>
   </header>
 
@@ -53,7 +66,8 @@ pourraient-être intéressantes.
 
     <div class="form-group">
     <label for="name">Nom / Pseudo</label>
-    <input name="name" id="name" type="text" class="form-control"/>
+    <input name="name" value="{{data.get('name', '')}}"
+           id="name" type="text" class="form-control"/>
     </div>
 
     <p class="help-block">
@@ -62,23 +76,29 @@ Un moyen de contact au moins est nécessaire
     <p>
       <div class="form-group">
         <label for="email">Email</label>
-        <input name="email" id="email" type="email" class="form-control">
+        <input name="email" value="{{data.get('email', '')}}"
+               id="email" type="email" class="form-control">
       </div>
       <div class="form-group">
         <label for="phone">Téléphone</label>
-        <input name="phone" id="phone" type="tel" class="form-control"/>
+        <input name="phone" value="{{data.get('phone', '')}}"
+               id="phone" type="tel" class="form-control"/>
       </div>
     </p>
 
     <h2>Je souhaite</h2>
     <p class="radio">
       <label>
-      <input type="radio" name="contrib-type" value="share" /> Partager une partie de ma connexion
+      <input type="radio" name="contrib-type" value="share"
+             {{'checked' if data.get('contrib-type') == 'share' else ''}}/>
+      Partager une partie de ma connexion
       </label>
     </p>
     <p class="radio">
       <label>
-      <input type="radio" name="contrib-type" value="connect" /> Me raccorder au réseau expérimental
+      <input type="radio" name="contrib-type" value="connect"
+             {{'checked' if data.get('contrib-type') == 'connect' else ''}}/>
+      Me raccorder au réseau expérimental
       </label>
     </p>
 
@@ -86,17 +106,25 @@ Un moyen de contact au moins est nécessaire
     <h2>Partager une connexion</h2>
     <h3>Type de connexion</h3>
     <p class="radio"><label>
-      <input type="radio" name="access-type" value="fiber"/>
+      <input {{'checked' if data.get('access-type') == 'fiber' else ''}}
+         type="radio" name="access-type" value="fiber"/>
       Fibre
     </label></p>
     <p class="radio"><label>
-      <input type="radio" name="access-type" value="vdsl"/>
+      <input {{'checked' if data.get('access-type') == 'vdsl' else ''}}
+      type="radio" name="access-type" value="vdsl"/>
       VDSL
     </label></p>
     <p class="radio"><label>
-      <input type="radio" name="access-type" value="adsl"/>
+      <input {{'checked' if data.get('access-type') == 'adsl' else ''}}
+             type="radio" name="access-type" value="adsl"/>
       ADSL
     </label></p>
+    <p class="radio"><label>
+      <input {{'checked' if data.get('access-type') == 'cable' else ''}}
+             type="radio" name="access-type" value="cable"/>
+      Câble
+    </label></p>
     <h3>Débits</h3>
     <p class="help-block">
       Il est possible de limiter techniquement la quantité de bande passante
@@ -105,11 +133,13 @@ Un moyen de contact au moins est nécessaire
     </p>
     <p>
       <label for="bandwidth">Débit total (Mbps)</label>
-      <input name="bandwidth" id="bandwidth" type="number" min="0" class="form-control"/>
+      <input name="bandwidth" value="{{data.get('bandwidth', '')}}"
+             id="bandwidth" type="number" min="0" class="form-control"/>
     </p>
     <p>
       <label for="share-part">Je souhaite partager (au max.) (Mbps)</label>
-      <input name="share-part" id="share-part" type="number" min="0" class="form-control"/>
+      <input name="share-part" value="{{data.get('share-part', '')}}"
+             id="share-part" type="number" min="0" class="form-control"/>
     </p>
     </div>
 
@@ -121,14 +151,17 @@ Un moyen de contact au moins est nécessaire
       </div>
       <div class="form-group col-sm-6">
         <div class="form-group form-group-lg form-inline">
-          <input type="text" name="search" id="search" placeholder="rue du calvaire, nantes" class="form-control" />
+          <input type="text" name="search"
+                 id="search" placeholder="rue du calvaire, nantes" class="form-control" />
           <span id="search-btn" class="btn btn-default btn-lg" data-loading-text="...">Recherche</span>
 
           <div id="search-results" class=""></div>
           <p class="help-block">Déplacer le marqueur pour pointer précisément le bâtiment au besoin</p>
         </div>
-        <input type="hidden" name="latitude" id="latitude" value="" />
-        <input type="hidden" name="longitude" id="longitude" value=""  />
+        <input name="latitude" value="{{data.get('latitude', '')}}"
+               type="hidden" id="latitude" />
+        <input name="longitude" value="{{data.get('longitude', '')}}"
+               type="hidden"  id="longitude" />
         </div>
       </div>
     </div>
@@ -137,45 +170,30 @@ Un moyen de contact au moins est nécessaire
 
     <div class="form-group">
     <label for="orientation" />Orientation(s) de mes fenêtres</label><br />
+%for val, label in orientations:
     <label class="checkbox-inline">
-      <input type="checkbox" name="orientation" value="N">Nord
-    </label>
-    <label class="checkbox-inline">
-      <input type="checkbox" name="orientation" value="NO" class="checkbox-inline">Nord-Ouest<br />
-    </label>
-    <label class="checkbox-inline">
-      <input type="checkbox" name="orientation" value="O" class="checkbox-inline">Ouest<br />
-    </label>
-    <label class="checkbox-inline">
-      <input type="checkbox" name="orientation" value="SO" class="checkbox-inline">Sud-Ouest<br />
-    </label>
-    <label class="checkbox-inline">
-      <input type="checkbox" name="orientation" value="S" class="checkbox-inline">Sud<br>
-    </label>
-    <label class="checkbox-inline">
-      <input type="checkbox" name="orientation" value="SE" class="checkbox-inline">Sud-Est<br>
-    </label>
-    <label class="checkbox-inline">
-      <input type="checkbox" name="orientation" value="E" class="checkbox-inline">Est<br>
-    </label>
-    <label class="checkbox-inline">
-      <input type="checkbox" name="orientation" value="NE" class="checkbox-inline">Nord-Est<br>
+      <input type="checkbox" name="orientation" value="{{val}}"
+             {{'checked' if val in data.getall('orientation') else ''}}/>
+      {{label}}
     </label>
+%end
     </div>
 
     <div class="form-group">
         <label for="roof">Je peux accéder à mon toît
-          <input name="roof" type="checkbox" />
+          <input name="roof" {{'checked' if data.get('roof', False) else ''}}
+                 type="checkbox"/>
         </label>
     </div>
 
     <p>
       <label for="floor">Étage</label>
-      <input name="floor" id="floor" type="number" class="form-control" placeholder="Indiquer  « 0 » pour le Rez-de-chaussée"/>
+      <input name="floor" value="{{data.get('floor', '')}}"
+             id="floor" type="number" class="form-control" placeholder="Indiquer  « 0 » pour le Rez-de-chaussée"/>
     </p>
 
     <h2>Remarque/commentaire</h2>
-    <textarea name="comment" class="form-control" row="5"></textarea>
+    <textarea name="comment" class="form-control" row="5">{{data.get('comment', '').strip()}}</textarea>
 
     <h2>Mes données</h2>
 
@@ -194,24 +212,31 @@ J'autorise qu'apparaissent sur la carte publique :
     </div>
     <div class="checkbox">
       <label>
-        <input type="checkbox" name="privacy" checked value="coordinates"/>
+        <input type="checkbox" name="privacy" value="coordinates"
+        {{'checked' if (('coordinates' in data.getall('privacy')) or not data) else ''}} />
         Mes coordonnées GPS
       </label>
     </div>
     <div class="checkbox">
       <label>
-        <input type="checkbox" name="privacy" checked value="place_details"/>
+        <input type="checkbox" name="privacy" value="place_details"
+        {{'checked' if (('place_details' in data.getall('privacy')) or not data) else ''}}/>
+
         Mon étage et mes orientations
       </label>
     </div>
     <div class="checkbox">
       <label>
-        <input type="checkbox" name="privacy" value="login"/> Mon nom/pseudo
+        <input type="checkbox" name="privacy" value="name"
+        {{'checked' if 'name' in data.getall('privacy') else ''}}/>
+        Mon nom/pseudo
       </label>
     </div>
     <div class="checkbox">
       <label>
-        <input type="checkbox" name="privacy" value="comment"/> Mon commentaire
+        <input type="checkbox" name="privacy" value="comment"
+        {{'checked' if 'comment' in data.getall('privacy') else ''}}/>
+        Mon commentaire
       </label>
     </div>
     <input type="submit" value="Envoyer" class="btn btn-primary btn-lg"/>
@@ -236,4 +261,4 @@ J'autorise qu'apparaissent sur la carte publique :
 </div>
 
 </body>
-</html>
+</html>