【问题标题】:OpenERP custom report filenameOpenERP 自定义报告文件名
【发布时间】:2013-01-04 06:48:42
【问题描述】:

我想自定义报告文件名。

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

【问题讨论】:

  • forum.openerp.com/forum/topic24051.html它接近你想要的..
  • 不是很满意,对我现在工作的公司要求太多工作
  • 是的,它可以随心所欲地完成。比如 invoice_1234、invoice_1235 等。

标签: openerp-7


【解决方案1】:

我找到了适用于 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'])  
    

【讨论】:

    【解决方案2】:
    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)})
      

    【讨论】:

    • 这也适用于 OpenERP 7 吗?
    • 否,因为上下文中没有当前对象了
    【解决方案3】:

    我找到了一种解决方案,可以使用 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
    

    祝你好运

    【讨论】:

      猜你喜欢
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      相关资源
      最近更新 更多