-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv_views.py
336 lines (251 loc) · 9.55 KB
/
csv_views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# -*- coding: UTF-8 -*-
# Copyright (C) 2011 Hervé Cauwelier <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from the Standard Library
from cStringIO import StringIO
# Import from xlwt
from xlwt import Workbook, easyxf
from xlwt.Style import default_style
# Import from itools
from itools.core import freeze, is_prototype, merge_dicts, prototype, OrderedDict
from itools.csv import CSVFile
from itools.datatypes import Enumerate, String
from itools.gettext import MSG
from itools.stl import stl
from itools.web import ERROR
# Import from ikaaro
from ikaaro.buttons import BrowseButton
from ikaaro.utils import make_stl_template
from ikaaro.widgets import SelectWidget
ERR_NO_DATA = ERROR(u"No data to export.")
csv_writer_registry = OrderedDict()
def register_csv_writer(writer, name=None):
if name is None:
name = writer.name
csv_writer_registry[name] = writer
class XLSWriter(object):
name = 'xls'
title = MSG(u"MS Excel")
mimetype = 'application/vnd.ms-excel'
extension = 'xls'
header_style = easyxf('font: bold on')
def __init__(cls, columns, name):
cls.columns = columns
cls.workbook = workbook = Workbook()
# Taken from xlwt.Utils.valid_sheet_name
# Sheet name limited to 31 chars
if len(name) > 31:
name = u"{sheetname}...".format(sheetname=name[:28])
# XXX escape bug
# en.po locale/locale.pot:78:13: Séquence de contrôle invalide
# msgmerge: 1 erreur fatale trouvée
for c in ur"'[]:\\?/*\x00":
name = name.replace(c, u".")
cls.sheet = workbook.add_sheet(name)
# XXX
cls.y = 0
def get_nrows(cls):
return cls.y
def add_row(cls, row, is_header=False):
sheet = cls.sheet
if is_header is True:
style = cls.header_style
else:
style = default_style
for x, value in enumerate(row):
column = cls.columns[x]
value = column.encode(value)
sheet.write(cls.y, x, value, style)
cls.y += 1
def to_str(cls):
body = StringIO()
cls.workbook.save(body)
return body.getvalue()
register_csv_writer(XLSWriter)
class CSV_ODS_Writer(prototype):
name = 'ooo'
title = MSG(u"CSV for OpenOffice.org / LibreOffice")
mimetype = 'text/comma-separated-values'
extension = 'csv'
encoding = 'UTF-8'
separator = ","
newline = "\n"
def __init__(cls, columns, name):
cls.columns = columns
cls.csv = CSVFile()
def get_nrows(cls):
return cls.csv.get_nrows()
def add_row(cls, row, is_header=False):
values = []
for i, value in enumerate(row):
column = cls.columns[i]
value = column.encode(value)
if type(value) is unicode:
value = value.encode(cls.encoding)
else:
value = str(value)
values.append(value)
cls.csv.add_row(values)
def to_str(cls):
return cls.csv.to_str(separator=cls.separator, newline=cls.newline)
register_csv_writer(CSV_ODS_Writer)
class CSV_XLS_Writer(CSV_ODS_Writer):
name = 'excel'
title = MSG(u"CSV for MS Excel")
encoding = 'CP1252'
separator = ";"
newline = "\r\n"
register_csv_writer(CSV_XLS_Writer)
class CSVFormat(Enumerate):
formats = csv_writer_registry.keys()
def get_default(cls):
return cls.formats[0]
def get_options(cls):
options = []
for name in cls.formats:
writer = csv_writer_registry[name]
options.append({
'name': name,
'value': writer.title,
'writer': writer})
return options
def get_writer(cls, name):
for option in cls.get_options():
if option['name'] == name:
return option['writer']
raise ValueError, name
class CSVExportButton(BrowseButton):
access = 'is_allowed_to_view'
name = 'csv_export'
title = MSG(u"Export")
css = 'button-csv'
class CSVExportFormatButton(SelectWidget, CSVExportButton):
template = (make_stl_template("""
${label}:
<select id="${id}-format" name="csv_format">
<option stl:repeat="option options" value="${option/name}"
selected="${option/selected}">${option/value}</option>
</select>""")
+ CSVExportButton.template)
label = MSG(u"Export to format")
css = CSVExportButton.css
datatype = CSVFormat
def value(cls):
return cls.datatype.get_default()
class CSV_Export(object):
csv_schema = freeze({'csv_format': CSVFormat(mandatory=True)})
csv_template = '/ui/csv/format.xml'
csv_columns = freeze([])
csv_table_name = MSG(u"Sheet1")
csv_filename = None
csv_allow_empty = False
def get_csv_namespace(self, resource, context):
namespace = {}
datatype = self.csv_schema['csv_format']
namespace['format'] = SelectWidget('csv_format',
value=datatype.get_default(), datatype=datatype,
has_empty_option=False)
namespace['action'] = CSVExportButton(resource=resource,
context=context, items=[0])
template = resource.get_resource(self.csv_template)
return stl(template, namespace)
def get_csv_table_name(self, resource, context):
table_name = self.csv_table_name
if is_prototype(table_name, MSG):
table_name = table_name.gettext()
return table_name
def get_csv_filename(self, resource, context, writer):
filename = self.csv_filename
if filename is None:
filename = "{0}.{1}".format(resource.name, writer.extension)
return filename
def csv_write_header(self, resource, context, writer):
header = []
for column in self.csv_columns:
# TODO in 0.70 get title from class_schema
header.append(column.title)
writer.add_row(header, is_header=True)
def csv_write_row(self, resource, context, writer, item):
row = []
for column in self.csv_columns:
name = column.name
try:
value = getattr(item, name)
except AttributeError:
value = item.get_value(name)
else:
if callable(value):
value = value()
row.append(value)
writer.add_row(row)
def get_csv_items(self, resource, context, form):
raise NotImplementedError
def action_csv_export(self, resource, context, form):
datatype = self.get_schema(resource, context)['csv_format']
writer_class = datatype.get_writer(form['csv_format'])
# Get the items
items = self.get_csv_items(resource, context, form)
# Create the writer
table_name = self.get_csv_table_name(resource, context)
writer = writer_class(self.csv_columns, table_name)
# Add the header
self.csv_write_header(resource, context, writer)
header_rows = writer.get_nrows()
# Fill the CSV
for item in items:
self.csv_write_row(resource, context, writer, item)
if writer.get_nrows() == header_rows and not self.csv_allow_empty:
context.message = ERR_NO_DATA
return
# Set response type
context.set_content_type(writer.mimetype)
context.set_content_disposition('attachment; filename="{0}"'.format(
self.get_csv_filename(resource, context, writer)))
return writer.to_str()
class Folder_CSV_Export(CSV_Export):
# Allow to export the whole list without checking any box
action_csv_export_schema = freeze(merge_dicts(
CSV_Export.csv_schema,
ids=String(multiple=True, mandatory=False)))
def csv_write_row(self, resource, context, writer, item):
row = []
for column in self.csv_columns:
value = self.get_item_value(resource, context, item, column.name)
if type(value) is tuple:
value = value[0]
# TODO in 0.70 read datatype from class_schema
row.append(value)
writer.add_row(row)
def get_csv_items(self, resource, context, form):
results = self.get_items(resource, context)
if not len(results):
return
# Filter by selected ids or all
ids = form.get('ids')
# XXX
batch_start = context.query['batch_start']
batch_size = context.query['batch_size']
context.query['batch_start'] = context.query['batch_size'] = 0
for item in self.sort_and_batch(resource, context, results):
if ids:
item_id = self.get_item_value(resource, context, item,
'checkbox')
if type(item_id) is tuple:
item_id = item_id[0]
if item_id not in ids:
continue
yield item
context.query['batch_start'] = batch_start
context.query['batch_size'] = batch_size