[MERGE] branch trunk-some_typo: fix capitalization and typos
[odoo/odoo.git] / openerp / report / render / makohtml2html / makohtml2html.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21 import logging
22 import mako
23 from lxml import etree
24 from mako.template import Template
25 from mako.lookup import TemplateLookup
26 import openerp.netsvc as netsvc
27 import traceback, sys, os
28
29 _logger = logging.getLogger(__name__)
30
31 class makohtml2html(object):
32     def __init__(self, html, localcontext):
33         self.localcontext = localcontext
34         self.html = html
35
36     def format_header(self, html):
37         head = html.findall('head')
38         header = ''
39         for node in head:
40             header += etree.tostring(node)
41         return header
42
43     def format_footer(self, footer):
44         html_footer = ''
45         for node in footer[0].getchildren():
46             html_footer += etree.tostring(node)
47         return html_footer
48
49     def format_body(self, html):
50         body = html.findall('body')
51         body_list = []
52         footer =  self.format_footer(body[-1].getchildren())
53         for b in body[:-1]:
54             body_list.append(etree.tostring(b).replace('\t', '').replace('\n',''))
55         html_body ='''
56         <script type="text/javascript">
57
58         var indexer = 0;
59         var aryTest = %s ;
60         function nextData()
61             {
62             if(indexer < aryTest.length -1)
63                 {
64                 indexer += 1;
65                 document.forms[0].prev.disabled = false;
66                 document.getElementById("openerp_data").innerHTML=aryTest[indexer];
67                 document.getElementById("counter").innerHTML= indexer + 1 + ' / ' + aryTest.length;
68                 }
69             else
70                {
71                 document.forms[0].next.disabled = true;
72                }
73             }
74         function prevData()
75             {
76             if (indexer > 0)
77                 {
78                 indexer -= 1;
79                 document.forms[0].next.disabled = false;
80                 document.getElementById("openerp_data").innerHTML=aryTest[indexer];
81                 document.getElementById("counter").innerHTML=  indexer + 1 + ' / ' + aryTest.length;
82                 }
83             else
84                {
85                 document.forms[0].prev.disabled = true;
86                }
87             }
88     </script>
89     </head>
90     <body>
91         <div id="openerp_data">
92             %s
93         </div>
94         <div>
95         %s
96         </div>
97         <br>
98         <form>
99             <table>
100                 <tr>
101                     <td td align="left">
102                         <input name = "prev" type="button" value="Previous" onclick="prevData();">
103                     </td>
104                     <td>
105                         <div id = "counter">%s / %s</div>
106                     </td>
107                     <td align="right">
108                         <input name = "next" type="button" value="Next" onclick="nextData();">
109                     </td>
110                 </tr>
111             </table>
112         </form>
113     </body></html>'''%(body_list,body_list[0],footer,'1',len(body_list))
114         return html_body
115
116     def render(self):
117         path = os.path.realpath('addons/base/report')
118         temp_lookup = TemplateLookup(directories=[path],output_encoding='utf-8', encoding_errors='replace')
119         template = Template(self.html, lookup=temp_lookup)
120         self.localcontext.update({'css_path':path})
121         final_html ='''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
122                     <html>'''
123         try:
124             html = template.render_unicode(**self.localcontext)
125             etree_obj = etree.HTML(html)
126             final_html += self.format_header(etree_obj)
127             final_html += self.format_body(etree_obj)
128             return final_html
129         except Exception,e:
130             tb_s = reduce(lambda x, y: x+y, traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
131             _logger.error('report :\n%s\n%s\n', tb_s, str(e))
132
133 def parseNode(html, localcontext = {}):
134     r = makohtml2html(html, localcontext)
135     return r.render()
136
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: