w.c.s : petit outil dédié pour la conversion d'une liste en formulaire
authorProgfou <jean-christophe.andre@auf.org>
Fri, 25 Feb 2011 18:15:52 +0000 (01:15 +0700)
committerProgfou <jean-christophe.andre@auf.org>
Fri, 25 Feb 2011 18:15:52 +0000 (01:15 +0700)
wcs/list2form.py [new file with mode: 0755]

diff --git a/wcs/list2form.py b/wcs/list2form.py
new file mode 100755 (executable)
index 0000000..7e3ca7e
--- /dev/null
@@ -0,0 +1,116 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+import os
+import csv
+import cgi
+import cgitb
+cgitb.enable(display=0, logdir="/tmp")
+
+try:
+    from qommon.misc import simplify
+except:
+    simplify = lambda s: 'url-name'
+
+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>
+</head>
+<body>
+  <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 &agrave; convertir&nbsp;:<br />
+        (format texte ou CSV, encodage UTF-8)</td>
+    <td><input type="file" name="filename" /></td>
+  </tr>
+  <tr>
+    <td>Supprimer la premi&egrave;re ligne du fichier&nbsp;:</td>
+    <td><input type="checkbox" name="delete_first" checked="on" /></td>
+  </tr>
+  <tr>
+    <td>Nom du formulaire&nbsp;:</td>
+    <td><input type="text" name="form_name" size="40" value="Etablissements origine AOR BAP 2011" /></td>
+  </tr>
+  <tr>
+    <td>Nom du champ liste&nbsp;:</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>
+</body>
+</html>"""
+
+WCS_FORM = """<?xml version="1.0" encoding="utf-8"?>
+<formdef>
+  <name>%(form_name)s</name>
+  <url_name>%(url_name)s</url_name>
+  <category>Pour importation</category>
+  <only_allow_one>true</only_allow_one>
+  <allow_drafts>false</allow_drafts>
+  <discussion>false</discussion>
+  <confirmation>true</confirmation>
+  <signing>false</signing>
+  <fields>
+    <field>
+      <label>%(field_name)s</label>
+      <type>item</type>
+      <required>True</required>
+      <hint>Veuillez faire un choix dans la liste...</hint>
+      <in_listing>True</in_listing>
+      <prefill>
+        <type>none</type>
+      </prefill><wsf_prefill_explicit>False</wsf_prefill_explicit>
+      <items>
+%(item_list)s
+      </items><show_as_radio>False</show_as_radio>
+    </field>
+  </fields>
+</formdef>"""
+
+WCS_ITEM = """        <item>%s</item>"""
+
+def plain2list(fd):
+    item_list = []
+    for l in fd.readlines():
+        item_list.append(l.strip())
+    return item_list
+
+def csv2list(fd):
+    item_list = []
+    for l in csv.reader(fd):
+        item_list.append(l[0])
+    return item_list
+
+form = cgi.FieldStorage()
+if form and form['filename'].type in ('text/plain','text/csv'):
+    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')
+    url_name = simplify(form_name)
+    file_name = '%s.wcs' % url_name
+    info = {
+        'form_name': cgi.escape(form_name),
+        'url_name': cgi.escape(url_name),
+        'field_name': cgi.escape(form.getvalue('field_name')),
+        '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",
+    print "Content-Type: application/x-wcs-form; name=\"%s\"\r\n" % file_name,
+    print "Content-Disposition: attachment; filename=\"%s\"\r\n\r\n" % file_name,
+    print WCS_FORM % info,
+else:
+    print "Content-Type: text/html; charset=utf-8\r\n\r\n",
+    print HTML_FORM % os.environ,
+