Qweb,阻止报告

Qweb, Blocking the report

我需要在草稿状态下阻止报告,如果用户在草稿状态下单击打印按钮生成 pdf,它应该会发出警告消息。

提前致谢

一般情况下Qweb报表可以两种方式打印

  1. HTML
  2. PDF

每次根据报告类型调用报告时,都会听到不同的报告方法正在调用。

如果您将报告调用为 PDF,则调用 get_pdf() 方法,或者如果您调用报告输入 HTML 然后 get_html() 方法 被调用报告模块。

因此在我们的例子中,您必须在我们的模块中重写上述两个方法,然后添加类似这样的东西。

覆盖报表模块的get_pdf()方法:

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
    )

与方法相同,您可以在模块中覆盖 get_html() 并检查它

听到代码将检查销售订单报告操作。

以上代码我这边可以测试成功。

希望这对您有所帮助..:)