I would like to customize reports filename.
For instance, when I download an invoice, I will have something like 'Invoice.pdf' as filename. What I would like is something like 'invoice_number.pdf' but I don't know how to use dynamic file name ?
I would like to customize reports filename.
For instance, when I download an invoice, I will have something like 'Invoice.pdf' as filename. What I would like is something like 'invoice_number.pdf' but I don't know how to use dynamic file name ?
I found a way for 7.0 and current trunk:
For a quick fix, take a look at the Reports class in:
openerp-7.0\web\addons\web\controllers\main.py
The report name 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'])
For a quick fix, take a look at the Reports class in:
openerp-6.1\web\addons\web\controllers\main.py
The report name is set in a variable named header. For my needs, I only want the report to be named by the object name (that's suitable in 99% of cases) so I added a new variable named 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)})
I found a solution to set the default filename like the attach file name, using the expression in the attachment attribute in your xml report definition.
Found and modify this file: openerp7/openerp-web/addons/web/controllers/main.py.
Search this sentence:
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