[FIX] website: remove backslash from redirect with enable_editor
[odoo/odoo.git] / addons / report_webkit / report_helper.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2010 Camptocamp SA (http://www.camptocamp.com) 
5 # All Right Reserved
6 #
7 # Author : Nicolas Bessi (Camptocamp)
8 #
9 # WARNING: This program as such is intended to be used by professional
10 # programmers who take the whole responsability of assessing all potential
11 # consequences resulting from its eventual inadequacies and bugs
12 # End users who are looking for a ready-to-use solution with commercial
13 # garantees and support are strongly adviced to contract a Free Software
14 # Service Company
15 #
16 # This program is Free Software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation; either version 2
19 # of the License, or (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
29 #
30 ##############################################################################
31
32 import openerp
33
34 class WebKitHelper(object):
35     """Set of usefull report helper"""
36     def __init__(self, cursor, uid, report_id, context):
37         "constructor"
38         self.cursor = cursor
39         self.uid = uid
40         self.pool = openerp.registry(self.cursor.dbname)
41         self.report_id = report_id
42         self.context = context
43         
44     def embed_image(self, type, img, width=0, height=0) :
45         "Transform a DB image into an embedded HTML image"
46
47         if width :
48             width = 'width="%spx"'%(width)
49         else :
50             width = ' '
51         if height :
52             height = 'height="%spx"'%(height)
53         else :
54             height = ' '
55         toreturn = '<img %s %s src="data:image/%s;base64,%s" />'%(
56             width,
57             height,
58             type, 
59             str(img))
60         return toreturn
61             
62             
63     def get_logo_by_name(self, name):
64         """Return logo by name"""
65         header_obj = self.pool.get('ir.header_img')
66         header_img_id = header_obj.search(
67                                             self.cursor, 
68                                             self.uid, 
69                                             [('name','=',name)]
70                                         )
71         if not header_img_id :
72             return u''
73         if isinstance(header_img_id, list):
74             header_img_id = header_img_id[0]
75
76         head = header_obj.browse(self.cursor, self.uid, header_img_id)
77         return (head.img, head.type)
78             
79     def embed_logo_by_name(self, name, width=0, height=0):
80         """Return HTML embedded logo by name"""
81         img, type = self.get_logo_by_name(name)
82         return self.embed_image(type, img, width, height)
83
84     def embed_company_logo(self, width=0, height=0):
85         cr, uid, context = self.cursor, self.uid, self.context
86         my_user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
87         logo = my_user.company_id.logo_web
88         return self.embed_image("png", logo, width, height)
89
90         
91
92 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: