export4kaboom : gestion temporaire du statut d'observateur (bis)
[progfou.git] / wcs / list2form.py
CommitLineData
252760d2
P
1#!/usr/bin/env python
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
252760d2
P
6import os
7import csv
8import cgi
9import cgitb
10cgitb.enable(display=0, logdir="/tmp")
11
12try:
5602202b 13 from wcs.qommon.misc import simplify
252760d2
P
14except:
15 simplify = lambda s: 'url-name'
16
17HTML_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
55WCS_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
82WCS_ITEM = """ <item>%s</item>"""
83
84def plain2list(fd):
85 item_list = []
86 for l in fd.readlines():
e4fdfe59
P
87 d = l.strip()
88 if d != '':
89 item_list.append(d)
252760d2
P
90 return item_list
91
92def csv2list(fd):
93 item_list = []
94 for l in csv.reader(fd):
e5a1703d
P
95 if not l:
96 continue
e4fdfe59
P
97 d = l[0].strip()
98 if d != '':
99 item_list.append(d)
252760d2
P
100 return item_list
101
102form = cgi.FieldStorage()
103if 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,
122else:
123 print "Content-Type: text/html; charset=utf-8\r\n\r\n",
124 print HTML_FORM % os.environ,
125