Skip to content

Commit de2f501

Browse files
committed
Allow not only english language file uploads
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.
1 parent 0f25d31 commit de2f501

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

edx_sga/sga.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,8 @@ def download(self, path, mime_type, filename):
520520
return Response(
521521
app_iter=app_iter,
522522
content_type=mime_type,
523-
content_disposition="attachment; filename=" + filename)
523+
content_disposition=("attachment; filename=" +
524+
filename.encode('utf-8')))
524525

525526
@XBlock.handler
526527
def get_staff_grading_data(self, request, suffix=''):

edx_sga/tests.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
"""
23
Tests for SGA
34
"""
@@ -444,6 +445,38 @@ def test_staff_download(self):
444445
'student_id': student['item'].student_id}))
445446
self.assertEqual(response.body, expected)
446447

448+
def test_download_annotated_unicode_filename(self):
449+
"""
450+
Tests download annotated assignment
451+
with filename in unicode for non staff member.
452+
"""
453+
path = pkg_resources.resource_filename(__package__, 'tests.py')
454+
expected = open(path, 'rb').read()
455+
upload = mock.Mock(file=DummyUpload(path, 'файл.txt'))
456+
block = self.make_one()
457+
fred = self.make_student(block, "fred2")
458+
block.staff_upload_annotated(mock.Mock(params={
459+
'annotated': upload,
460+
'module_id': fred['module'].id}))
461+
self.personalize(block, **fred)
462+
response = block.download_annotated(None)
463+
self.assertEqual(response.body, expected)
464+
465+
def test_staff_download_unicode_filename(self):
466+
"""
467+
Tests download assignment with filename in unicode for staff.
468+
"""
469+
path = pkg_resources.resource_filename(__package__, 'tests.py')
470+
expected = open(path, 'rb').read()
471+
upload = mock.Mock(file=DummyUpload(path, 'файл.txt'))
472+
block = self.make_one()
473+
student = self.make_student(block, 'fred')
474+
self.personalize(block, **student)
475+
block.upload_assignment(mock.Mock(params={'assignment': upload}))
476+
response = block.staff_download(mock.Mock(params={
477+
'student_id': student['item'].student_id}))
478+
self.assertEqual(response.body, expected)
479+
447480
def test_get_staff_grading_data_not_staff(self):
448481
"""
449482
test staff grading data for non staff members.

0 commit comments

Comments
 (0)