From 54f9434d468be65bd925628a8812bf33349582ca Mon Sep 17 00:00:00 2001 From: Samuel Walladge Date: Tue, 30 Jul 2019 14:40:19 +0930 Subject: [PATCH] Allows unicode usernames to be used in CCX enrollments and reports. Previously, unicode would result in a UnicodeEncodeError as a result of the format string being str instead of unicode. Additionally, the log calls would crash with a traceback (while not affecting the program flow). --- lms/djangoapps/ccx/tests/test_views.py | 2 +- lms/djangoapps/ccx/utils.py | 10 +++++----- lms/djangoapps/ccx/views.py | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lms/djangoapps/ccx/tests/test_views.py b/lms/djangoapps/ccx/tests/test_views.py index f3cfd272fa..5439abab13 100644 --- a/lms/djangoapps/ccx/tests/test_views.py +++ b/lms/djangoapps/ccx/tests/test_views.py @@ -89,7 +89,7 @@ def setup_students_and_grades(context): context.student = student = UserFactory.create() CourseEnrollmentFactory.create(user=student, course_id=context.course.id) - context.student2 = student2 = UserFactory.create() + context.student2 = student2 = UserFactory.create(username=u'u\u0131\u028c\u0279\u0250\u026f') CourseEnrollmentFactory.create(user=student2, course_id=context.course.id) # create grades for self.student as if they'd submitted the ccx diff --git a/lms/djangoapps/ccx/utils.py b/lms/djangoapps/ccx/utils.py index 2b47f8165a..91f55b18f4 100644 --- a/lms/djangoapps/ccx/utils.py +++ b/lms/djangoapps/ccx/utils.py @@ -260,14 +260,14 @@ def ccx_students_enrolling_center(action, identifiers, email_students, course_ke if student: must_enroll = student in staff or student in admins or student == coach except CCXUserValidationException as exp: - log.info("%s", exp) - errors.append("{0}".format(exp)) + log.info(u"%s", exp) + errors.append(u"{0}".format(exp)) continue if CourseEnrollment.objects.is_course_full(ccx_course_overview) and not must_enroll: error = _(u'The course is full: the limit is {max_student_enrollments_allowed}').format( max_student_enrollments_allowed=ccx_course_overview.max_student_enrollments_allowed) - log.info("%s", error) + log.info(u"%s", error) errors.append(error) break enroll_email(course_key, email, auto_enroll=True, email_students=email_students, email_params=email_params) @@ -276,8 +276,8 @@ def ccx_students_enrolling_center(action, identifiers, email_students, course_ke try: email, __ = get_valid_student_with_email(identifier) except CCXUserValidationException as exp: - log.info("%s", exp) - errors.append("{0}".format(exp)) + log.info(u"%s", exp) + errors.append(u"{0}".format(exp)) continue unenroll_email(course_key, email, email_students=email_students, email_params=email_params) return errors diff --git a/lms/djangoapps/ccx/views.py b/lms/djangoapps/ccx/views.py index a47921a7a0..d378dcaee2 100644 --- a/lms/djangoapps/ccx/views.py +++ b/lms/djangoapps/ccx/views.py @@ -552,7 +552,8 @@ def ccx_grades_csv(request, course, ccx=None): } row_percents = [percents.get(label, 0.0) for label in header] - rows.append([student.id, student.email, student.username, + rows.append([student.id, student.email.encode('utf-8'), + student.username.encode('utf-8'), course_grade.percent] + row_percents) buf = StringIO()