~pexego/ocb-web/ocb-web-va-ca

« back to all changes in this revision

Viewing changes to addons/web/controllers/main.py

  • Committer: Pedro M. Baeza
  • Author(s): yann.papouin at gmail
  • Date: 2014-05-19 13:25:36 UTC
  • mfrom: (2521.1.3 ocb-6.1)
  • Revision ID: pedro.baeza@serviciosbaeza.com-20140519132536-k77yyin0lx1uwb3i
[IMP] web: Better report filename when it's printed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1862
1862
                break
1863
1863
 
1864
1864
            time.sleep(self.POLLING_DELAY)
 
1865
            
 
1866
        # Load built-in report name
 
1867
        file_name = action['report_name']
 
1868
        # Try to get current object model and their ids from context
 
1869
        if 'context' in action:  
 
1870
            action_context = action['context']
 
1871
            if action_context.get('active_model') and action_context['active_ids']:
 
1872
                # Use built-in ORM method to get data from DB
 
1873
                m = req.session.model(action_context['active_model'])
 
1874
                r = m.name_get(action_context['active_ids'], context)
 
1875
                # Parse result to create a better filename
 
1876
                item_names = [item[1] or str(item[0]) for item in r]
 
1877
                if action.get('name'):
 
1878
                    item_names.insert(0, action['name'])
 
1879
                file_name = '-'.join(item_names)
 
1880
                # Create safe filename
 
1881
                p = re.compile('[/:(")<>|?*]|(\\\)')
 
1882
                file_name = p.sub('_', file_name)
1865
1883
 
1866
1884
        report = base64.b64decode(report_struct['result'])
1867
1885
        if report_struct.get('code') == 'zlib':
1870
1888
            report_struct['format'], 'octet-stream')
1871
1889
        return req.make_response(report,
1872
1890
             headers=[
1873
 
                 ('Content-Disposition', 'attachment; filename="%s.%s"' % (action['report_name'], report_struct['format'])),
 
1891
                 ('Content-Disposition', 'attachment; filename="%s.%s"' % (file_name, report_struct['format'])),
1874
1892
                 ('Content-Type', report_mimetype),
1875
1893
                 ('Content-Length', len(report))],
1876
1894
             cookies={'fileToken': int(token)})