2 # -*- coding: utf-8 -*-
4 # Mise en place dans Apache :
5 # ScriptAlias /cgi-bin/list2form.py /usr/local/lib/list2form.py
7 import sys
# exit brutal sur erreur
13 from exceptions
import UnicodeDecodeError, UnicodeEncodeError
14 cgitb
.enable(display
=0, logdir
="/tmp")
17 from wcs
.qommon
.misc
import simplify
19 simplify
= lambda s
: 'url-name'
21 today
= time
.strftime('%Y-%m-%d')
25 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
26 <title>Convertisseur de liste vers un formulaire w.c.s</title>
28 body { margin: 0; padding: 0; background: #ffffff; }
29 div#content { margin: 0; padding: 0.5em; width: 45em; background: #f0f0ff; }
31 fieldset { margin-bottom: 1em; }
32 div { margin-top: 0.5em; }
37 <h1>Convertisseur de liste vers un formulaire w.c.s</h1>
38 <form action="%%(SCRIPT_NAME)s" method="POST" enctype="multipart/form-data">
40 <legend>Source (fichier texte ou CSV à une colonne)</legend>
42 <label>Fichier à convertir :</label>
43 <input type="file" name="filename" required autofocus />
46 <label>Encodage du fichier à convertir :</label>
47 <select name="encoding">
48 <option value="iso-8859-1">Latin-1 / Europe occidentale (ISO-8859-1, utilisé par GDE)</option>
49 <option value="iso-8859-15">Latin-9 / Europe occidentale (ISO-8859-15/EURO)</option>
50 <option value="utf-8-sig">Unicode (UTF-8)</option>
51 <option value="utf-16">Unicode (UTF-16)</option>
55 <label>Supprimer la première ligne du fichier :</label>
56 <input type="checkbox" name="delete_first" />
60 <legend>Destination (formulaire WCS à importer)</legend>
62 <label>Titre du formulaire :</label>
63 <input type="text" name="form_name" size="50" value="Établissements membres TOUS/DRxxx %s" required />
66 <label>Nom du champ liste :</label>
67 <input type="text" name="field_name" size="50" value="Établissement (membre de l'AUF) :" required />
70 <div style="text-align: right;">
71 <input name="convert" type="submit" value="Convertir" />
78 HTML_ERROR
= """<html>
80 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
81 <title>Convertisseur de liste vers un formulaire w.c.s</title>
83 body { margin: 0; padding: 0; background: #ffffff; }
84 div#content { margin: 0; padding: 0.5em; width: 45em; background: #f0f0ff; }
85 div { margin-top: 0.5em; }
90 <h1>Erreur de conversion :</h1>
95 <code>%(details)s</code>
101 WCS_FORM
= """<?xml version="1.0" encoding="%(encoding)s"?>
103 <name>%(form_name)s</name>
104 <url_name>%(url_name)s</url_name>
105 <category category_id="1">Pour importation</category>
106 <workflow workflow_id="_default">Par défaut</workflow>
107 <detailed_emails>true</detailed_emails>
108 <disabled>false</disabled>
109 <only_allow_one>true</only_allow_one>
110 <allow_drafts>false</allow_drafts>
111 <discussion>false</discussion>
112 <enable_tracking_codes>false</enable_tracking_codes>
113 <always_advertise>false</always_advertise>
114 <private_status_and_history>true</private_status_and_history>
115 <confirmation>true</confirmation>
116 <signing>false</signing>
117 <max_field_id>1</max_field_id>
120 <label>%(field_name)s</label>
122 <required>True</required>
123 <hint>Veuillez faire un choix dans la liste...</hint>
124 <in_listing>True</in_listing>
128 <wsf_prefill_explicit>False</wsf_prefill_explicit>
132 <show_as_radio>False</show_as_radio>
139 WCS_ITEM
= """ <item>%s</item>"""
143 for l
in fd
.readlines():
149 def utf8_encoder(unicode_stream
):
150 for line
in unicode_stream
:
151 yield line
.encode('utf-8')
153 def unicode_csv_reader(unicode_stream
, **kwargs
):
154 # csv.py doesn't do Unicode; encode temporarily as UTF-8:
155 for row
in csv
.reader(utf8_encoder(unicode_stream
), **kwargs
):
156 # decode UTF-8 back to Unicode, cell by cell:
157 yield [unicode(cell
, 'utf-8') for cell
in row
]
161 for l
in unicode_csv_reader(fd
):
169 form
= cgi
.FieldStorage()
170 if form
and form
['filename'].type in ('text/plain','text/csv'):
171 encoding
= form
.getvalue('encoding')
172 delete_first
= form
.getvalue('delete_first')
173 conv_func
= form
['filename'].type.split('/')[-1] + '2list'
174 # file field is not always a file...
175 if hasattr(form
['filename'].file, 'fileno'):
176 item_fd
= form
['filename'].file.fileno()
179 tmpfile
= tempfile
.TemporaryFile('rw+b')
180 tmpfile
.write(form
['filename'].file.read())
182 item_fd
= tmpfile
.fileno()
183 item_file
= io
.open(item_fd
, 'rt', encoding
=encoding
)
185 item_list
= globals()[conv_func
](item_file
)
186 except (UnicodeDecodeError, UnicodeEncodeError) as e
:
187 print "Content-Type: text/html; charset=utf-8\r\n\r\n",
189 'message': "Erreur de décodage du fichier. Vérifiez l'encodage choisi pour le fichier.",
195 form_name
= form
.getvalue('form_name').decode('utf-8')
196 url_name
= simplify(form_name
)
197 file_name
= u
'%s.wcs' % url_name
198 field_name
= form
.getvalue('field_name').decode('utf-8')
199 # conversion systématique en UTF-8
202 'encoding': cgi
.escape(encoding
),
203 'form_name': cgi
.escape(form_name
.encode(encoding
)),
204 'url_name': cgi
.escape(url_name
.encode(encoding
)),
205 'field_name': cgi
.escape(field_name
.encode(encoding
)),
206 'item_list': '\n'.join([WCS_ITEM
% cgi
.escape(i
.encode(encoding
)) for i
in item_list
]),
208 #print "Location: https://preprod-formulaires.auf.org/admin/forms/import\r\n",
209 print "Content-Type: application/x-wcs-form; name=\"%s\"\r\n" % file_name
,
210 print "Content-Disposition: attachment; filename=\"%s\"\r\n\r\n" % file_name
,
211 print WCS_FORM
% info
,
213 print "Content-Type: text/html; charset=utf-8\r\n\r\n",
214 print HTML_FORM
% os
.environ
,