list2form : choix de l'encodage, améliorations cosmétiques
[progfou.git] / wcs / list2form.py
CommitLineData
4ae2d14d 1#!/usr/bin/python
252760d2 2# -*- coding: utf-8 -*-
5602202b
P
3# Debian-Depends: wcs
4# Mise en place dans Apache :
5# ScriptAlias /cgi-bin/list2form.py /usr/local/lib/list2form.py
4ae2d14d 6import time
252760d2
P
7import os
8import csv
9import cgi
10import cgitb
11cgitb.enable(display=0, logdir="/tmp")
12
13try:
5602202b 14 from wcs.qommon.misc import simplify
252760d2
P
15except:
16 simplify = lambda s: 'url-name'
17
4ae2d14d
P
18today = time.strftime('%Y-%m-%d')
19
252760d2
P
20HTML_FORM = """<html>
21<head>
22 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
23 <title>Convertisseur de liste vers un formulaire w.c.s</title>
4ae2d14d
P
24 <style>
25body { margin: 0; padding: 0; background: #ffffff; }
26div#content { margin: 0; padding: 0.5em; width: 45em; background: #f0f0ff; }
27form { margin: 1px; }
28fieldset { margin-bottom: 1em; }
29div { margin-top: 0.5em; }
30 </style>
252760d2
P
31</head>
32<body>
4ae2d14d 33 <div id="content">
252760d2 34 <h1>Convertisseur de liste vers un formulaire w.c.s</h1>
4ae2d14d
P
35 <form action="%%(SCRIPT_NAME)s" method="POST" enctype="multipart/form-data">
36 <fieldset>
37 <legend>Source (fichier texte ou CSV à une colonne)</legend>
38 <div>
39 <label>Fichier &agrave; convertir&nbsp;:</label>
40 <input type="file" name="filename" required autofocus />
41 </div>
42 <div>
43 <label>Encodage du fichier &agrave; convertir&nbsp;:</label>
44 <select name="encoding">
45 <option value="iso-8859-1">Latin-1 / Europe occidentale (ISO-8859-1, utilisé par GDE)</option>
46 <option value="iso-8859-15">Latin-9 / Europe occidentale (ISO-8859-15/EURO)</option>
47 <option value="utf-8">Unicode (UTF-8)</option>
48 </select>
49 </div>
50 <div>
51 <label>Supprimer la premi&egrave;re ligne du fichier&nbsp;:</label>
52 <input type="checkbox" name="delete_first" checked="on" />
53 </div>
54 </fieldset>
55 <fieldset>
56 <legend>Destination (formulaire WCS à importer)</legend>
57 <div>
58 <label>Titre du formulaire&nbsp;:</label>
59 <input type="text" name="form_name" size="50" value="Établissements membres TOUS/DRxxx %s" required />
60 </div>
61 <div>
62 <label>Nom du champ liste&nbsp;:</label>
63 <input type="text" name="field_name" size="50" value="Établissement (membre de l'AUF) :" required />
64 </div>
65 </fieldset>
66 <div style="text-align: right;">
67 <input name="convert" type="submit" value="Convertir" />
68 </div>
252760d2 69 </form>
4ae2d14d 70 </div>
252760d2 71</body>
4ae2d14d 72</html>""" % today
252760d2 73
4ae2d14d 74WCS_FORM = """<?xml version="1.0" encoding="%(encoding)s"?>
252760d2
P
75<formdef>
76 <name>%(form_name)s</name>
77 <url_name>%(url_name)s</url_name>
78 <category>Pour importation</category>
79 <only_allow_one>true</only_allow_one>
80 <allow_drafts>false</allow_drafts>
81 <discussion>false</discussion>
82 <confirmation>true</confirmation>
83 <signing>false</signing>
84 <fields>
85 <field>
86 <label>%(field_name)s</label>
87 <type>item</type>
88 <required>True</required>
89 <hint>Veuillez faire un choix dans la liste...</hint>
90 <in_listing>True</in_listing>
91 <prefill>
92 <type>none</type>
4ae2d14d
P
93 </prefill>
94 <wsf_prefill_explicit>False</wsf_prefill_explicit>
252760d2
P
95 <items>
96%(item_list)s
4ae2d14d
P
97 </items>
98 <show_as_radio>False</show_as_radio>
252760d2
P
99 </field>
100 </fields>
101</formdef>"""
102
103WCS_ITEM = """ <item>%s</item>"""
104
105def plain2list(fd):
106 item_list = []
107 for l in fd.readlines():
e4fdfe59
P
108 d = l.strip()
109 if d != '':
110 item_list.append(d)
252760d2
P
111 return item_list
112
113def csv2list(fd):
114 item_list = []
115 for l in csv.reader(fd):
e5a1703d
P
116 if not l:
117 continue
e4fdfe59
P
118 d = l[0].strip()
119 if d != '':
120 item_list.append(d)
252760d2
P
121 return item_list
122
123form = cgi.FieldStorage()
124if form and form['filename'].type in ('text/plain','text/csv'):
4ae2d14d 125 encoding = form.getvalue('encoding')
252760d2
P
126 delete_first = form.getvalue('delete_first')
127 conv_func = form['filename'].type.split('/')[-1] + '2list'
128 item_list = globals()[conv_func](form['filename'].file)
129 if delete_first:
130 item_list.pop(0)
4ae2d14d 131 form_name = form.getvalue('form_name').decode('utf-8')
252760d2 132 url_name = simplify(form_name)
4ae2d14d
P
133 file_name = u'%s.wcs' % url_name
134 field_name = form.getvalue('field_name').decode('utf-8')
252760d2 135 info = {
4ae2d14d
P
136 'encoding': cgi.escape(encoding),
137 'form_name': cgi.escape(form_name.encode(encoding)),
138 'url_name': cgi.escape(url_name.encode(encoding)),
139 'field_name': cgi.escape(field_name.encode(encoding)),
252760d2
P
140 'item_list': '\n'.join([WCS_ITEM % cgi.escape(i) for i in item_list]),
141 }
142 #print "Location: https://preprod-formulaires.auf.org/admin/forms/import\r\n",
143 print "Content-Type: application/x-wcs-form; name=\"%s\"\r\n" % file_name,
144 print "Content-Disposition: attachment; filename=\"%s\"\r\n\r\n" % file_name,
145 print WCS_FORM % info,
146else:
147 print "Content-Type: text/html; charset=utf-8\r\n\r\n",
148 print HTML_FORM % os.environ,
149