|
1 """csv export views |
|
2 |
|
3 :organization: Logilab |
|
4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 """ |
|
7 __docformat__ = "restructuredtext en" |
|
8 |
|
9 from cubicweb.common.uilib import UnicodeCSVWriter |
|
10 from cubicweb.view import EntityView, AnyRsetView |
|
11 |
|
12 class CSVMixIn(object): |
|
13 """mixin class for CSV views""" |
|
14 templatable = False |
|
15 content_type = "text/comma-separated-values" |
|
16 binary = True # avoid unicode assertion |
|
17 csv_params = {'dialect': 'excel', |
|
18 'quotechar': '"', |
|
19 'delimiter': ';', |
|
20 'lineterminator': '\n'} |
|
21 |
|
22 def set_request_content_type(self): |
|
23 """overriden to set a .csv filename""" |
|
24 self.req.set_content_type(self.content_type, filename='cubicwebexport.csv') |
|
25 |
|
26 def csvwriter(self, **kwargs): |
|
27 params = self.csv_params.copy() |
|
28 params.update(kwargs) |
|
29 return UnicodeCSVWriter(self.w, self.req.encoding, **params) |
|
30 |
|
31 |
|
32 class CSVRsetView(CSVMixIn, AnyRsetView): |
|
33 """dumps raw result set in CSV""" |
|
34 id = 'csvexport' |
|
35 title = _('csv export') |
|
36 |
|
37 def call(self): |
|
38 writer = self.csvwriter() |
|
39 writer.writerow(self.columns_labels()) |
|
40 rset, descr = self.rset, self.rset.description |
|
41 eschema = self.schema.eschema |
|
42 for rowindex, row in enumerate(rset): |
|
43 csvrow = [] |
|
44 for colindex, val in enumerate(row): |
|
45 etype = descr[rowindex][colindex] |
|
46 if val is not None and not eschema(etype).is_final(): |
|
47 # csvrow.append(val) # val is eid in that case |
|
48 content = self.view('textincontext', rset, |
|
49 row=rowindex, col=colindex) |
|
50 else: |
|
51 content = self.view('final', rset, |
|
52 displaytime=True, format='text/plain', |
|
53 row=rowindex, col=colindex) |
|
54 csvrow.append(content) |
|
55 writer.writerow(csvrow) |
|
56 |
|
57 |
|
58 class CSVEntityView(CSVMixIn, EntityView): |
|
59 """dumps rset's entities (with full set of attributes) in CSV |
|
60 |
|
61 the generated CSV file will have a table per entity type found in the |
|
62 resultset. ('table' here only means empty lines separation between table |
|
63 contents) |
|
64 """ |
|
65 id = 'ecsvexport' |
|
66 title = _('csv entities export') |
|
67 |
|
68 def call(self): |
|
69 req = self.req |
|
70 rows_by_type = {} |
|
71 writer = self.csvwriter() |
|
72 rowdef_by_type = {} |
|
73 for index in xrange(len(self.rset)): |
|
74 entity = self.complete_entity(index) |
|
75 if entity.e_schema not in rows_by_type: |
|
76 rowdef_by_type[entity.e_schema] = [rs for rs, at in entity.e_schema.attribute_definitions() |
|
77 if at != 'Bytes'] |
|
78 rows_by_type[entity.e_schema] = [[display_name(req, rschema.type) |
|
79 for rschema in rowdef_by_type[entity.e_schema]]] |
|
80 rows = rows_by_type[entity.e_schema] |
|
81 rows.append([entity.printable_value(rs.type, format='text/plain') |
|
82 for rs in rowdef_by_type[entity.e_schema]]) |
|
83 for rows in rows_by_type.itervalues(): |
|
84 writer.writerows(rows) |
|
85 # use two empty lines as separator |
|
86 writer.writerows([[], []]) |
|
87 |