QWeb 报告¶
报告以 HTML/QWeb 编写,就像 Odoo 中的网站视图一样。您可以使用通常的 QWeb control flow tools。 PDF 渲染本身由 wkhtmltopdf 执行。
报告使用 report action 进行声明,并使用 报告模板 表示要使用的操作。
如果有用或必要,可以为报告指定 论文格式。
报告模板¶
报告模板将始终提供以下变量:
timePython 标准库中对
time的引用user打印报告的用户的“
res.user”记录res_company当前“
user”公司的记录website当前网站对象(如果有)(此项可以存在,但``None``)
web_base_url网络服务器的基本 url
context_timestamp函数采用 UTC1 中的
datetime.datetime并将其转换为打印报告的用户的时区
最小可行模板¶
最小模板如下所示:
<template id="report_invoice">
<t t-call="web.html_container">
<t t-foreach="docs" t-as="o">
<t t-call="web.external_layout">
<div class="page">
<h2>Report title</h2>
<p>This object's name is <span t-field="o.name"/></p>
</div>
</t>
</t>
</t>
</template>
调用模板接收到的``external_layout`` will add the default header and footer on your report. The PDF body will be the content inside the <div class="page">. The template’s id must be the name specified in the report declaration; for example account.report_invoice for the above report. Since this is a QWeb template, you can access all the fields of the ``docs``对象。
默认情况下,渲染上下文还将公开以下项目:
docs当前报告的记录
doc_idsdocs记录的 ID 列表doc_modeldocs记录的模型
如果您希望访问模板中的其他记录/模型,则需要 a custom report,但在这种情况下,您必须提供上述项目(如果需要)。
可翻译模板¶
如果您希望翻译报告(例如,翻译成合作伙伴的语言),您需要定义两个模板:
主要报告模板
可翻译的文件
然后,您可以使用属性“t-lang` set to a language code (for example fr or ``en_US`”从主模板调用可翻译文档或调用记录字段。如果您使用可翻译的字段(例如国家/地区名称、销售条件等),您还需要在适当的上下文中重新浏览相关记录。
警告
如果您的报告模板不使用可翻译的记录字段,则“没有”必要以另一种语言重新浏览记录,并且会影响性能。
例如,让我们看一下销售模块中的销售订单报告:
<!-- Main template -->
<template id="report_saleorder">
<t t-call="web.html_container">
<t t-foreach="docs" t-as="doc">
<t t-call="sale.report_saleorder_document" t-lang="doc.partner_id.lang"/>
</t>
</t>
</template>
<!-- Translatable template -->
<template id="report_saleorder_document">
<!-- Re-browse of the record with the partner lang -->
<t t-set="doc" t-value="doc.with_context(lang=doc.partner_id.lang)" />
<t t-call="web.external_layout">
<div class="page">
<div class="oe_structure"/>
<div class="row">
<div class="col-6">
<strong t-if="doc.partner_shipping_id == doc.partner_invoice_id">Invoice and shipping address:</strong>
<strong t-if="doc.partner_shipping_id != doc.partner_invoice_id">Invoice address:</strong>
<div t-field="doc.partner_invoice_id" t-options="{"no_marker": True}"/>
<...>
<div class="oe_structure"/>
</div>
</t>
</template>
主模板使用“doc.partner_id.lang` as a ``t-lang`”参数调用可翻译模板,因此它将以合作伙伴的语言呈现。这样,每个销售订单都将以相应客户的语言打印。如果您只想翻译文档的正文,但将页眉和页脚保留为默认语言,则可以通过以下方式调用报告的外部布局:
<t t-call="web.external_layout" t-lang="en_US">
小技巧
请注意,这仅在调用外部模板时有效,您将无法通过设置``t-lang`` attribute on an xml node other than t-call. If you wish to translate part of a template, you can create an external template with this partial template and call it from the main one with the ``t-lang``属性来翻译文档的部分内容。
条形码¶
条形码是控制器返回的图像,借助 QWeb 语法可以轻松嵌入到报告中(例如,参见 属性):
<img t-att-src="'/report/barcode/QR/%s' % 'My text in qr code'"/>
更多参数可以作为查询字符串传递
<img t-att-src="'/report/barcode/?
barcode_type=%s&value=%s&width=%s&height=%s'%('QR', 'text', 200, 200)"/>
有用的备注¶
Twitter Bootstrap 和 FontAwesome 类可以在您的报告模板中使用
本地CSS可以直接放入模板中
通过继承其模板并插入您的 CSS,可以将全局 CSS 插入到主报告布局中:
<template id="report_saleorder_style" inherit_id="report.style"> <xpath expr="."> <t> .example-css-class { background-color: red; } </t> </xpath> </template>
如果您的 PDF 报告似乎缺少样式,请检查 these instructions。
论文格式¶
论文格式是“report.paperformat”的记录,可以包含以下属性:
- ``name``(强制)
仅在在某种列表中查找报告时用作报告的助记符/描述
description对您的格式的简短描述
format预定义格式(A0 到 A9、B0 到 B10、Legal、Letter、Tabloid…)或
custom;默认为 A4。如果定义页面尺寸,则不能使用非自定义格式。dpi输出DPI;默认90
margin_top,margin_bottom,margin_left,margin_right边距尺寸(毫米)
page_height,page_width页面尺寸(毫米)
orientation横向或纵向
header_line显示标题行的布尔值
header_spacing标题间距(毫米)
例子::
<record id="paperformat_frenchcheck" model="report.paperformat">
<field name="name">French Bank Check</field>
<field name="default" eval="True"/>
<field name="format">custom</field>
<field name="page_height">80</field>
<field name="page_width">175</field>
<field name="orientation">Portrait</field>
<field name="margin_top">3</field>
<field name="margin_bottom">3</field>
<field name="margin_left">3</field>
<field name="margin_right">3</field>
<field name="header_line" eval="False"/>
<field name="header_spacing">3</field>
<field name="dpi">80</field>
</record>
定制报告¶
默认情况下,报告系统根据通过“model”字段指定的目标模型构建渲染值。
但是,它首先会查找名为 report.module.report_name 的模型并调用该模型的 _get_report_values(doc_ids, data)` 以便为模板准备渲染数据。
这可用于包含渲染模板时要使用或显示的任意项目,例如来自其他模型的数据:
from odoo import api, models
class ParticularReport(models.AbstractModel):
_name = 'report.module.report_name'
def _get_report_values(self, docids, data=None):
# get the report action back as we will need its data
report = self.env['ir.actions.report']._get_report_from_name('module.report_name')
# get the records selected for this rendering of the report
obj = self.env[report.model].browse(docids)
# return a custom rendering context
return {
'lines': docids.get_lines()
}
警告
使用自定义报告时,“默认”文档相关项目(doc_ids, doc_model and docs)将*不*包含在内。如果您想要它们,您需要自己将它们包括在内。
在上面的示例中,渲染上下文将包含“全局”值以及我们放入其中的“lines”,但仅此而已。
自定义字体¶
如果您想使用自定义字体,则需要将自定义字体和相关的 less/CSS 添加到 web.reports_assets_common assets bundle. Adding your custom font(s) to web.assets_common or web.assets_backend 不会使您的字体在 QWeb 报告中可用。
例子::
<template id="report_assets_common_custom_fonts" name="Custom QWeb fonts" inherit_id="web.report_assets_common">
<xpath expr="." position="inside">
<link href="/your_module/static/src/less/fonts.less" rel="stylesheet" type="text/less"/>
</xpath>
</template>
您需要定义“@font-face` within this less file, even if you’ve used in another assets bundle (other than ``web.reports_assets_common`”)。
例子::
@font-face {
font-family: 'MonixBold';
src: local('MonixBold'), local('MonixBold'), url(/your_module/static/fonts/MonixBold-Regular.otf) format('opentype');
}
.h1-title-big {
font-family: MonixBold;
font-size: 60px;
color: #3399cc;
}
将 less 添加到资产包后,您可以在自定义 QWeb 报告中使用这些类(在本示例中为“h1-title-big”)。
报告是网页¶
报告由报告模块动态生成,可以通过 URL 直接访问:
例如,您可以通过转至 http://<server-address>/report/html/sale.report_saleorder/38 以 html 模式访问销售订单报告
或者您可以访问 pdf 版本 http://<server-address>/report/pdf/sale.report_saleorder/38
- 1
无论
python:datetime对象实际位于哪个时区(包括没有时区),它的时区将无条件“设置”为 UTC,然后再调整为用户的时区