OpenERP report file name

I would like to customize the name of the reports file.

For example, when I upload an invoice, I will have something like "Invoice.pdf" as the filename. I would like something like "invoice_number.pdf" but I don't know how to use a dynamic filename?

+3


source to share


3 answers


I found a way for 7.0 and the current trunk:



  • For a quick fix, check out the Reports class in:

    openerp-7.0\web\addons\web\controllers\main.py
    
          

  • The name of the report is set in a variable named file_name:

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

  • Find this line and insert this part before it:

    '''
      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


source


  • For a quick fix, check out the Reports class in:

    openerp-6.1\web\addons\web\controllers\main.py
    
          

  • The name of the report is set in the header of the named variable. For my needs, I want the report to be named with the name of the object (which is fine in 99% of cases), so I added a new variable called 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)})
    
          



+1


source


I found a solution to set a default file name like the file name of the attached file using the expression in the attachment attribute in the xml report definition.

Found and modify this file: openerp7 / openerp-web / addons / web / controller / main.py.

Search for this offer:

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

      

Then add this code before:

    '''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

      

Good luky

+1


source







All Articles