3

我想自定义报告文件名。

例如,当我下载发票时,我会有类似“Invoice.pdf”的文件名。我想要的是类似'invoice_number.pdf'但我不知道如何使用动态文件名?

4

3 回答 3

2

我找到了 7.0 和当前主干的方法:

  1. 如需快速修复,请查看以下中的 Reports 类:

    openerp-7.0\web\addons\web\controllers\main.py
    
  2. 报告名称在名为 file_name 的变量中设置:

    file_name = '%s.%s' % (file_name, report_struct['format'])
    
  3. 找到这一行并在它之前插入这个部分:

    '''
      YP: Added proc. to create reports with real object names (the object must have a field named "name"):
      eg: "PO00006.pdf" instead of "Request For Quotation.pdf" for a single object report
          "PO00006-PO00002.pdf" instead of "Request For Quotation.pdf" for a multiple objects report
    '''
    
    # Try to get current object model and their ids from context
    if action.has_key('context'):
        action_context = action.get('context',{})
        if action_context.has_key('active_model') and action_context.has_key('active_id'):
            action_active_model = action_context.get('active_model','')
            action_active_ids = action_context.get('active_ids', [])
            if action_active_model and action_active_ids:
                # Use built-in ORM method to get data from DB
                m = req.session.model(action_active_model)
                r = m.read(action_active_ids, False, context)
                # Parse result to create a better filename
                for i, item in enumerate(r):
                  if item.has_key('name'):
                    if i == 0: 
                      file_name = ('%s') % (item['name'])
                    else:
                      file_name = ('%s-%s') % (file_name, item['name'])  
    
于 2013-07-04T11:07:08.613 回答
1

我找到了一种解决方案,可以使用 xml 报告定义中的附件属性中的表达式来设置默认文件名,如附件文件名。

找到并修改这个文件:openerp7/openerp-web/addons/web/controllers/main.py。

搜索这句话:

        file_name = '%s.%s' % (file_name, report_struct['format'])

然后在之前添加此代码:

    '''Code added by Raul Paz to set the default file_name like the attach_name
        The attach_name mach with the attachment expresion from your report_xml
        <report
            id="report_webkit.YourModule_report_id"
            model="YourModule.model"        
            name="Your_report_name"
            file="YOUR_MODULE/report/YOUR_MAKO_FILE.mako"
            string="Your Text in the print button" 
            auto="False" 
            report_type="webkit"
            attachment="'Valid_filename_expresion"
            usage="default"
        />
        And
        to modify existing report sale_report.xml to manage the name:
        create in your module a file : sale_report.xml
        <?xml version="1.0" encoding="UTF-8"?>
        <openerp>
           <data>
              <record id="sale.report_sale_order" model="ir.actions.report.xml">
                  <field name="attachment">(object.name or 'Sale Order')</field>
          <field name="attachment_use" eval="True"/>
      </record>
          </data>
        </openerp>

    '''
    try:
        if action.get('attachment_use',False) and action['attachment']:
            model = context['active_model']
            cr = openerp.pooler.get_db(req.session._db).cursor()
            uid = context['uid']
            ids = context['active_ids']
            objects=openerp.pooler.get_pool(req.session._db).get(model).browse(cr,uid,ids,context=context)
            file_name=[eval(action['attachment'],{'object':x, 'time':time}) for x in objects][0]
    except:
        pass
    #end code added

好运

于 2013-10-25T11:57:19.653 回答
1
  1. 如需快速修复,请查看以下中的 Reports 类:

    openerp-6.1\web\addons\web\controllers\main.py
    
  2. 报告名称设置在名为 header 的变量中。出于我的需要,我只希望报表以对象名称命名(这适用于 99% 的情况),因此我添加了一个名为 report_name 的新变量:

    report_name = action['report_name']
    if action.has_key('context'):
        action_context = action.get('context',{})
        if action_context.has_key('name'):
            report_name = action_context['name'] 
    
    return req.make_response(report,
         headers=[
             ('Content-Disposition', 'attachment; filename="%s.%s"' % (report_name, report_struct['format'])),
             ('Content-Type', report_mimetype),
             ('Content-Length', len(report))],
         cookies={'fileToken': int(token)})
    
于 2013-07-03T14:57:47.670 回答