gpt4 book ai didi

python - 如何更改打印的 qweb 文件名

转载 作者:太空宇宙 更新时间:2023-11-04 05:10:12 27 4
gpt4 key购买 nike

如何根据源文档字段名更改打印的qweb文件名?我在模型 stock.picking 中

因此,当我单击打印 -> 交货单时,将打印 qweb,但文件名将取决于源文档字段。在这里,我展示了解释我的意思的图片。

EXAMPLE

最佳答案

您可以使用配置给动态报​​告名称,但它会在您打印一份报告时应用。

下面是在报告中打印自定义名称的例子。在ir.actions.report.xml创建一个字段,其中用户可以配置报告名称

from openerp import models, fields
class IrActionsReportXml(models.Model):
_inherit = 'ir.actions.report.xml'

download_filename = fields.Char(
'Download filename')

现在您需要创建两个文件。

  1. 报告控制者

    from openerp import http
    from openerp.addons.mail.models import mail_template
    from openerp.addons.report.controllers.main import ReportController
    from openerp.addons.web.controllers.main import content_disposition


    class ReportController(ReportController):
    @http.route([
    '/report/<path:converter>/<reportname>',
    '/report/<path:converter>/<reportname>/<docids>',
    ])
    def report_routes(self, reportname, docids=None, converter=None, **data):
    response = super(ReportController, self).report_routes(
    reportname, docids=docids, converter=converter, **data)
    if docids:
    docids = [int(i) for i in docids.split(',')]
    report_xml = http.request.session.model('ir.actions.report.xml')
    report_ids = report_xml.search(
    [('report_name', '=', reportname)])
    for report in report_xml.browse(report_ids):
    if not report.download_filename:
    continue
    objects = http.request.session.model(report.model)\
    .browse(docids or [])
    generated_filename = mail_template.mako_template_env\
    .from_string(report.download_filename)\
    .render({
    'objects': objects,
    'o': objects[:1],
    'object': objects[:1],
    'ext': report.report_type.replace('qweb-', ''),
    })
    response.headers['Content-Disposition'] = content_disposition(
    generated_filename)
    return response

    @http.route(['/report/download'])
    def report_download(self, data, token):
    response = super(ReportController, self).report_download(data, token)
    # if we got another content disposition before, ditch the one added
    # by super()
    last_index = None
    for i in range(len(response.headers) - 1, -1, -1):
    if response.headers[i][0] == 'Content-Disposition':
    if last_index:
    response.headers.pop(last_index)
    last_index = i
    return response

2.报表.py

    import json
from openerp import http
from openerp.addons.web.controllers import main
from openerp.addons.mail.models import mail_template


class Reports(main.Reports):
@http.route('/web/report', type='http', auth="user")
@main.serialize_exception
def index(self, action, token):
result = super(Reports, self).index(action, token)
action = json.loads(action)
context = dict(http.request.context)
context.update(action["context"])
report_xml = http.request.env['ir.actions.report.xml']
reports = report_xml.search([
('report_name', '=', action['report_name']),
('download_filename', '!=', False)])
for report in reports:
objects = http.request.session.model(context['active_model'])\
.browse(context['active_ids'])
generated_filename = mail_template.mako_template_env\
.from_string(report.download_filename)\
.render({
'objects': objects,
'o': objects[0],
'object': objects[0],
})
result.headers['Content-Disposition'] = main.content_disposition(
generated_filename)
return result

Odoo 社区为我们提供了报告自定义名称的默认模块。您可以直接安装此模块并设置报告名称,如:${o.name}

这里的o代表你的记录。

下面是odoo社区模块的链接。

https://www.odoo.com/apps/modules/9.0/report_custom_filename/

这可能对你有帮助。

关于python - 如何更改打印的 qweb 文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43176154/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com