Exemplo n.º 1
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(HeaderFooterTextWebKitParser,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.split(os.path.sep))
            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')
              )

        css = report_xml.webkit_header.css
        if not css:
            css = ''
        user = self.pool.get('res.users').browse(cursor, uid, uid)

        #default_filters=['unicode', 'entity'] can be used to set global filter
        template = unicode(template)
        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)
Exemplo n.º 2
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)

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

    self.pool = pool
    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 = unicode(file(path).read(),'UTF-8')
    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 = ''

    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
    parser_instance.localcontext['context'] = context

    # 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)

    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.error(msg)
                raise except_osv(_('Webkit render!'), msg)
Exemplo n.º 3
0
         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)
 else:
     try :
         html = body_mako_tpl.render(dict(parser_instance.localcontext))
         htmls.append(html)
     except Exception, e:
         msg = u"%s" % e
         _logger.error(msg)
         raise except_osv(_('Webkit render!'), msg)
 head_mako_tpl = mako_template(header)
 try :
     head = head_mako_tpl.render(dict(parser_instance.localcontext, _debug=False))
 except Exception, e:
     raise except_osv(_('Webkit render!'), u"%s" % e)
 foot = False
 if footer :
     foot_mako_tpl = mako_template(footer)
     try :
         foot = foot_mako_tpl.render(dict(parser_instance.localcontext))
     except Exception, e:
         msg = u"%s" % e
         _logger.error(msg)
         raise except_osv(_('Webkit render!'), msg)
 if report_xml.webkit_debug :
     try :
Exemplo n.º 4
0
def new_create_single_pdf(self, cursor, uid, ids, data,
                          report_xml, context=None):
    """ Generate the PDF """
    context = context or {}
    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)

    template = False
    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)

    if report_xml.report_file:
        path = addons.get_module_resource(
            *report_xml.report_file.split(os.path.sep))
        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

    #jon   if the parser includes
    #get_webkit_header_by_partner  function,
    #use this function replace header
    header_function = getattr(self.parser, 'get_webkit_header_by_partner',
                              False)
    _logger.debug(header_function)

    if header_function:
        header_function = self.parser.get_webkit_header_by_partner
        partner_header = header_function(cursor, uid, ids)
        header = partner_header and partner_header.html or header

    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 = ''

    #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:
                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,
                                        _=self.translate_call,
                                        **self.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,
                                    _=self.translate_call,
                                    _debug=False,
                                    **self.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,
                                        _=self.translate_call,
                                        **self.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)),
                                       _=self.translate_call,
                                       **self.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')
Exemplo n.º 5
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)

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

    self.pool = pool
    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 = unicode(file(path).read(), 'UTF-8')
    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 = ''

    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
    parser_instance.localcontext['context'] = context

    # 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)

    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.error(msg)
                raise except_osv(_('Webkit render!'), msg)
Exemplo n.º 6
0
         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)
 else:
     try:
         html = body_mako_tpl.render(dict(parser_instance.localcontext))
         htmls.append(html)
     except Exception, e:
         msg = u"%s" % e
         _logger.error(msg)
         raise except_osv(_('Webkit render!'), msg)
 head_mako_tpl = mako_template(header)
 try:
     head = head_mako_tpl.render(
         dict(parser_instance.localcontext, _debug=False))
 except Exception, e:
     raise except_osv(_('Webkit render!'), u"%s" % e)
 foot = False
 if footer:
     foot_mako_tpl = mako_template(footer)
     try:
         foot = foot_mako_tpl.render(dict(parser_instance.localcontext))
     except Exception, e:
         msg = u"%s" % e
         _logger.error(msg)
         raise except_osv(_('Webkit render!'), msg)
 if report_xml.webkit_debug:
Exemplo n.º 7
0
def new_create_single_pdf(self,
                          cursor,
                          uid,
                          ids,
                          data,
                          report_xml,
                          context=None):
    """ Generate the PDF """
    context = context or {}
    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)

    template = False
    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)

    if report_xml.report_file:
        path = addons.get_module_resource(
            *report_xml.report_file.split(os.path.sep))
        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

    #jon   if the parser includes
    #get_webkit_header_by_partner  function,
    #use this function replace header
    header_function = getattr(self.parser, 'get_webkit_header_by_partner',
                              False)
    _logger.debug(header_function)

    if header_function:
        header_function = self.parser.get_webkit_header_by_partner
        partner_header = header_function(cursor, uid, ids)
        header = partner_header and partner_header.html or header

    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 = ''

    #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:
                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,
                                        _=self.translate_call,
                                        **self.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,
                                    _=self.translate_call,
                                    _debug=False,
                                    **self.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,
                                        _=self.translate_call,
                                        **self.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)),
                                       _=self.translate_call,
                                       **self.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')