Beispiel #1
0
    def create_single_pdf(self, cursor, uid, ids, data, report_xml, context=None):
        """generate the PDF"""

        # just try to find an xml id for the report
        cr = cursor
        registry = odoo.registry(cr.dbname)
        with registry.cursor() as cr:
            env = api.Environment(cr, SUPERUSER_ID, {})
            found_xml_ids = env["ir.model.data"].search([["model", "=", "ir.actions.report.xml"], \
                ["res_id", "=", report_xml.id]])
            xml_id = None
            #print "===found_xml_ids====",found_xml_ids
            if found_xml_ids:
                xml_id = found_xml_ids.read(["module", "name"])
                #print "===xml_id===",xml_id
                xml_id = "%s.%s" % (xml_id[0]["module"], xml_id[0]["name"])
    
            if context is None:
                context={}
            htmls = []
            if report_xml.report_type != 'webkit':
                return super(WebKitParser, self).create_single_pdf(cursor, uid, ids, data, report_xml, context=context)
    
            parser_instance = self.parser(cursor,
                                          uid,
                                          self.name2,
                                          context=context)
    
            self.pool = registry
            objs = self.getObjects(cursor, uid, ids, context)
            parser_instance.set_context(objs, data, ids, report_xml.report_type)
    
            template =  False
    
            if report_xml.report_file :
                path = get_module_resource(*report_xml.report_file.split('/'))
                if path and os.path.exists(path) :
                    template = file(path).read()
            if not template and report_xml.report_webkit_data :
                template =  report_xml.report_webkit_data
            if not template :
                raise UserError(_('Webkit report template not found!'))
            header = report_xml.webkit_header.html
            footer = report_xml.webkit_header.footer_html
            if not header and report_xml.use_global_header:
                raise UserError(_('No header defined for this Webkit report!') + " " + _('Please set a header in company settings.'))
            if not report_xml.use_global_header :
                header = ''
                default_head = get_module_resource('aos_report_webkit', 'default_header.html')
                with open(default_head,'r') as f:
                    header = f.read()
            css = report_xml.webkit_header.css
            if not css :
                css = ''
    
            translate_call = partial(self.translate_call, parser_instance)
            body_mako_tpl = mako_template(template)
            helper = WebKitHelper(cursor, uid, report_xml.id, context)
            parser_instance.localcontext['helper'] = helper
            parser_instance.localcontext['css'] = css
            parser_instance.localcontext['_'] = translate_call
    
            # apply extender functions
            additional = {}
            if xml_id in _extender_functions:
                for fct in _extender_functions[xml_id]:
                    fct(pool, cr, uid, parser_instance.localcontext, context)
            #print "===report_xml===",report_xml#.precise_mode
            if report_xml.precise_mode:
                ctx = dict(parser_instance.localcontext)
                for obj in parser_instance.localcontext['objects']:
                    ctx['objects'] = [obj]
                    try :
                        html = body_mako_tpl.render(dict(ctx))
                        htmls.append(html)
                    except Exception, e:
                        msg = u"%s" % e
                        _logger.info(msg, exc_info=True)
                        raise UserError(msg)
            else:
Beispiel #2
0
    def create_single_pdf(self, cursor, uid, ids, data, report_xml, context=None):
        """generate the PDF"""

        if context is None:
            context={}
        htmls = []
        if report_xml.report_type != 'webkit':
            return super(WebKitParser,self).create_single_pdf(cursor, uid, ids, data, report_xml, context=context)

        parser_instance = self.parser(cursor,
                                      uid,
                                      self.name2,
                                      context=context)

        self.pool = pooler.get_pool(cursor.dbname)
        objs = self.getObjects(cursor, uid, ids, context)
        parser_instance.set_context(objs, data, ids, report_xml.report_type)

        template =  False

        if report_xml.report_file :
            # backward-compatible if path in Windows format
            report_path = report_xml.report_file.replace("\\", "/")
            path = addons.get_module_resource(*report_path.split('/'))
            if path and os.path.exists(path) :
                template = file(path).read()
        if not template and report_xml.report_webkit_data :
            template =  report_xml.report_webkit_data
        if not template :
            raise except_osv(_('Error!'), _('Webkit report template not found!'))
        header = report_xml.webkit_header.html
        footer = report_xml.webkit_header.footer_html
        if not header and report_xml.header:
            raise except_osv(
                  _('No header defined for this Webkit report!'),
                  _('Please set a header in company settings.')
              )
        if not report_xml.header :
            header = ''
            default_head = addons.get_module_resource('report_webkit', 'default_header.html')
            with open(default_head,'r') as f:
                header = f.read()
        css = report_xml.webkit_header.css
        if not css :
            css = ''

        translate_call = partial(self.translate_call, parser_instance)
        #default_filters=['unicode', 'entity'] can be used to set global filter
        body_mako_tpl = mako_template(template)
        helper = WebKitHelper(cursor, uid, report_xml.id, context)
        if report_xml.precise_mode:
            objs = parser_instance.localcontext['objects']
            for obj in objs:
                parser_instance.localcontext['objects'] = [obj]
                try :
                    html = body_mako_tpl.render(helper=helper,
                                                css=css,
                                                _=translate_call,
                                                **parser_instance.localcontext)
                    htmls.append(html)
                except Exception:
                    msg = exceptions.text_error_template().render()
                    _logger.error(msg)
                    raise except_osv(_('Webkit render!'), msg)
        else:
            try :
                html = body_mako_tpl.render(helper=helper,
                                            css=css,
                                            _=translate_call,
                                            **parser_instance.localcontext)
                htmls.append(html)
            except Exception:
                msg = exceptions.text_error_template().render()
                _logger.error(msg)
                raise except_osv(_('Webkit render!'), msg)
        head_mako_tpl = mako_template(header)
        try :
            head = head_mako_tpl.render(helper=helper,
                                        css=css,
                                        _=translate_call,
                                        _debug=False,
                                        **parser_instance.localcontext)
        except Exception:
            raise except_osv(_('Webkit render!'),
                exceptions.text_error_template().render())
        foot = False
        if footer :
            foot_mako_tpl = mako_template(footer)
            try :
                foot = foot_mako_tpl.render(helper=helper,
                                            css=css,
                                            _=translate_call,
                                            **parser_instance.localcontext)
            except:
                msg = exceptions.text_error_template().render()
                _logger.error(msg)
                raise except_osv(_('Webkit render!'), msg)
        if report_xml.webkit_debug :
            try :
                deb = head_mako_tpl.render(helper=helper,
                                           css=css,
                                           _debug=tools.ustr("\n".join(htmls)),
                                           _=translate_call,
                                           **parser_instance.localcontext)
            except Exception:
                msg = exceptions.text_error_template().render()
                _logger.error(msg)
                raise except_osv(_('Webkit render!'), msg)
            return (deb, 'html')
        bin = self.get_lib(cursor, uid)
        pdf = self.generate_pdf(bin, report_xml, head, foot, htmls)
        return (pdf, 'pdf')
    def create_single_pdf(self, cursor, uid, ids, data, report_xml, context=None):
        """generate the PDF"""

        if context is None:
            context={}
        htmls = []
        if report_xml.report_type != 'webkit':
            return super(WebKitParser,self).create_single_pdf(cursor, uid, ids, data, report_xml, context=context)

        self.parser_instance = self.parser(cursor,
                                           uid,
                                           self.name2,
                                           context=context)

        self.pool = pooler.get_pool(cursor.dbname)
        objs = self.getObjects(cursor, uid, ids, context)
        self.parser_instance.set_context(objs, data, ids, report_xml.report_type)

        template =  False

        if report_xml.report_file :
            path = addons.get_module_resource(report_xml.report_file)
            if os.path.exists(path) :
                template = file(path).read()
        if not template and report_xml.report_webkit_data :
            template =  report_xml.report_webkit_data
        if not template :
            raise except_osv(_('Error!'), _('Webkit Report template not found !'))
        header = report_xml.webkit_header.html
        footer = report_xml.webkit_header.footer_html
        if not header and report_xml.header:
            raise except_osv(
                  _('No header defined for this Webkit report!'),
                  _('Please set a header in company settings')
              )
        if not report_xml.header :
            header = ''
            default_head = addons.get_module_resource('report_webkit', 'default_header.html')
            with open(default_head,'r') as f:
                header = f.read()
        css = report_xml.webkit_header.css
        if not css :
            css = ''
        user = self.pool.get('res.users').browse(cursor, uid, uid)
        company= user.company_id

        #default_filters=['unicode', 'entity'] can be used to set global filter
        body_mako_tpl = mako_template(template)
        helper = WebKitHelper(cursor, uid, report_xml.id, context)
        if report_xml.precise_mode:
            for obj in objs:
                self.parser_instance.localcontext['objects'] = [obj]
                try :
                    html = body_mako_tpl.render(helper=helper,
                                                css=css,
                                                _=self.translate_call,
                                                **self.parser_instance.localcontext)
                    htmls.append(html)
                except Exception, e:
                    msg = exceptions.text_error_template().render()
                    _logger.error(msg)
                    raise except_osv(_('Webkit render'), msg)
Beispiel #4
0
    def create_single_pdf(self,
                          cursor,
                          uid,
                          ids,
                          data,
                          report_xml,
                          context=None):
        """generate the PDF"""

        # just try to find an xml id for the report
        cr = cursor
        pool = openerp.registry(cr.dbname)
        found_xml_ids = pool["ir.model.data"].search(cr, uid, [["model", "=", "ir.actions.report.xml"], \
            ["res_id", "=", report_xml.id]], context=context)
        xml_id = None
        if found_xml_ids:
            xml_id = pool["ir.model.data"].read(cr, uid, found_xml_ids[0],
                                                ["module", "name"])
            xml_id = "%s.%s" % (xml_id["module"], xml_id["name"])

        if context is None:
            context = {}
        htmls = []
        if report_xml.report_type != 'webkit':
            return super(WebKitParser, self).create_single_pdf(cursor,
                                                               uid,
                                                               ids,
                                                               data,
                                                               report_xml,
                                                               context=context)

        self.parser_instance = self.parser(cursor,
                                           uid,
                                           self.name2,
                                           context=context)

        self.pool = pool
        objs = self.getObjects(cursor, uid, ids, context)
        self.parser_instance.set_context(objs, data, ids,
                                         report_xml.report_type)

        template = False

        if report_xml.report_file:
            path = get_module_resource(*report_xml.report_file.split('/'))
            if path and os.path.exists(path):
                template = file(path).read()
        if not template and report_xml.report_webkit_data:
            template = report_xml.report_webkit_data
        if not template:
            raise except_osv(_('Error!'),
                             _('Webkit report template not found!'))
        header = report_xml.webkit_header.html
        footer = report_xml.webkit_header.footer_html
        if not header and report_xml.use_global_header:
            raise except_osv(_('No header defined for this Webkit report!'),
                             _('Please set a header in company settings.'))
        if not report_xml.use_global_header:
            header = ''
            default_head = get_module_resource('report_webkit',
                                               'default_header.html')
            with open(default_head, 'r') as f:
                header = f.read()
        css = report_xml.webkit_header.css
        if not css:
            css = ''

        body_mako_tpl = mako_template(template)
        helper = WebKitHelper(cursor, uid, report_xml.id, context)
        self.parser_instance.localcontext['helper'] = helper
        self.parser_instance.localcontext['css'] = css
        self.parser_instance.localcontext['_'] = self.translate_call

        # apply extender functions
        additional = {}
        if xml_id in _extender_functions:
            for fct in _extender_functions[xml_id]:
                fct(pool, cr, uid, self.parser_instance.localcontext, context)

        if report_xml.precise_mode:
            ctx = dict(self.parser_instance.localcontext)
            for obj in self.parser_instance.localcontext['objects']:
                ctx['objects'] = [obj]
                try:
                    html = body_mako_tpl.render(dict(ctx))
                    htmls.append(html)
                except Exception, e:
                    msg = u"%s" % e
                    _logger.error(msg)
                    raise except_osv(_('Webkit render!'), msg)
Beispiel #5
0
    def create_single_pdf(self,
                          cursor,
                          uid,
                          ids,
                          data,
                          report_xml,
                          context=None):
        """generate the PDF"""

        if context is None:
            context = {}

        if report_xml.report_type != 'webkit':
            return super(WebKitParser, self).create_single_pdf(cursor,
                                                               uid,
                                                               ids,
                                                               data,
                                                               report_xml,
                                                               context=context)

        self.parser_instance = self.parser(cursor,
                                           uid,
                                           self.name2,
                                           context=context)

        self.pool = pooler.get_pool(cursor.dbname)
        objs = self.getObjects(cursor, uid, ids, context)
        self.parser_instance.set_context(objs, data, ids,
                                         report_xml.report_type)

        template = False

        if report_xml.report_file:
            path = addons.get_module_resource(report_xml.report_file)
            if path and os.path.exists(path):
                template = file(path).read()
        if not template and report_xml.report_webkit_data:
            template = report_xml.report_webkit_data
        if not template:
            raise except_osv(_('Error!'),
                             _('Webkit Report template not found !'))
        header = report_xml.webkit_header.html
        footer = report_xml.webkit_header.footer_html
        if not header and report_xml.header:
            raise except_osv(_('No header defined for this Webkit report!'),
                             _('Please set a header in company settings'))
        if not report_xml.header:
            #I know it could be cleaner ...
            header = u"""
<html>
    <head>
        <meta content="text/html; charset=UTF-8" http-equiv="content-type"/>
        <style type="text/css"> 
            ${css}
        </style>
        <script>
        function subst() {
           var vars={};
           var x=document.location.search.substring(1).split('&');
           for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
           var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
           for(var i in x) {
             var y = document.getElementsByClassName(x[i]);
             for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
           }
         }
        </script>
    </head>
<body style="border:0; margin: 0;" onload="subst()">
</body>
</html>"""
        css = report_xml.webkit_header.css
        if not css:
            css = ''
        user = self.pool.get('res.users').browse(cursor, uid, uid)
        company = user.company_id

        #default_filters=['unicode', 'entity'] can be used to set global filter
        body_mako_tpl = mako_template(template)
        helper = WebKitHelper(cursor, uid, report_xml.id, context)
        self.parser_instance.localcontext.update({'setLang': self.setLang})
        self.parser_instance.localcontext.update(
            {'formatLang': self.formatLang})
        try:
            html = body_mako_tpl.render(helper=helper,
                                        css=css,
                                        _=self.translate_call,
                                        **self.parser_instance.localcontext)
        except Exception, e:
            msg = exceptions.text_error_template().render()
            netsvc.Logger().notifyChannel('Webkit render', netsvc.LOG_ERROR,
                                          msg)
            raise except_osv(_('Webkit render'), msg)