Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to unicode file name #22

Merged
merged 6 commits into from
Jun 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/tiny_example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""
tiny_example.py
:copyright: (c) 2015 by C. W.
Expand Down Expand Up @@ -34,6 +35,12 @@ def export_records():
file_name="export_data")


@app.route("/download_file_named_in_unicode", methods=['GET'])
def download_file_named_in_unicode():
return excel.make_response_from_array([[1, 2], [3, 4]], "csv",
file_name=u"中文文件名")


# insert database related code here
if __name__ == "__main__":
app.run()
16 changes: 15 additions & 1 deletion flask_excel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
:copyright: (c) 2015 by Onni Software Ltd.
:license: New BSD License
"""
try:
# if in py2
from urllib import quote
_PY_VERSION = 2
except ImportError:
# else (aka in py3)
from urllib.parse import quote
_PY_VERSION = 3

from flask import Flask, Request, Response
import pyexcel_webio as webio

Expand Down Expand Up @@ -38,8 +47,13 @@ def _make_response(content, content_type, status, file_name=None):
"""
response = Response(content, content_type=content_type, status=status)
if file_name:
if _PY_VERSION == 2 and isinstance(file_name, unicode):
file_name = file_name.encode('utf-8')
url_encoded_file_name = quote(file_name)
response.headers["Content-Disposition"] = (
"attachment; filename=%s" % (file_name))
"attachment; filename=%s;filename*=utf-8''%s"
% (url_encoded_file_name, url_encoded_file_name)
)
return response


Expand Down
36 changes: 35 additions & 1 deletion tests/test_upload_n_download_excel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# -*- coding: utf-8 -*-

try:
from urllib import quote
except ImportError:
from urllib.parse import quote

from testapp import app
import pyexcel as pe
from nose.tools import eq_
Expand Down Expand Up @@ -71,8 +78,35 @@ def test_no_file_type(self):
def test_override_file_name(self):
for file_type in FILE_TYPE_MIME_TABLE.keys():
file_name = 'override_file_name'
url_encoded_file_name = quote(file_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you have tried to demonstrate in the tiny_example.py, could you please invest a bit more effort in writing two additional test case for the previous two demo functions.

response = self.app.post('/file_name/%s/%s' % (file_type,
file_name))
eq_(response.content_type, FILE_TYPE_MIME_TABLE[file_type])
eq_(response.headers.get("Content-Disposition", None),
("attachment; filename=%s.%s;filename*=utf-8''%s.%s"
% (url_encoded_file_name, file_type,
url_encoded_file_name, file_type)))

def test_unicode_file_name(self):
for file_type in FILE_TYPE_MIME_TABLE.keys():
file_name = u'中文文件名'
url_encoded_file_name = quote(file_name.encode('utf-8'))
response = self.app.post('/file_name/%s/%s' % (file_type,
file_name))
eq_(response.content_type, FILE_TYPE_MIME_TABLE[file_type])
eq_(response.headers.get("Content-Disposition", None),
("attachment; filename=%s.%s;filename*=utf-8''%s.%s"
% (url_encoded_file_name, file_type,
url_encoded_file_name, file_type)))

def test_utf8_file_name(self):
for file_type in FILE_TYPE_MIME_TABLE.keys():
file_name = '中文文件名'
url_encoded_file_name = quote(file_name)
response = self.app.post('/file_name/%s/%s' % (file_type,
file_name))
eq_(response.content_type, FILE_TYPE_MIME_TABLE[file_type])
eq_(response.headers.get("Content-Disposition", None),
("attachment; filename=%s.%s" % (file_name, file_type)))
("attachment; filename=%s.%s;filename*=utf-8''%s.%s"
% (url_encoded_file_name, file_type,
url_encoded_file_name, file_type)))