如何更改打印的qweb文件名

时间:2017-04-03 03:39:06

标签: python xml openerp qweb

如何更改打印的qweb文件名取决于源文档字段名称? 我在模特stock.picking

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

EXAMPLE

3 个答案:

答案 0 :(得分:1)

您可以使用配置提供动态报告名称,但在打印一份报告时将适用。

以下是报告中打印自定义名称的示例。创建 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. 2.Report.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/

    这可能会对你有帮助。

答案 1 :(得分:0)

在您的reprot中尝试附件,将报告文件名更改为您想要的名称。

<report
            string="Delivery Slip"
            id="action_report_delivery"
            model="stock.picking"
            report_type="qweb-pdf"
            name="stock.report_deliveryslip"
            file="stock.report_deliveryslip"
            attachment="'Custom Text...'+'.pdf')"
        />

答案 2 :(得分:0)

Here is my code which works for different reportname in the same model. So the filename only changes for the certain filename.

> 
from openerp.addons.web.http import Controller, route, request
from openerp.addons.web.controllers.main import _serialize_exception
from openerp.osv import osv
from openerp.addons.report.controllers.main import ReportController
from openerp import http
import simplejson
import logging

class SponsorReportController(ReportController):

    @route(['/report/download'], type='http', auth="user")
    def report_download(self, data, token):
        requestcontent = simplejson.loads(data)
        url, type = requestcontent[0], requestcontent[1]

        logging.info(url)
        logging.info(type)

        response = ReportController().report_download(data, token)

        if len(url.split('/report/pdf/')) > 1 and type == 'qweb-pdf':

            reportname = url.split('/report/pdf/')[1].split('?')[0]
            reportname, docids = reportname.split('/')
            logging.info(reportname)
            assert docids
            logging.info(docids)

            if reportname == 'pci_stock_picking_report.report_delivery_note':
                partner_obj = http.request.env['stock.picking']
                object = partner_obj.browse(int(docids))
                filename = "No."+(object.origin)
                response.headers.set('Content-Disposition', 'attachment; filename=%s.pdf;' % filename)

        return response