Search code examples
openerp-8odoo-8qweb

Qweb, Blocking the report


I need to block the report at the draft state, In draft state if the user click the print button to generate the pdf it should raise a warning message.

Thanks in advance


Solution

  • In General Case Qweb Report Can Be printed in Two Way

    1. HTML
    2. PDF

    Hear each and every time when you call the report based on report type the different report method is calling.

    If you call the report as PDF then the get_pdf() method is called or if you call the report type as HTML then get_html() method is called of report module.

    so that in our case you must have to override the above two method in our module then add some thing like this.

    Override the get_pdf() method of report module :

    class Report(osv.Model):
        _inherit = "report"
        _description = "Report"
    
    @api.v7
    def get_pdf(self, cr, uid, ids, report_name, html=None, data=None, context=None):
        """This method generates and returns pdf version of a report.
        """
        order_pool=self.pool.get('sale.order')
        for order in order_pool.browse(cr, uid, ids, context=None):
            if order.state:
                if order.state == 'draft':
                    raise osv.except_osv(_("Warning!"), _("Your Printed Report is in Draft State ...!! "))
    
        if context is None:
            context = {}
    
        if html is None:
            html = self.get_html(cr, uid, ids, report_name, data=data, context=context)
    
        html = html.decode('utf-8')  # Ensure the current document is utf-8 encoded.
    
        # Get the ir.actions.report.xml record we are working on.
    
        report = self._get_report_from_name(cr, uid, report_name)
        # Check if we have to save the report or if we have to get one from the db.
        save_in_attachment = self._check_attachment_use(cr, uid, ids, report)
        # Get the paperformat associated to the report, otherwise fallback on the company one.
        if not report.paperformat_id:
            user = self.pool['res.users'].browse(cr, uid, uid)
            paperformat = user.company_id.paperformat_id
        else:
            paperformat = report.paperformat_id
    
        # Preparing the minimal html pages
        css = ''  # Will contain local css
        headerhtml = []
        contenthtml = []
        footerhtml = []
        irconfig_obj = self.pool['ir.config_parameter']
        base_url = irconfig_obj.get_param(cr, SUPERUSER_ID, 'report.url') or irconfig_obj.get_param(cr, SUPERUSER_ID, 'web.base.url')
    
        # Minimal page renderer
        view_obj = self.pool['ir.ui.view']
        render_minimal = partial(view_obj.render, cr, uid, 'report.minimal_layout', context=context)
    
        # The received html report must be simplified. We convert it in a xml tree
        # in order to extract headers, bodies and footers.
        try:
            root = lxml.html.fromstring(html)
            match_klass = "//div[contains(concat(' ', normalize-space(@class), ' '), ' {} ')]"
    
            for node in root.xpath("//html/head/style"):
                css += node.text
    
            for node in root.xpath(match_klass.format('header')):
                body = lxml.html.tostring(node)
                header = render_minimal(dict(css=css, subst=True, body=body, base_url=base_url))
                headerhtml.append(header)
    
            for node in root.xpath(match_klass.format('footer')):
                body = lxml.html.tostring(node)
                footer = render_minimal(dict(css=css, subst=True, body=body, base_url=base_url))
                footerhtml.append(footer)
    
            for node in root.xpath(match_klass.format('page')):
                # Previously, we marked some reports to be saved in attachment via their ids, so we
                # must set a relation between report ids and report's content. We use the QWeb
                # branding in order to do so: searching after a node having a data-oe-model
                # attribute with the value of the current report model and read its oe-id attribute
                if ids and len(ids) == 1:
                    reportid = ids[0]
                else:
                    oemodelnode = node.find(".//*[@data-oe-model='%s']" % report.model)
                    if oemodelnode is not None:
                        reportid = oemodelnode.get('data-oe-id')
                        if reportid:
                            reportid = int(reportid)
                    else:
                        reportid = False
    
                # Extract the body
                body = lxml.html.tostring(node)
                reportcontent = render_minimal(dict(css=css, subst=False, body=body, base_url=base_url))
    
                contenthtml.append(tuple([reportid, reportcontent]))
    
        except lxml.etree.XMLSyntaxError:
            contenthtml = []
            contenthtml.append(html)
            save_in_attachment = {}  # Don't save this potentially malformed document
    
        # Get paperformat arguments set in the root html tag. They are prioritized over
        # paperformat-record arguments.
        specific_paperformat_args = {}
        for attribute in root.items():
            if attribute[0].startswith('data-report-'):
                specific_paperformat_args[attribute[0]] = attribute[1]
    
        # Run wkhtmltopdf process
        return self._run_wkhtmltopdf(
            cr, uid, headerhtml, footerhtml, contenthtml, context.get('landscape'),
            paperformat, specific_paperformat_args, save_in_attachment
        )
    

    As same as method you can override as get_html() in your module and check it

    Hear the code will check the sale order report action.

    Above code can be tested successfully from my side.

    I hope this should helpful for you ..:)