Merge lp:~therp-nl/openerp-web/7.0_lp1261322_better_report_name into lp:openerp-web/7.0

Proposed by Ronald Portier (Therp)
Status: Needs review
Proposed branch: lp:~therp-nl/openerp-web/7.0_lp1261322_better_report_name
Merge into: lp:openerp-web/7.0
Diff against target: 37 lines (+17/-9)
1 file modified
addons/web/controllers/main.py (+17/-9)
To merge this branch: bzr merge lp:~therp-nl/openerp-web/7.0_lp1261322_better_report_name
Reviewer Review Type Date Requested Status
OpenERP Core Team Pending
Review via email: mp+220777@code.launchpad.net

Description of the change

Make sure printed reports have sensible filenames.

Fixes lp1261322.

To post a comment you must log in.

Unmerged revisions

4187. By Ronald Portier (Therp)

[ENH] Make filename for print download dependent on object name.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'addons/web/controllers/main.py'
2--- addons/web/controllers/main.py 2014-02-21 11:58:55 +0000
3+++ addons/web/controllers/main.py 2014-05-23 09:52:23 +0000
4@@ -1751,16 +1751,24 @@
5 report = zlib.decompress(report)
6 report_mimetype = self.TYPES_MAPPING.get(
7 report_struct['format'], 'octet-stream')
8- file_name = action.get('name', 'report')
9- if 'name' not in action:
10- reports = req.session.model('ir.actions.report.xml')
11- res_id = reports.search([('report_name', '=', action['report_name']),],
12- 0, False, False, context)
13- if len(res_id) > 0:
14- file_name = reports.read(res_id[0], ['name'], context)['name']
15- else:
16- file_name = action['report_name']
17+ file_name = action['report_name']
18+ # Try to get current object model and their ids from context
19+ if 'context' in action:
20+ action_context = action['context']
21+ if (action_context.get('active_model')
22+ and action_context['active_ids']):
23+ # Use built-in ORM method to get data from DB
24+ m = req.session.model(action_context['active_model'])
25+ r = m.name_get(action_context['active_ids'], context)
26+ # Parse result to create a better filename
27+ item_names = [item[1] or str(item[0]) for item in r]
28+ if action.get('name'):
29+ item_names.insert(0, action['name'])
30+ file_name = '-'.join(item_names)
31 file_name = '%s.%s' % (file_name, report_struct['format'])
32+ # Create safe filename
33+ p = re.compile('[/:(")<>|?*]|(\\\)')
34+ file_name = p.sub('_', file_name)
35
36 return req.make_response(report,
37 headers=[