list2form : améliorations sur le code XML généré
[progfou.git] / wcs / list2form.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Debian-Depends: wcs
4 # Mise en place dans Apache :
5 # ScriptAlias /cgi-bin/list2form.py /usr/local/lib/list2form.py
6 import time
7 import os
8 import csv
9 import cgi
10 import cgitb
11 cgitb.enable(display=0, logdir="/tmp")
12
13 try:
14 from wcs.qommon.misc import simplify
15 except:
16 simplify = lambda s: 'url-name'
17
18 today = time.strftime('%Y-%m-%d')
19
20 HTML_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>
24 <style>
25 body { margin: 0; padding: 0; background: #ffffff; }
26 div#content { margin: 0; padding: 0.5em; width: 45em; background: #f0f0ff; }
27 form { margin: 1px; }
28 fieldset { margin-bottom: 1em; }
29 div { margin-top: 0.5em; }
30 </style>
31 </head>
32 <body>
33 <div id="content">
34 <h1>Convertisseur de liste vers un formulaire w.c.s</h1>
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" />
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>
69 </form>
70 </div>
71 </body>
72 </html>""" % today
73
74 WCS_FORM = """<?xml version="1.0" encoding="%(encoding)s"?>
75 <formdef>
76 <name>%(form_name)s</name>
77 <url_name>%(url_name)s</url_name>
78 <category category_id="1">Pour importation</category>
79 <workflow workflow_id="_default">Par d&#233;faut</workflow>
80 <detailed_emails>true</detailed_emails>
81 <disabled>false</disabled>
82 <only_allow_one>true</only_allow_one>
83 <allow_drafts>false</allow_drafts>
84 <discussion>false</discussion>
85 <enable_tracking_codes>false</enable_tracking_codes>
86 <always_advertise>false</always_advertise>
87 <private_status_and_history>true</private_status_and_history>
88 <confirmation>true</confirmation>
89 <signing>false</signing>
90 <max_field_id>1</max_field_id>
91 <fields>
92 <field>
93 <label>%(field_name)s</label>
94 <type>item</type>
95 <required>True</required>
96 <hint>Veuillez faire un choix dans la liste...</hint>
97 <in_listing>True</in_listing>
98 <prefill>
99 <type>none</type>
100 </prefill>
101 <wsf_prefill_explicit>False</wsf_prefill_explicit>
102 <items>
103 %(item_list)s
104 </items>
105 <show_as_radio>False</show_as_radio>
106 <id>0</id>
107 </field>
108 </fields>
109 <options />
110 </formdef>"""
111
112 WCS_ITEM = """ <item>%s</item>"""
113
114 def plain2list(fd):
115 item_list = []
116 for l in fd.readlines():
117 d = l.strip()
118 if d != '':
119 item_list.append(d)
120 return item_list
121
122 def csv2list(fd):
123 item_list = []
124 for l in csv.reader(fd):
125 if not l:
126 continue
127 d = l[0].strip()
128 if d != '':
129 item_list.append(d)
130 return item_list
131
132 form = cgi.FieldStorage()
133 if form and form['filename'].type in ('text/plain','text/csv'):
134 encoding = form.getvalue('encoding')
135 delete_first = form.getvalue('delete_first')
136 conv_func = form['filename'].type.split('/')[-1] + '2list'
137 item_list = globals()[conv_func](form['filename'].file)
138 if delete_first:
139 item_list.pop(0)
140 form_name = form.getvalue('form_name').decode('utf-8')
141 url_name = simplify(form_name)
142 file_name = u'%s.wcs' % url_name
143 field_name = form.getvalue('field_name').decode('utf-8')
144 info = {
145 'encoding': cgi.escape(encoding),
146 'form_name': cgi.escape(form_name.encode(encoding)),
147 'url_name': cgi.escape(url_name.encode(encoding)),
148 'field_name': cgi.escape(field_name.encode(encoding)),
149 'item_list': '\n'.join([WCS_ITEM % cgi.escape(i) for i in item_list]),
150 }
151 #print "Location: https://preprod-formulaires.auf.org/admin/forms/import\r\n",
152 print "Content-Type: application/x-wcs-form; name=\"%s\"\r\n" % file_name,
153 print "Content-Disposition: attachment; filename=\"%s\"\r\n\r\n" % file_name,
154 print WCS_FORM % info,
155 else:
156 print "Content-Type: text/html; charset=utf-8\r\n\r\n",
157 print HTML_FORM % os.environ,
158