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