Skip to content

Commit 6f786cb

Browse files
committed
addressing #38: migrate the support for download file name in unicode. more test cases to come next
1 parent e4bb4d4 commit 6f786cb

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

django_excel/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from django.http import HttpResponse
1616
import pyexcel as pe
1717
import pyexcel_webio as webio
18-
from ._compact import DJANGO_ONE_SIX
18+
from ._compact import DJANGO_ONE_SIX, PY2_VERSION, urllib_quote
1919

2020

2121
class ExcelMixin(webio.ExcelInput):
@@ -124,8 +124,13 @@ def _make_response(content, content_type, status, file_name=None):
124124
"""
125125
response = HttpResponse(content, content_type=content_type, status=status)
126126
if file_name:
127+
if PY2_VERSION and isinstance(file_name, unicode):
128+
file_name = file_name.encode('utf-8')
129+
url_encoded_file_name = urllib_quote(file_name)
127130
response["Content-Disposition"] = (
128-
"attachment; filename=%s" % (file_name))
131+
"attachment; filename=%s;filename*=utf-8''%s"
132+
% (url_encoded_file_name, url_encoded_file_name)
133+
)
129134
return response
130135

131136

django_excel/_compact.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import pkg_resources
22
from distutils.version import LooseVersion
33

4+
try:
5+
# if in py2
6+
from urllib import quote as urllib_quote
7+
PY2_VERSION = True
8+
except ImportError:
9+
# else (aka in py3)
10+
from urllib.parse import quote as urllib_quote # flake8: noqa
11+
PY2_VERSION = False
12+
413

514
django_version = pkg_resources.get_distribution('django').version
615

testResponse.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import pyexcel.ext.xls # noqa
1010
import pyexcel.ext.xlsx # noqa
1111
import pyexcel.ext.ods3 # noqa
12+
from django_excel._compact import urllib_quote
13+
1214
PY2 = sys.version_info[0] == 2
1315
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
1416
from ordereddict import OrderedDict
@@ -68,9 +70,12 @@ def test_download_attachment(self):
6870
print(file_type)
6971
response = self.client.get(
7072
"/polls/download_attachment/"+file_type+"/"+test_file_name)
73+
url_encoded_file_name = urllib_quote(test_file_name)
7174
assert response['Content-Type'] == FILE_TYPE_MIME_TABLE[file_type]
7275
assert response['Content-Disposition'] == (
73-
"attachment; filename=%s.%s" % (test_file_name, file_type))
76+
"attachment; filename=%s.%s;filename*=utf-8''%s.%s"
77+
% (url_encoded_file_name, file_type,
78+
url_encoded_file_name, file_type))
7479
sheet = pe.get_sheet(file_type=file_type,
7580
file_content=response.content)
7681
sheet.format(int)

0 commit comments

Comments
 (0)