Merge lp:~camptocamp/openobject-addons/trunk_webkit_improvment_rc into lp:openobject-addons

Proposed by Nicolas Bessi - Camptocamp
Status: Merged
Merged at revision: 3984
Proposed branch: lp:~camptocamp/openobject-addons/trunk_webkit_improvment_rc
Merge into: lp:openobject-addons
Diff against target: 181 lines (+75/-50)
2 files modified
report_webkit/report_helper.py (+1/-1)
report_webkit/webkit_report.py (+74/-49)
To merge this branch: bzr merge lp:~camptocamp/openobject-addons/trunk_webkit_improvment_rc
Reviewer Review Type Date Requested Status
Olivier Dony (Odoo) Pending
Review via email: mp+41882@code.launchpad.net

This proposal supersedes a proposal from 2010-11-15.

Description of the change

[FIX] RC2 fix of image embeeding, possibilit to have tmp file name in same milisecond corrected, imporvement of multi header support when cusomizing report, fix of translation getter retunr scr if no translation available

To post a comment you must log in.
Revision history for this message
Olivier Dony (Odoo) (odo-openerp) wrote : Posted in a previous version of this proposal

Hello Nicolas,

A few remarks:
- adding try/except blocks without specifying the exception class is a no-go, you need to at least specify "except Exception" if you don't know a more precise class to catch. Also, couldn't it be a single try/except block?

I haven't checked, but do you know if the "Exception" type that you are throwing is properly reported to the user, or is it simply displayed in the log? Don't you want to use an except_osv one?
I know I had to fix several things during the initial review to get exceptions reported to the user correctly.

- As for the question about 0 margins, OpenERP does not yet support distinguishing 0 from NULL values for integers. However I wonder if your margins fields should not be fields.char instead, because the wkhtmltopdf params are strings indeed, as you can pass "10px" or "10.5cm", etc. This would give you more flexibility, wouldn't it? Note: You need to be careful however because I think wkhtmltopdf only supports the same unit of measure for all margins, and the default ones are "10mm". So when specifying a unit it needs to be set for all margins and it must be the same.

I'm okay with the latter change for RC2, as it is needed to properly fix the margins.

What do you think?

review: Needs Fixing
Revision history for this message
Nicolas Bessi - Camptocamp (nbessi-c2c-deactivatedaccount) wrote : Posted in a previous version of this proposal

Hello,

For the try catch block around the render method I strictly followed the mako doc. That why there is not one unique try except block but one around each render call. Mako does not provide a render exception type (or I missed it in doc). The exception returned is munch more easy to understand. But I agree with the osv_except point and the general Exception type catch point (I have to test if it works).

For the margin I prefer to keep float because setting margin in different unit may crash wkhtmltopdf, and it prevent wrong entries. But why not provide a select field with supported unit and set mm as default.

Many thanks

Nicolas

Le 17 nov. 2010 à 16:53, "Olivier Dony (OpenERP)" <email address hidden> a écrit :

> Review: Needs Fixing
> Hello Nicolas,
>
> A few remarks:
> - adding try/except blocks without specifying the exception class is a no-go, you need to at least specify "except Exception" if you don't know a more precise class to catch. Also, couldn't it be a single try/except block?
>
> I haven't checked, but do you know if the "Exception" type that you are throwing is properly reported to the user, or is it simply displayed in the log? Don't you want to use an except_osv one?
> I know I had to fix several things during the initial review to get exceptions reported to the user correctly.
>
> - As for the question about 0 margins, OpenERP does not yet support distinguishing 0 from NULL values for integers. However I wonder if your margins fields should not be fields.char instead, because the wkhtmltopdf params are strings indeed, as you can pass "10px" or "10.5cm", etc. This would give you more flexibility, wouldn't it? Note: You need to be careful however because I think wkhtmltopdf only supports the same unit of measure for all margins, and the default ones are "10mm". So when specifying a unit it needs to be set for all margins and it must be the same.
>
> I'm okay with the latter change for RC2, as it is needed to properly fix the margins.
>
> What do you think?
> --
> https://code.launchpad.net/~c2c/openobject-addons/trunk_webkit_improvment_rc/+merge/40847
> Your team Camptocamp is subscribed to branch lp:~c2c/openobject-addons/trunk_webkit_improvment_rc.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'report_webkit/report_helper.py'
2--- report_webkit/report_helper.py 2010-10-14 13:38:14 +0000
3+++ report_webkit/report_helper.py 2010-11-25 16:51:17 +0000
4@@ -51,7 +51,7 @@
5 height = 'width="%spx"'%(height)
6 else :
7 height = ' '
8- toreturn = '<img %s %s src="data:image/%s;base64,%s">'%(
9+ toreturn = '<img %s %s src="data:image/%s;base64,%s" />'%(
10 width,
11 height,
12 type,
13
14=== modified file 'report_webkit/webkit_report.py'
15--- report_webkit/webkit_report.py 2010-10-26 13:22:54 +0000
16+++ report_webkit/webkit_report.py 2010-11-25 16:51:17 +0000
17@@ -35,7 +35,7 @@
18 import tempfile
19 import time
20 from mako.template import Template
21-
22+from mako import exceptions
23 import netsvc
24 import pooler
25 from report_helper import WebKitHelper
26@@ -84,8 +84,10 @@
27 _('path to Wkhtmltopdf is not absolute'),
28 'for path %s'%(path)
29 )
30- def generate_pdf(self, comm_path, report_xml, header, footer, html_list):
31+ def generate_pdf(self, comm_path, report_xml, header, footer, html_list, webkit_header=False):
32 """Call webkit in order to generate pdf"""
33+ if not webkit_header:
34+ webkit_header = report_xml.webkit_header
35 tmp_dir = tempfile.gettempdir()
36 out = report_xml.name+str(time.time())+'.pdf'
37 out = os.path.join(tmp_dir, out.replace(' ',''))
38@@ -120,20 +122,22 @@
39 file_to_del.append(foot_file.name)
40 command.append("--footer-html '%s'"%(foot_file.name))
41
42- if report_xml.webkit_header.margin_top :
43- command.append('--margin-top %s'%(report_xml.webkit_header.margin_top))
44- if report_xml.webkit_header.margin_bottom :
45- command.append('--margin-bottom %s'%(report_xml.webkit_header.margin_bottom))
46- if report_xml.webkit_header.margin_left :
47- command.append('--margin-left %s'%(report_xml.webkit_header.margin_left))
48- if report_xml.webkit_header.margin_right :
49- command.append('--margin-right %s'%(report_xml.webkit_header.margin_right))
50- if report_xml.webkit_header.orientation :
51- command.append("--orientation '%s'"%(report_xml.webkit_header.orientation))
52- if report_xml.webkit_header.format :
53- command.append(" --page-size '%s'"%(report_xml.webkit_header.format))
54+ if webkit_header.margin_top :
55+ command.append('--margin-top %s'%(webkit_header.margin_top))
56+ if webkit_header.margin_bottom :
57+ command.append('--margin-bottom %s'%(webkit_header.margin_bottom))
58+ if webkit_header.margin_left :
59+ command.append('--margin-left %s'%(webkit_header.margin_left))
60+ if webkit_header.margin_right :
61+ command.append('--margin-right %s'%(webkit_header.margin_right))
62+ if webkit_header.orientation :
63+ command.append("--orientation '%s'"%(webkit_header.orientation))
64+ if webkit_header.format :
65+ command.append(" --page-size '%s'"%(webkit_header.format))
66+ count = 0
67 for html in html_list :
68- html_file = file(os.path.join(tmp_dir, str(time.time()) + '.body.html'), 'w')
69+ html_file = file(os.path.join(tmp_dir, str(time.time()) + str(count) +'.body.html'), 'w')
70+ count += 1
71 html_file.write(html)
72 html_file.close()
73 file_to_del.append(html_file.name)
74@@ -168,6 +172,8 @@
75 """Translate String."""
76 ir_translation = self.pool.get('ir.translation')
77 res = ir_translation._get_source(self.parser_instance.cr, self.parser_instance.uid, self.name, 'report', self.localcontext.get('lang', 'en_US'), src)
78+ if not res :
79+ return src
80 return res
81
82 def formatLang(self, value, digits=None, date=False, date_time=False, grouping=True, monetary=False):
83@@ -272,45 +278,64 @@
84 #default_filters=['unicode', 'entity'] can be used to set global filter
85 body_mako_tpl = Template(template ,input_encoding='utf-8')
86 helper = WebKitHelper(cursor, uid, report_xml.id, context)
87- html = body_mako_tpl.render( helper=helper,
88- css=css,
89- _=self.translate_call,
90- **self.parser_instance.localcontext
91- )
92+ try :
93+ html = body_mako_tpl.render( helper=helper,
94+ css=css,
95+ _=self.translate_call,
96+ **self.parser_instance.localcontext
97+ )
98+ except Exception, e:
99+ msg = exceptions.text_error_template().render()
100+ netsvc.Logger().notifyChannel('Webkit render', netsvc.LOG_ERROR, msg)
101+ raise except_osv(_('Webkit render'), msg)
102 head_mako_tpl = Template(header, input_encoding='utf-8')
103- head = head_mako_tpl.render(
104- company=company,
105- time=time,
106- helper=helper,
107- css=css,
108- formatLang=self.formatLang,
109- setLang=self.setLang,
110- _=self.translate_call,
111- _debug=False
112- )
113+ try :
114+ head = head_mako_tpl.render(
115+ company=company,
116+ time=time,
117+ helper=helper,
118+ css=css,
119+ formatLang=self.formatLang,
120+ setLang=self.setLang,
121+ _=self.translate_call,
122+ _debug=False
123+ )
124+ except Exception, e:
125+ raise except_osv(_('Webkit render'),
126+ exceptions.text_error_template().render())
127 foot = False
128 if footer :
129 foot_mako_tpl = Template(footer ,input_encoding='utf-8')
130- foot = foot_mako_tpl.render(
131- company=company,
132- time=time,
133- helper=helper,
134- css=css,
135- formatLang=self.formatLang,
136- setLang=self.setLang,
137- _=self.translate_call,
138- )
139+ try :
140+ foot = foot_mako_tpl.render(
141+ company=company,
142+ time=time,
143+ helper=helper,
144+ css=css,
145+ formatLang=self.formatLang,
146+ setLang=self.setLang,
147+ _=self.translate_call,
148+ )
149+ except:
150+ msg = exceptions.text_error_template().render()
151+ netsvc.Logger().notifyChannel('Webkit render', netsvc.LOG_ERROR, msg)
152+ raise except_osv(_('Webkit render'), msg)
153 if report_xml.webkit_debug :
154- deb = head_mako_tpl.render(
155- company=company,
156- time=time,
157- helper=helper,
158- css=css,
159- _debug=html,
160- formatLang=self.formatLang,
161- setLang=self.setLang,
162- _=self.translate_call,
163- )
164+ try :
165+ deb = head_mako_tpl.render(
166+ company=company,
167+ time=time,
168+ helper=helper,
169+ css=css,
170+ _debug=html,
171+ formatLang=self.formatLang,
172+ setLang=self.setLang,
173+ _=self.translate_call,
174+ )
175+ except Exception, e:
176+ msg = exceptions.text_error_template().render()
177+ netsvc.Logger().notifyChannel('Webkit render', netsvc.LOG_ERROR, msg)
178+ raise except_osv(_('Webkit render'), msg)
179 return (deb, 'html')
180 bin = self.get_lib(cursor, uid, company.id)
181 pdf = self.generate_pdf(bin, report_xml, head, foot, [html])

Subscribers

People subscribed via source and target branches

to all changes: