w.c.s : correction d'une fausse bonne idée…
[progfou.git] / wcs / wcs2json.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # le nom d'hote hébergeant wcs
5 VHOST = "formulaires.auf.org"
6 # nom du formulaire à explorer
7 FORM_NAME = "demande-de-bourse-de-doctorat-2010-2011"
8 # nom des fichiers à générer (un par formulaire)
9 OUTPUT_DIRECTORY = "/srv/www/wcs/%s/%s" % (VHOST, FORM_NAME)
10
11 URL_BASE = "http://%s/backoffice/%s" % (VHOST, FORM_NAME)
12 URL_DOWNLOAD = "%s/%s" % (URL_BASE, "%s/download?f=%s")
13 #TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
14 TIME_FORMAT = "%Y-%m-%d"
15
16 import os
17 import sys
18 from wcs import publisher
19 from wcs.formdef import FormDef
20 from wcs.fields import TitleField, CommentField, TextField, \
21 StringField, ItemField, EmailField, \
22 DateField, FileField, BoolField
23 from time import strftime
24 from simplejson import dumps, loads
25
26 def cleanup(s):
27 avec_accent = u'çÇáàâÁÀÂéèêëÉÈÊËíìîïÍÌÎÏóòôöÓÒÔÖúùûüÚÙÛÜýỳyÿÝỲYŸ'
28 sans_accent = u'cCaaaAAAeeeeEEEEiiiiIIIIooooOOOOuuuuUUUUyyyyYYYY'
29 if type(s) is not unicode:
30 s = unicode(s, 'utf-8')
31 u = False
32 r = ''
33 for c in s:
34 index = avec_accent.find(c)
35 if index >= 0:
36 c = sans_accent[index]
37 elif c != '-' and not ('a' <= c.lower() <= 'z'):
38 c = '-'
39 r += c
40 if not u:
41 r = r.encode('utf-8')
42 return r
43
44 f = open(FORM_NAME + '_field-names.txt', 'r')
45 field_names = dict([l.strip().split(':') for l in f.readlines()])
46 f.close()
47
48 qfile_commands = open(FORM_NAME + '_copy-qfiles.sh', 'w')
49
50 pub = publisher.WcsPublisher.create_publisher()
51 pub.app_dir = os.path.join(pub.app_dir, VHOST)
52
53
54 formdef = FormDef.get_by_urlname(FORM_NAME)
55 for object in formdef.data_class().select():
56 result = { 'num_dossier': object.id }
57 qfiles = { }
58 for field in formdef.fields:
59 field_id = str(field.id)
60 if not field_id in object.data:
61 continue
62 if isinstance(field, TitleField) or isinstance(field, CommentField):
63 continue
64 field_name = field_names.get(field_id, field.label)
65 data = object.data.get(field_id)
66 if isinstance(field, StringField) or isinstance(field, TextField) \
67 or isinstance(field, EmailField):
68 result[field_name] = data
69 elif isinstance(field, ItemField):
70 if field_name.startswith('pays') or 'etablissement' in field_name:
71 index = data.find('(')
72 if index >= 0:
73 end = data.find(' ', index+1)
74 data = data[index+1:end]
75 elif field_name == 'discipline':
76 index = data.rfind('(')
77 if index >= 0:
78 end = data.find(')', index+1)
79 data = data[index+1:end]
80 #elif field_name == 'region':
81 # index = data.rfind('(')
82 # if index >= 0:
83 # index = data.find('-', index+1)
84 # end = data.find(')', index+1)
85 # data = filter(lambda c: c.isupper(), data[index+1:end])
86 result[field_name] = data
87 elif isinstance(field, BoolField):
88 result[field_name] = (data == 'True')
89 elif isinstance(field, FileField):
90 extension = data.orig_filename.rpartition('.')[2].lower()
91 result[field_name] = "%s.%s" % (field_name, extension)
92 qfiles[field_name] = data.qfilename
93 elif isinstance(field, DateField):
94 data = strftime(TIME_FORMAT, object.data.get(field_id))
95 result[field_name] = data
96 else:
97 data = object.data.get(field_id)
98 print "WARNING: unknown field type '%s' for '%s'" % \
99 (field.__class__.__name__, field.label)
100 raise RuntimeError
101
102 num_dossier = result['num_dossier']
103 nom = cleanup('-'.join(result['nom'].split()).upper())
104 prenom = cleanup('-'.join(result['prenom'].split()).upper())
105 email = result['email'].replace('@','-').lower()
106
107 filename = "%s/%04d-%s-%s-%s.json" % \
108 (OUTPUT_DIRECTORY, num_dossier, nom, prenom, email)
109
110 f = open(filename, 'wb')
111 f.write(dumps(result, ensure_ascii=False))
112 f.close()
113
114 filename = filename[:-len('.json')]
115 for f in qfiles:
116 print >>qfile_commands, "cp -af '%s/uploads/%s' '%s_%s'" % \
117 (pub.app_dir, qfiles[f], filename, result[f])
118
119 qfile_commands.close()
120