10 from cubicweb.view import EntityView, AnyRsetView |
10 from cubicweb.view import EntityView, AnyRsetView |
11 |
11 |
12 class CSVMixIn(object): |
12 class CSVMixIn(object): |
13 """mixin class for CSV views""" |
13 """mixin class for CSV views""" |
14 templatable = False |
14 templatable = False |
15 content_type = "text/comma-separated-values" |
15 content_type = "text/comma-separated-values" |
16 binary = True # avoid unicode assertion |
16 binary = True # avoid unicode assertion |
17 csv_params = {'dialect': 'excel', |
17 csv_params = {'dialect': 'excel', |
18 'quotechar': '"', |
18 'quotechar': '"', |
19 'delimiter': ';', |
19 'delimiter': ';', |
20 'lineterminator': '\n'} |
20 'lineterminator': '\n'} |
21 |
21 |
22 def set_request_content_type(self): |
22 def set_request_content_type(self): |
23 """overriden to set a .csv filename""" |
23 """overriden to set a .csv filename""" |
24 self.req.set_content_type(self.content_type, filename='cubicwebexport.csv') |
24 self.req.set_content_type(self.content_type, filename='cubicwebexport.csv') |
25 |
25 |
26 def csvwriter(self, **kwargs): |
26 def csvwriter(self, **kwargs): |
27 params = self.csv_params.copy() |
27 params = self.csv_params.copy() |
28 params.update(kwargs) |
28 params.update(kwargs) |
29 return UnicodeCSVWriter(self.w, self.req.encoding, **params) |
29 return UnicodeCSVWriter(self.w, self.req.encoding, **params) |
30 |
30 |
31 |
31 |
32 class CSVRsetView(CSVMixIn, AnyRsetView): |
32 class CSVRsetView(CSVMixIn, AnyRsetView): |
33 """dumps raw result set in CSV""" |
33 """dumps raw result set in CSV""" |
34 id = 'csvexport' |
34 id = 'csvexport' |
35 title = _('csv export') |
35 title = _('csv export') |
36 |
36 |
37 def call(self): |
37 def call(self): |
38 writer = self.csvwriter() |
38 writer = self.csvwriter() |
39 writer.writerow(self.columns_labels()) |
39 writer.writerow(self.columns_labels()) |
40 rset, descr = self.rset, self.rset.description |
40 rset, descr = self.rset, self.rset.description |
41 eschema = self.schema.eschema |
41 eschema = self.schema.eschema |
43 csvrow = [] |
43 csvrow = [] |
44 for colindex, val in enumerate(row): |
44 for colindex, val in enumerate(row): |
45 etype = descr[rowindex][colindex] |
45 etype = descr[rowindex][colindex] |
46 if val is not None and not eschema(etype).is_final(): |
46 if val is not None and not eschema(etype).is_final(): |
47 # csvrow.append(val) # val is eid in that case |
47 # csvrow.append(val) # val is eid in that case |
48 content = self.view('textincontext', rset, |
48 content = self.view('textincontext', rset, |
49 row=rowindex, col=colindex) |
49 row=rowindex, col=colindex) |
50 else: |
50 else: |
51 content = self.view('final', rset, |
51 content = self.view('final', rset, |
52 displaytime=True, format='text/plain', |
52 displaytime=True, format='text/plain', |
53 row=rowindex, col=colindex) |
53 row=rowindex, col=colindex) |
54 csvrow.append(content) |
54 csvrow.append(content) |
55 writer.writerow(csvrow) |
55 writer.writerow(csvrow) |
56 |
56 |
57 |
57 |
58 class CSVEntityView(CSVMixIn, EntityView): |
58 class CSVEntityView(CSVMixIn, EntityView): |
59 """dumps rset's entities (with full set of attributes) in CSV |
59 """dumps rset's entities (with full set of attributes) in CSV |
60 |
60 |
61 the generated CSV file will have a table per entity type found in the |
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 |
62 resultset. ('table' here only means empty lines separation between table |
81 rows.append([entity.printable_value(rs.type, format='text/plain') |
81 rows.append([entity.printable_value(rs.type, format='text/plain') |
82 for rs in rowdef_by_type[entity.e_schema]]) |
82 for rs in rowdef_by_type[entity.e_schema]]) |
83 for rows in rows_by_type.itervalues(): |
83 for rows in rows_by_type.itervalues(): |
84 writer.writerows(rows) |
84 writer.writerows(rows) |
85 # use two empty lines as separator |
85 # use two empty lines as separator |
86 writer.writerows([[], []]) |
86 writer.writerows([[], []]) |
87 |
87 |