~tempo-openerp/+junk/loewert-prod

« back to all changes in this revision

Viewing changes to server/openerp/report/render/makohtml2html/makohtml2html.py

  • Committer: jbe at tempo-consulting
  • Date: 2013-08-21 08:48:11 UTC
  • Revision ID: jbe@tempo-consulting.fr-20130821084811-913uo4l7b5ayxq8m
[NEW] Création de la branche trunk Loewert

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 os
 
27
 
 
28
_logger = logging.getLogger(__name__)
 
29
 
 
30
class makohtml2html(object):
 
31
    def __init__(self, html, localcontext):
 
32
        self.localcontext = localcontext
 
33
        self.html = html
 
34
 
 
35
    def format_header(self, html):
 
36
        head = html.findall('head')
 
37
        header = ''
 
38
        for node in head:
 
39
            header += etree.tostring(node)
 
40
        return header
 
41
 
 
42
    def format_footer(self, footer):
 
43
        html_footer = ''
 
44
        for node in footer[0].getchildren():
 
45
            html_footer += etree.tostring(node)
 
46
        return html_footer
 
47
 
 
48
    def format_body(self, html):
 
49
        body = html.findall('body')
 
50
        body_list = []
 
51
        footer =  self.format_footer(body[-1].getchildren())
 
52
        for b in body[:-1]:
 
53
            body_list.append(etree.tostring(b).replace('\t', '').replace('\n',''))
 
54
        html_body ='''
 
55
        <script type="text/javascript">
 
56
 
 
57
        var indexer = 0;
 
58
        var aryTest = %s ;
 
59
        function nextData()
 
60
            {
 
61
            if(indexer < aryTest.length -1)
 
62
                {
 
63
                indexer += 1;
 
64
                document.forms[0].prev.disabled = false;
 
65
                document.getElementById("openerp_data").innerHTML=aryTest[indexer];
 
66
                document.getElementById("counter").innerHTML= indexer + 1 + ' / ' + aryTest.length;
 
67
                }
 
68
            else
 
69
               {
 
70
                document.forms[0].next.disabled = true;
 
71
               }
 
72
            }
 
73
        function prevData()
 
74
            {
 
75
            if (indexer > 0)
 
76
                {
 
77
                indexer -= 1;
 
78
                document.forms[0].next.disabled = false;
 
79
                document.getElementById("openerp_data").innerHTML=aryTest[indexer];
 
80
                document.getElementById("counter").innerHTML=  indexer + 1 + ' / ' + aryTest.length;
 
81
                }
 
82
            else
 
83
               {
 
84
                document.forms[0].prev.disabled = true;
 
85
               }
 
86
            }
 
87
    </script>
 
88
    </head>
 
89
    <body>
 
90
        <div id="openerp_data">
 
91
            %s
 
92
        </div>
 
93
        <div>
 
94
        %s
 
95
        </div>
 
96
        <br>
 
97
        <form>
 
98
            <table>
 
99
                <tr>
 
100
                    <td td align="left">
 
101
                        <input name = "prev" type="button" value="Previous" onclick="prevData();">
 
102
                    </td>
 
103
                    <td>
 
104
                        <div id = "counter">%s / %s</div>
 
105
                    </td>
 
106
                    <td align="right">
 
107
                        <input name = "next" type="button" value="Next" onclick="nextData();">
 
108
                    </td>
 
109
                </tr>
 
110
            </table>
 
111
        </form>
 
112
    </body></html>'''%(body_list,body_list[0],footer,'1',len(body_list))
 
113
        return html_body
 
114
 
 
115
    def render(self):
 
116
        path = os.path.realpath('addons/base/report')
 
117
        temp_lookup = TemplateLookup(directories=[path],output_encoding='utf-8', encoding_errors='replace')
 
118
        template = Template(self.html, lookup=temp_lookup)
 
119
        self.localcontext.update({'css_path':path})
 
120
        final_html ='''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 
121
                    <html>'''
 
122
        try:
 
123
            html = template.render_unicode(**self.localcontext)
 
124
            etree_obj = etree.HTML(html)
 
125
            final_html += self.format_header(etree_obj)
 
126
            final_html += self.format_body(etree_obj)
 
127
            return final_html
 
128
        except Exception:
 
129
            _logger.exception('report :')
 
130
 
 
131
def parseNode(html, localcontext = {}):
 
132
    r = makohtml2html(html, localcontext)
 
133
    return r.render()
 
134
 
 
135
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: