Commit | Line | Data |
---|---|---|
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 |
6 | import os |
7 | import csv | |
8 | import cgi | |
9 | import cgitb | |
10 | cgitb.enable(display=0, logdir="/tmp") | |
11 | ||
12 | try: | |
5602202b | 13 | from wcs.qommon.misc import simplify |
252760d2 P |
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 à convertir :<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ère ligne du fichier :</td> | |
33 | <td><input type="checkbox" name="delete_first" checked="on" /></td> | |
34 | </tr> | |
35 | <tr> | |
36 | <td>Nom du formulaire :</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 :</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(): | |
e4fdfe59 P |
87 | d = l.strip() |
88 | if d != '': | |
89 | item_list.append(d) | |
252760d2 P |
90 | return item_list |
91 | ||
92 | def csv2list(fd): | |
93 | item_list = [] | |
94 | for l in csv.reader(fd): | |
e4fdfe59 P |
95 | d = l[0].strip() |
96 | if d != '': | |
97 | item_list.append(d) | |
252760d2 P |
98 | return item_list |
99 | ||
100 | form = cgi.FieldStorage() | |
101 | if form and form['filename'].type in ('text/plain','text/csv'): | |
102 | delete_first = form.getvalue('delete_first') | |
103 | conv_func = form['filename'].type.split('/')[-1] + '2list' | |
104 | item_list = globals()[conv_func](form['filename'].file) | |
105 | if delete_first: | |
106 | item_list.pop(0) | |
107 | form_name = form.getvalue('form_name') | |
108 | url_name = simplify(form_name) | |
109 | file_name = '%s.wcs' % url_name | |
110 | info = { | |
111 | 'form_name': cgi.escape(form_name), | |
112 | 'url_name': cgi.escape(url_name), | |
113 | 'field_name': cgi.escape(form.getvalue('field_name')), | |
114 | 'item_list': '\n'.join([WCS_ITEM % cgi.escape(i) for i in item_list]), | |
115 | } | |
116 | #print "Location: https://preprod-formulaires.auf.org/admin/forms/import\r\n", | |
117 | print "Content-Type: application/x-wcs-form; name=\"%s\"\r\n" % file_name, | |
118 | print "Content-Disposition: attachment; filename=\"%s\"\r\n\r\n" % file_name, | |
119 | print WCS_FORM % info, | |
120 | else: | |
121 | print "Content-Type: text/html; charset=utf-8\r\n\r\n", | |
122 | print HTML_FORM % os.environ, | |
123 |