2013-01-21 54 views
3

我想自定义报告文件名。OpenERP自定义报告文件名

例如,当我下载发票时,我会将'Invoice.pdf'作为文件名。我想要的是像'invoice_number.pdf',但我不知道如何使用动态文件名?

+0

看到http://forum.openerp.com/forum/topic24051.html它接近你想要的.. –

+0

不是很满意,为一家像我正在为 –

+0

工作的公司提出太多的工作是的,它可以按你的意愿完成。像invoice_1234,invoice_1235等。 –

回答

1
  1. 为了速战速决,看看报表类:

    openerp-6.1\web\addons\web\controllers\main.py 
    
  2. 报告名称是一个变量指定的头设置。 对于我的需求,我只希望报告能为对象名称来命名(即在案件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)}) 
    
+0

这是否也适用于OpenERP 7? –

+0

没有,因为上下文没有上下文中的当前对象 –

2

我找到一种方式, 7.0和当前的中继线:

  1. 快速修复,采取看看报告类:

    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']) 
    
+0

Yann Papouin伟大的工作.. !!! –

+0

你的答案真的帮了我很多.. !!! :) –

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 

好luky