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