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
|
# -*- coding: utf-8 -*-
from lxml import etree
from mx import DateTime
from tools.translate import _
from tools.misc import file_open
from osv import osv
from report_webkit.webkit_report import WebKitParser
from report import report_sxw
import os
import netsvc
import pooler
from mako.template import Template
from mako import exceptions
from mako.runtime import Context
from tools.misc import file_open
import pooler
import addons
import time
import zipfile
import tempfile
import codecs
class SpreadsheetReport(WebKitParser):
_fields_process = {
'date': report_sxw._date_format,
'datetime': report_sxw._dttime_format
}
def __init__(self, name, table, rml=False, parser=report_sxw.rml_parse, header='external', store=False):
if not rml:
rml = 'addons/spreadsheet_xml/report/spreadsheet_xls.mako'
WebKitParser.__init__(self, name, table, rml=rml, parser=parser, header=header, store=store)
self.sheet_name_used = []
self.total_sheet_number = 1
def sheet_name(self, default_name=False, context=None):
sheet_max_size = 31
if not default_name:
default_name = 'Sheet %s' % (self.total_sheet_number, )
default_name = default_name[0:sheet_max_size].replace('/','_')
if default_name in self.sheet_name_used:
default_name = '%s %s'% (default_name[0:sheet_max_size - len('%s' % self.total_sheet_number) - 1], self.total_sheet_number)
self.sheet_name_used.append(default_name)
self.total_sheet_number += 1
return default_name
def create_single_pdf(self, cr, uid, ids, data, report_xml, context=None):
if context is None:
context = {}
if report_xml.report_type != 'webkit':
return super(WebKitParser,self).create_single_pdf(cr, uid, ids, data, report_xml, context=context)
self.report_xml = report_xml
self.parser_instance = self.parser(cr, uid, self.name2, context=context)
self.pool = pooler.get_pool(cr.dbname)
if not context.get('splitbrowse'):
objs = self.getObjects(cr, uid, ids, context)
else:
objs = []
self.parser_instance.localcontext['ids'] = ids
self.parser_instance.localcontext['context'] = context
self.parser_instance.set_context(objs, data, ids, report_xml.report_type)
template = False
if report_xml.report_file:
path = addons.get_module_resource(report_xml.report_file)
if path and os.path.exists(path):
template = file(path).read()
if self.tmpl:
f = file_open(self.tmpl)
template = f.read()
f.close()
if not template:
raise osv.except_osv(_('Error!'), _('Webkit Report template not found !'))
self.localcontext.update({'lang': context.get('lang')})
self.parser_instance.localcontext.update({'setLang':self.setLang})
self.parser_instance.localcontext.update({'formatLang':self.formatLang})
null, tmpname = tempfile.mkstemp()
fileout = codecs.open(tmpname, 'wb', 'utf8')
body_mako_tpl = Template(template, input_encoding='utf-8', default_filters=['unicode'])
try:
mako_ctx = Context(fileout, _=self.translate_call, **self.parser_instance.localcontext)
body_mako_tpl.render_context(mako_ctx)
fileout.close()
except Exception, e:
msg = exceptions.text_error_template().render()
netsvc.Logger().notifyChannel('Webkit render', netsvc.LOG_ERROR, msg)
raise osv.except_osv(_('Webkit render'), msg)
# circular reference on parse_instance, force memory free
del self.parser_instance
if context.get('zipit'):
null1, tmpzipname = tempfile.mkstemp()
zf = zipfile.ZipFile(tmpzipname, 'w')
zf.write(tmpname, 'export_result.xls', zipfile.ZIP_DEFLATED)
zf.close()
out = file(tmpzipname, 'rb').read()
os.close(null1)
os.close(null)
os.unlink(tmpzipname)
os.unlink(tmpname)
return (out, 'zip')
out = file(tmpname, 'rb').read()
os.close(null)
os.unlink(tmpname)
return (out, 'xls')
def getObjects(self, cr, uid, ids, context):
table_obj = pooler.get_pool(cr.dbname).get(self.table)
self.sheet_name_used = []
self.total_sheet_number = 1
self.parser_instance.localcontext['sheet_name'] = self.sheet_name
return table_obj.browse(cr, uid, ids, list_class=report_sxw.browse_record_list, context=context, fields_process=self._fields_process)
def create(self, cr, uid, ids, data, context=None):
a = super(SpreadsheetReport, self).create(cr, uid, ids, data, context)
# This permit to test XLS report generation with tools.tests_reports without given some warning
# Cf. tools/tests_reports.py:89
if context and context.get('from_yml', False) and context.get('from_yml') is True:
return (a[0], 'foobar')
return a
class SpreadsheetCreator(object):
def __init__(self, title, headers, datas):
self.headers = headers
self.datas = datas
self.title = title
def get_xml(self, default_filters=[]):
f, filename = file_open('addons/spreadsheet_xml/report/spreadsheet_writer_xls.mako', pathinfo=True)
f[0].close()
tmpl = Template(filename=filename, input_encoding='utf-8', output_encoding='utf-8', default_filters=default_filters)
return tmpl.render(objects=self.datas, headers=self.headers, title= self.title)
|