[MERGE] new livechat web module chs
[odoo/odoo.git] / openerp / report / render / simple.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
22 import render
23
24 from cStringIO import StringIO
25 import xml.dom.minidom
26
27 from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table
28 from reportlab.lib.units import mm
29 from reportlab.lib.pagesizes import A4
30 import reportlab.lib
31
32 import copy
33
34 class simple(render.render):
35     def _render(self):
36         self.result = StringIO()
37         parser = xml.dom.minidom.parseString(self.xml)
38
39         title = parser.documentElement.tagName
40         doc = SimpleDocTemplate(self.result, pagesize=A4, title=title,
41           author='OpenERP, Fabien Pinckaers', leftmargin=10*mm, rightmargin=10*mm)
42
43         styles = reportlab.lib.styles.getSampleStyleSheet()
44         title_style = copy.deepcopy(styles["Heading1"])
45         title_style.alignment = reportlab.lib.enums.TA_CENTER
46         story = [ Paragraph(title, title_style) ]
47         style_level = {}
48         nodes = [ (parser.documentElement,0) ]
49         while len(nodes):
50             node = nodes.pop(0)
51             value = ''
52             n=len(node[0].childNodes)-1
53             while n>=0:
54                 if node[0].childNodes[n].nodeType==3:
55                     value += node[0].childNodes[n].nodeValue
56                 else:
57                     nodes.insert( 0, (node[0].childNodes[n], node[1]+1) )
58                 n-=1
59             if not node[1] in style_level:
60                 style = copy.deepcopy(styles["Normal"])
61                 style.leftIndent=node[1]*6*mm
62                 style.firstLineIndent=-3*mm
63                 style_level[node[1]] = style
64             story.append( Paragraph('<b>%s</b>: %s' % (node[0].tagName, value), style_level[node[1]]))
65         doc.build(story)
66         return self.result.getvalue()
67
68 if __name__=='__main__':
69     import time
70     s = simple()
71     s.xml = '''<test>
72         <author-list>
73             <author>
74                 <name>Fabien Pinckaers</name>
75                 <age>23</age>
76             </author>
77             <author>
78                 <name>Michel Pinckaers</name>
79                 <age>53</age>
80             </author>
81             No other
82         </author-list>
83     </test>'''
84     if s.render():
85         print s.get()
86
87 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
88