-#!/usr/bin/env python
+#!/usr/bin/python
# -*- coding: utf-8 -*-
# Debian-Depends: wcs
# Mise en place dans Apache :
# ScriptAlias /cgi-bin/list2form.py /usr/local/lib/list2form.py
+import time
import os
import csv
import cgi
except:
simplify = lambda s: 'url-name'
+today = time.strftime('%Y-%m-%d')
+
HTML_FORM = """<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Convertisseur de liste vers un formulaire w.c.s</title>
+ <style>
+body { margin: 0; padding: 0; background: #ffffff; }
+div#content { margin: 0; padding: 0.5em; width: 45em; background: #f0f0ff; }
+form { margin: 1px; }
+fieldset { margin-bottom: 1em; }
+div { margin-top: 0.5em; }
+ </style>
</head>
<body>
+ <div id="content">
<h1>Convertisseur de liste vers un formulaire w.c.s</h1>
- <form action="%(SCRIPT_NAME)s" method="POST" enctype="multipart/form-data">
- <table cellborder="0" cellpadding="5">
- <tr>
- <td>Fichier à convertir :<br />
- (format texte ou CSV, encodage UTF-8)</td>
- <td><input type="file" name="filename" /></td>
- </tr>
- <tr>
- <td>Supprimer la première ligne du fichier :</td>
- <td><input type="checkbox" name="delete_first" checked="on" /></td>
- </tr>
- <tr>
- <td>Nom du formulaire :</td>
- <td><input type="text" name="form_name" size="40" value="Etablissements origine AOR BAP 2011" /></td>
- </tr>
- <tr>
- <td>Nom du champ liste :</td>
- <td><select name="field_name">
- <option>Etablissement d'origine (membre de l'AUF)</option>
- <option>Etablissement d'accueil (membre de l'AUF)</option>
- </select></td>
- </tr>
- <tr>
- <td></td>
- <td><input type="submit" value="Convertir" /></td>
- </tr>
- </table>
+ <form action="%%(SCRIPT_NAME)s" method="POST" enctype="multipart/form-data">
+ <fieldset>
+ <legend>Source (fichier texte ou CSV à une colonne)</legend>
+ <div>
+ <label>Fichier à convertir :</label>
+ <input type="file" name="filename" required autofocus />
+ </div>
+ <div>
+ <label>Encodage du fichier à convertir :</label>
+ <select name="encoding">
+ <option value="iso-8859-1">Latin-1 / Europe occidentale (ISO-8859-1, utilisé par GDE)</option>
+ <option value="iso-8859-15">Latin-9 / Europe occidentale (ISO-8859-15/EURO)</option>
+ <option value="utf-8">Unicode (UTF-8)</option>
+ </select>
+ </div>
+ <div>
+ <label>Supprimer la première ligne du fichier :</label>
+ <input type="checkbox" name="delete_first" checked="on" />
+ </div>
+ </fieldset>
+ <fieldset>
+ <legend>Destination (formulaire WCS à importer)</legend>
+ <div>
+ <label>Titre du formulaire :</label>
+ <input type="text" name="form_name" size="50" value="Établissements membres TOUS/DRxxx %s" required />
+ </div>
+ <div>
+ <label>Nom du champ liste :</label>
+ <input type="text" name="field_name" size="50" value="Établissement (membre de l'AUF) :" required />
+ </div>
+ </fieldset>
+ <div style="text-align: right;">
+ <input name="convert" type="submit" value="Convertir" />
+ </div>
</form>
+ </div>
</body>
-</html>"""
+</html>""" % today
-WCS_FORM = """<?xml version="1.0" encoding="utf-8"?>
+WCS_FORM = """<?xml version="1.0" encoding="%(encoding)s"?>
<formdef>
<name>%(form_name)s</name>
<url_name>%(url_name)s</url_name>
<in_listing>True</in_listing>
<prefill>
<type>none</type>
- </prefill><wsf_prefill_explicit>False</wsf_prefill_explicit>
+ </prefill>
+ <wsf_prefill_explicit>False</wsf_prefill_explicit>
<items>
%(item_list)s
- </items><show_as_radio>False</show_as_radio>
+ </items>
+ <show_as_radio>False</show_as_radio>
</field>
</fields>
</formdef>"""
form = cgi.FieldStorage()
if form and form['filename'].type in ('text/plain','text/csv'):
+ encoding = form.getvalue('encoding')
delete_first = form.getvalue('delete_first')
conv_func = form['filename'].type.split('/')[-1] + '2list'
item_list = globals()[conv_func](form['filename'].file)
if delete_first:
item_list.pop(0)
- form_name = form.getvalue('form_name')
+ form_name = form.getvalue('form_name').decode('utf-8')
url_name = simplify(form_name)
- file_name = '%s.wcs' % url_name
+ file_name = u'%s.wcs' % url_name
+ field_name = form.getvalue('field_name').decode('utf-8')
info = {
- 'form_name': cgi.escape(form_name),
- 'url_name': cgi.escape(url_name),
- 'field_name': cgi.escape(form.getvalue('field_name')),
+ 'encoding': cgi.escape(encoding),
+ 'form_name': cgi.escape(form_name.encode(encoding)),
+ 'url_name': cgi.escape(url_name.encode(encoding)),
+ 'field_name': cgi.escape(field_name.encode(encoding)),
'item_list': '\n'.join([WCS_ITEM % cgi.escape(i) for i in item_list]),
}
#print "Location: https://preprod-formulaires.auf.org/admin/forms/import\r\n",