wcs : gestion des champs de type date non remplis.
[progfou.git] / wcs / list2form.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 import os
4 import csv
5 import cgi
6 import cgitb
7 cgitb.enable(display=0, logdir="/tmp")
8
9 try:
10 from qommon.misc import simplify
11 except:
12 simplify = lambda s: 'url-name'
13
14 HTML_FORM = """<html>
15 <head>
16 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
17 <title>Convertisseur de liste vers un formulaire w.c.s</title>
18 </head>
19 <body>
20 <h1>Convertisseur de liste vers un formulaire w.c.s</h1>
21 <form action="%(SCRIPT_NAME)s" method="POST" enctype="multipart/form-data">
22 <table cellborder="0" cellpadding="5">
23 <tr>
24 <td>Fichier &agrave; convertir&nbsp;:<br />
25 (format texte ou CSV, encodage UTF-8)</td>
26 <td><input type="file" name="filename" /></td>
27 </tr>
28 <tr>
29 <td>Supprimer la premi&egrave;re ligne du fichier&nbsp;:</td>
30 <td><input type="checkbox" name="delete_first" checked="on" /></td>
31 </tr>
32 <tr>
33 <td>Nom du formulaire&nbsp;:</td>
34 <td><input type="text" name="form_name" size="40" value="Etablissements origine AOR BAP 2011" /></td>
35 </tr>
36 <tr>
37 <td>Nom du champ liste&nbsp;:</td>
38 <td><select name="field_name">
39 <option>Etablissement d'origine (membre de l'AUF)</option>
40 <option>Etablissement d'accueil (membre de l'AUF)</option>
41 </select></td>
42 </tr>
43 <tr>
44 <td></td>
45 <td><input type="submit" value="Convertir" /></td>
46 </tr>
47 </table>
48 </form>
49 </body>
50 </html>"""
51
52 WCS_FORM = """<?xml version="1.0" encoding="utf-8"?>
53 <formdef>
54 <name>%(form_name)s</name>
55 <url_name>%(url_name)s</url_name>
56 <category>Pour importation</category>
57 <only_allow_one>true</only_allow_one>
58 <allow_drafts>false</allow_drafts>
59 <discussion>false</discussion>
60 <confirmation>true</confirmation>
61 <signing>false</signing>
62 <fields>
63 <field>
64 <label>%(field_name)s</label>
65 <type>item</type>
66 <required>True</required>
67 <hint>Veuillez faire un choix dans la liste...</hint>
68 <in_listing>True</in_listing>
69 <prefill>
70 <type>none</type>
71 </prefill><wsf_prefill_explicit>False</wsf_prefill_explicit>
72 <items>
73 %(item_list)s
74 </items><show_as_radio>False</show_as_radio>
75 </field>
76 </fields>
77 </formdef>"""
78
79 WCS_ITEM = """ <item>%s</item>"""
80
81 def plain2list(fd):
82 item_list = []
83 for l in fd.readlines():
84 item_list.append(l.strip())
85 return item_list
86
87 def csv2list(fd):
88 item_list = []
89 for l in csv.reader(fd):
90 item_list.append(l[0])
91 return item_list
92
93 form = cgi.FieldStorage()
94 if form and form['filename'].type in ('text/plain','text/csv'):
95 delete_first = form.getvalue('delete_first')
96 conv_func = form['filename'].type.split('/')[-1] + '2list'
97 item_list = globals()[conv_func](form['filename'].file)
98 if delete_first:
99 item_list.pop(0)
100 form_name = form.getvalue('form_name')
101 url_name = simplify(form_name)
102 file_name = '%s.wcs' % url_name
103 info = {
104 'form_name': cgi.escape(form_name),
105 'url_name': cgi.escape(url_name),
106 'field_name': cgi.escape(form.getvalue('field_name')),
107 'item_list': '\n'.join([WCS_ITEM % cgi.escape(i) for i in item_list]),
108 }
109 #print "Location: https://preprod-formulaires.auf.org/admin/forms/import\r\n",
110 print "Content-Type: application/x-wcs-form; name=\"%s\"\r\n" % file_name,
111 print "Content-Disposition: attachment; filename=\"%s\"\r\n\r\n" % file_name,
112 print WCS_FORM % info,
113 else:
114 print "Content-Type: text/html; charset=utf-8\r\n\r\n",
115 print HTML_FORM % os.environ,
116