Skip to content

Commit

Permalink
Allow not only english language file uploads
Browse files Browse the repository at this point in the history
Currently a student as well as staff member could
upload any file as part of submission or grade.

Unfortunately when the user trying to DOWNLOAD
the files that filename is not in English (Russian for example)
we got an 500 error on the server.

This occurs because of Python encoding rules, and original
issue is something like this:

"UnicodeEncodeError: 'latin-1' codec can't encode characters
in position 21-24: ordinal not in range(256)"

(Depends on the filename)

This patch is fixing it.
  • Loading branch information
aliaksandrb committed Jul 2, 2015
1 parent 0f25d31 commit de2f501
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion edx_sga/sga.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,8 @@ def download(self, path, mime_type, filename):
return Response(
app_iter=app_iter,
content_type=mime_type,
content_disposition="attachment; filename=" + filename)
content_disposition=("attachment; filename=" +
filename.encode('utf-8')))

@XBlock.handler
def get_staff_grading_data(self, request, suffix=''):
Expand Down
33 changes: 33 additions & 0 deletions edx_sga/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""
Tests for SGA
"""
Expand Down Expand Up @@ -444,6 +445,38 @@ def test_staff_download(self):
'student_id': student['item'].student_id}))
self.assertEqual(response.body, expected)

def test_download_annotated_unicode_filename(self):
"""
Tests download annotated assignment
with filename in unicode for non staff member.
"""
path = pkg_resources.resource_filename(__package__, 'tests.py')
expected = open(path, 'rb').read()
upload = mock.Mock(file=DummyUpload(path, 'файл.txt'))
block = self.make_one()
fred = self.make_student(block, "fred2")
block.staff_upload_annotated(mock.Mock(params={
'annotated': upload,
'module_id': fred['module'].id}))
self.personalize(block, **fred)
response = block.download_annotated(None)
self.assertEqual(response.body, expected)

def test_staff_download_unicode_filename(self):
"""
Tests download assignment with filename in unicode for staff.
"""
path = pkg_resources.resource_filename(__package__, 'tests.py')
expected = open(path, 'rb').read()
upload = mock.Mock(file=DummyUpload(path, 'файл.txt'))
block = self.make_one()
student = self.make_student(block, 'fred')
self.personalize(block, **student)
block.upload_assignment(mock.Mock(params={'assignment': upload}))
response = block.staff_download(mock.Mock(params={
'student_id': student['item'].student_id}))
self.assertEqual(response.body, expected)

def test_get_staff_grading_data_not_staff(self):
"""
test staff grading data for non staff members.
Expand Down

0 comments on commit de2f501

Please sign in to comment.