如何在 Odoo 中的公共 URL 上呈现自定义 QWeb 报告

问题描述 投票:0回答:1

在 odoo 中,我们可以通过令牌 url 访问内置对象,如下所示:

http://localhost:8069/my/invoices/8336?access_token=96c52b2d-3604-4817-82be-4392aa8d051e&report_type=pdf

如果我设计了这样的自定义 qweb 报告 PDF

<?xml version="1.0" encoding="utf-8" ?>
<odoo>
    <template id="vendor_bill_report">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="o">
                <!-- Removing the default Odoo header/footer by avoiding t-call to web.external_layout -->
                <html lang="en">
                </html>
            </t>
        </t>
    </template>

    <record id="paperformat_sensorglobal_vendor_bill" model="report.paperformat">
        <field name="name">Sensorglobal Vendor Bill</field>
        <field name="default" eval="False"/>
        <field name="format">custom</field>
        <field name="page_height">266</field>
        <field name="page_width">210</field>
        <field name="margin_top">0</field>
        <field name="margin_bottom">0</field>
        <field name="margin_left">0</field>
        <field name="margin_right">0</field>
        <field name="orientation">Portrait</field>
        <field name="dpi">100</field>
    </record>
    
    <!-- Report Action -->
    <record id="report_account_move_action" model="ir.actions.report">
        <field name="name">SG Vendor Bill</field>
        <field name="model">account.move</field>
        <field name="report_type">qweb-pdf</field>
        <field name="report_name">module_name.vendor_bill_report</field>
        <field name="report_file">module_name.vendor_bill_report</field>
        <field name="print_report_name">'V_%s_%s' % (object.name, object.create_date.strftime("%b_%y").upper())</field>
        <field name="binding_model_id" ref="account.model_account_move" />
        <field name="binding_type">report</field>
        <field name="paperformat_id" ref="module_name.paperformat_sensorglobal_vendor_bill"/>
    </record>
</odoo>

如果我想通过自定义网址访问此 PDF,如下所示: http://localhost:8069/my/custom_vendor_bills/8337?access_token=e5751590-8027-4f2c-b5ab-a0d62f675721&report_type=pdf

我怎样才能实现这一点,它尝试添加一些控制器代码,但它不起作用:

from odoo import http
from odoo.http import request

class CustomVendorBillReport(http.Controller):

@http.route(['/my/custom_vendor_bills/<int:move_id>'], type='http', auth="public", website=True)
def custom_vendor_bill_report(self, move_id, access_token=None, **kw):
    try:
        # Get the account.move object based on the move_id and access token
        move = request.env['account.move'].sudo().browse(move_id)
        print("move", move)

        if not move or not move.check_access_rights('read', raise_exception=False):
            print("test")
            return request.redirect('/my')  # Redirect to a default page if unauthorized

        # Generate the report
        pdf = request.env.ref('ir.actions.report').sudo()._render_qweb_html('module_name.vendor_bill_report', [move_id])[0]
        pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))]

        return request.make_response(pdf, headers=pdfhttpheaders)

    except Exception as e:
        return request.not_found()

任何人都可以帮助我在公共网址上呈现自定义 qweb 报告。我正在使用 odoo 版本 16 TIA

controller odoo qweb odoo-16
1个回答
0
投票
class CustomVendorBillReport(http.Controller):

    @http.route(['/my/vendor_bills/<int:move_id>'], type='http', auth="public", website=True)
    def custom_vendor_bill_report(self, move_id, access_token=None, **kw):
        try:
            # Get the account.move object based on the move_id and access token
            move = request.env['account.move'].sudo().browse(move_id)
            print("move", move)

            if not move or not move.check_access_rights('read', raise_exception=False):
                print("test")
                return request.redirect('/my')  # Redirect to a default page if unauthorized

            pdf, _ = request.env['ir.actions.report'].sudo()._render_qweb_pdf('sg_util.vendor_bill_report', [move_id])
            pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))]
            return request.make_response(pdf, headers=pdfhttpheaders)

        except Exception as e:
            return request.not_found()

这段代码终于成功了!

© www.soinside.com 2019 - 2024. All rights reserved.