[MERGE] content-disposition fixes by Giedrius Slavinskas

lp bug: https://launchpad.net/bugs/1072803 fixed

bzr revid: al@openerp.com-20121103153406-m2xlwm9af2ekcdc7
This commit is contained in:
Antony Lesuisse 2012-11-03 16:34:06 +01:00
commit 74cda3df5b
1 changed files with 21 additions and 17 deletions

View File

@ -575,6 +575,20 @@ def from_elementtree(el, preserve_whitespaces=False):
res["children"] = kids res["children"] = kids
return res return res
def content_disposition(filename, req):
filename = filename.encode('utf8')
escaped = urllib2.quote(filename)
browser = req.httprequest.user_agent.browser
version = int((req.httprequest.user_agent.version or '0').split('.')[0])
if browser == 'msie' and version < 9:
return "attachment; filename=%s" % escaped
elif browser == 'safari':
return "attachment; filename=%s" % filename
else:
return "attachment; filename*=UTF-8''%s" % escaped
#---------------------------------------------------------- #----------------------------------------------------------
# OpenERP Web web Controllers # OpenERP Web web Controllers
#---------------------------------------------------------- #----------------------------------------------------------
@ -841,7 +855,7 @@ class Database(openerpweb.Controller):
} }
return req.make_response(db_dump, return req.make_response(db_dump,
[('Content-Type', 'application/octet-stream; charset=binary'), [('Content-Type', 'application/octet-stream; charset=binary'),
('Content-Disposition', 'attachment; filename="' + filename + '"')], ('Content-Disposition', content_disposition(filename, req))],
{'fileToken': int(token)} {'fileToken': int(token)}
) )
except xmlrpclib.Fault, e: except xmlrpclib.Fault, e:
@ -1520,17 +1534,6 @@ class Binary(openerpweb.Controller):
def placeholder(self, req): def placeholder(self, req):
addons_path = openerpweb.addons_manifest['web']['addons_path'] addons_path = openerpweb.addons_manifest['web']['addons_path']
return open(os.path.join(addons_path, 'web', 'static', 'src', 'img', 'placeholder.png'), 'rb').read() return open(os.path.join(addons_path, 'web', 'static', 'src', 'img', 'placeholder.png'), 'rb').read()
def content_disposition(self, filename, req):
filename = filename.encode('utf8')
escaped = urllib2.quote(filename)
browser = req.httprequest.user_agent.browser
version = int((req.httprequest.user_agent.version or '0').split('.')[0])
if browser == 'msie' and version < 9:
return "attachment; filename=%s" % escaped
elif browser == 'safari':
return "attachment; filename=%s" % filename
else:
return "attachment; filename*=UTF-8''%s" % escaped
@openerpweb.httprequest @openerpweb.httprequest
def saveas(self, req, model, field, id=None, filename_field=None, **kw): def saveas(self, req, model, field, id=None, filename_field=None, **kw):
@ -1566,7 +1569,7 @@ class Binary(openerpweb.Controller):
filename = res.get(filename_field, '') or filename filename = res.get(filename_field, '') or filename
return req.make_response(filecontent, return req.make_response(filecontent,
[('Content-Type', 'application/octet-stream'), [('Content-Type', 'application/octet-stream'),
('Content-Disposition', self.content_disposition(filename, req))]) ('Content-Disposition', content_disposition(filename, req))])
@openerpweb.httprequest @openerpweb.httprequest
def saveas_ajax(self, req, data, token): def saveas_ajax(self, req, data, token):
@ -1596,7 +1599,7 @@ class Binary(openerpweb.Controller):
filename = res.get(filename_field, '') or filename filename = res.get(filename_field, '') or filename
return req.make_response(filecontent, return req.make_response(filecontent,
headers=[('Content-Type', 'application/octet-stream'), headers=[('Content-Type', 'application/octet-stream'),
('Content-Disposition', self.content_disposition(filename, req))], ('Content-Disposition', content_disposition(filename, req))],
cookies={'fileToken': int(token)}) cookies={'fileToken': int(token)})
@openerpweb.httprequest @openerpweb.httprequest
@ -1861,7 +1864,8 @@ class Export(View):
return req.make_response(self.from_data(columns_headers, import_data), return req.make_response(self.from_data(columns_headers, import_data),
headers=[('Content-Disposition', 'attachment; filename="%s"' % self.filename(model)), headers=[('Content-Disposition',
content_disposition(self.filename(model), req)),
('Content-Type', self.content_type)], ('Content-Type', self.content_type)],
cookies={'fileToken': int(token)}) cookies={'fileToken': int(token)})
@ -1997,11 +2001,11 @@ class Reports(View):
file_name = reports.read(res_id[0], ['name'], context)['name'] file_name = reports.read(res_id[0], ['name'], context)['name']
else: else:
file_name = action['report_name'] file_name = action['report_name']
file_name = '%s.%s' % (file_name, report_struct['format'])
return req.make_response(report, return req.make_response(report,
headers=[ headers=[
# maybe we should take of what characters can appear in a file name? ('Content-Disposition', content_disposition(file_name, req)),
('Content-Disposition', 'attachment; filename="%s.%s"' % (file_name, report_struct['format'])),
('Content-Type', report_mimetype), ('Content-Type', report_mimetype),
('Content-Length', len(report))], ('Content-Length', len(report))],
cookies={'fileToken': int(token)}) cookies={'fileToken': int(token)})