From 4e733c6e1f11d1c30b6a023cc925e98e9d07bf9d Mon Sep 17 00:00:00 2001 From: Omar Al-Ithawi Date: Tue, 3 Feb 2015 13:01:22 +0200 Subject: [PATCH] Unicode fixes for log messages and other things --- .../management/commands/assigngroups.py | 8 +- common/djangoapps/student/models.py | 43 ++++----- common/djangoapps/student/views.py | 18 ++-- lms/djangoapps/certificates/views.py | 3 +- lms/djangoapps/courseware/grades.py | 5 +- .../management/commands/regrade_partial.py | 88 ++++++++++++++----- .../management/commands/remove_input_state.py | 2 +- lms/djangoapps/instructor/views/api.py | 12 ++- lms/djangoapps/instructor/views/legacy.py | 10 ++- lms/djangoapps/lms_migration/migrate.py | 6 +- lms/djangoapps/shoppingcart/models.py | 30 +++++-- lms/djangoapps/shoppingcart/views.py | 18 ++-- lms/djangoapps/verify_student/views.py | 2 +- 13 files changed, 162 insertions(+), 83 deletions(-) diff --git a/common/djangoapps/student/management/commands/assigngroups.py b/common/djangoapps/student/management/commands/assigngroups.py index 3c6df5b3ba..166e5cab16 100644 --- a/common/djangoapps/student/management/commands/assigngroups.py +++ b/common/djangoapps/student/management/commands/assigngroups.py @@ -82,9 +82,11 @@ class Command(BaseCommand): v = random.uniform(0, 1) group = group_from_value(groups, v) group_objects[group].users.add(user) - f.write("Assigned user {name} ({id}) to {group}\n".format(name=user.username, - id=user.id, - group=group)) + f.write(u"Assigned user {name} ({id}) to {group}\n".format( + name=user.username, + id=user.id, + group=group + ).encode('utf-8')) ## Save groups for group in group_objects: diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index 0159587a24..fc036399a9 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -889,7 +889,12 @@ class CourseEnrollment(models.Model): except: # pylint: disable=bare-except if event_name and self.course_id: - log.exception('Unable to emit event %s for user %s and course %s', event_name, self.user.username, self.course_id) + log.exception( + u'Unable to emit event %s for user %s and course %s', + event_name, + self.user.username, # pylint: disable=no-member + self.course_id, + ) @classmethod def enroll(cls, user, course_key, mode="honor", check_access=False): @@ -930,10 +935,9 @@ class CourseEnrollment(models.Model): course = modulestore().get_course(course_key) except ItemNotFoundError: log.warning( - "User {0} failed to enroll in non-existent course {1}".format( - user.username, - course_key.to_deprecated_string() - ) + u"User %s failed to enroll in non-existent course %s", + user.username, + course_key.to_deprecated_string(), ) raise NonExistentCourseError @@ -942,27 +946,24 @@ class CourseEnrollment(models.Model): raise NonExistentCourseError if CourseEnrollment.is_enrollment_closed(user, course): log.warning( - "User {0} failed to enroll in course {1} because enrollment is closed".format( - user.username, - course_key.to_deprecated_string() - ) + u"User %s failed to enroll in course %s because enrollment is closed", + user.username, + course_key.to_deprecated_string() ) raise EnrollmentClosedError if CourseEnrollment.is_course_full(course): log.warning( - "User {0} failed to enroll in full course {1}".format( - user.username, - course_key.to_deprecated_string() - ) + u"User %s failed to enroll in full course %s", + user.username, + course_key.to_deprecated_string(), ) raise CourseFullError if CourseEnrollment.is_enrolled(user, course_key): log.warning( - "User {0} attempted to enroll in {1}, but they were already enrolled".format( - user.username, - course_key.to_deprecated_string() - ) + u"User %s attempted to enroll in %s, but they were already enrolled", + user.username, + course_key.to_deprecated_string() ) if check_access: raise AlreadyEnrolledError @@ -1029,8 +1030,8 @@ class CourseEnrollment(models.Model): record.update_enrollment(is_active=False, skip_refund=skip_refund) except cls.DoesNotExist: - err_msg = u"Tried to unenroll student {} from {} but they were not enrolled" - log.error(err_msg.format(user, course_id)) + err_msg = u"Tried to unenroll student %s from %s but they were not enrolled" + log.error(err_msg, user, course_id) @classmethod def unenroll_by_email(cls, email, course_id): @@ -1046,8 +1047,8 @@ class CourseEnrollment(models.Model): user = User.objects.get(email=email) return cls.unenroll(user, course_id) except User.DoesNotExist: - err_msg = u"Tried to unenroll email {} from course {}, but user not found" - log.error(err_msg.format(email, course_id)) + err_msg = u"Tried to unenroll email %s from course %s, but user not found" + log.error(err_msg, user, course_id) @classmethod def is_enrolled(cls, user, course_key): diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index 774cf23168..792dd3c454 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -338,7 +338,7 @@ def _cert_info(user, course, cert_status): if status == 'ready': if 'download_url' not in cert_status: - log.warning("User %s has a downloadable cert for %s, but no download url", + log.warning(u"User %s has a downloadable cert for %s, but no download url", user.username, course.id) return default_info else: @@ -841,13 +841,11 @@ def change_enrollment(request, check_access=True): try: course_id = SlashSeparatedCourseKey.from_deprecated_string(request.POST.get("course_id")) except InvalidKeyError: - log.warning( - "User {username} tried to {action} with invalid course id: {course_id}".format( - username=user.username, - action=action, - course_id=request.POST.get("course_id") - ) - ) + log.warning(u"User %(username)s tried to %(action)s with invalid course id: %(course_id)s", { + "username": user.username, + "action": action, + "course_id": request.POST.get("course_id"), + }) return HttpResponseBadRequest(_("Invalid course id")) if action == "enroll": @@ -1248,11 +1246,11 @@ def disable_account_ajax(request): if account_action == 'disable': user_account.account_status = UserStanding.ACCOUNT_DISABLED context['message'] = _("Successfully disabled {}'s account").format(username) - log.info("{} disabled {}'s account".format(request.user, username)) + log.info(u"%s disabled %s's account", request.user, username) elif account_action == 'reenable': user_account.account_status = UserStanding.ACCOUNT_ENABLED context['message'] = _("Successfully reenabled {}'s account").format(username) - log.info("{} reenabled {}'s account".format(request.user, username)) + log.info(u"%s reenabled %s's account", request.user, username) else: context['message'] = _("Unexpected account status") return JsonResponse(context, status=400) diff --git a/lms/djangoapps/certificates/views.py b/lms/djangoapps/certificates/views.py index e2d5f49f5f..13d341dc1a 100644 --- a/lms/djangoapps/certificates/views.py +++ b/lms/djangoapps/certificates/views.py @@ -36,7 +36,8 @@ def request_certificate(request): status = certificate_status_for_student(student, course_key)['status'] if status in [CertificateStatuses.unavailable, CertificateStatuses.notpassing, CertificateStatuses.error]: - logger.info('Grading and certification requested for user {} in course {} via /request_certificate call'.format(username, course_key)) + log_msg = u'Grading and certification requested for user %s in course %s via /request_certificate call' + logger.info(log_msg, username, course_key) status = xqci.add_cert(student, course_key, course=course) return HttpResponse(json.dumps({'add_status': status}), mimetype='application/json') return HttpResponse(json.dumps({'add_status': 'ERRORANONYMOUSUSER'}), mimetype='application/json') diff --git a/lms/djangoapps/courseware/grades.py b/lms/djangoapps/courseware/grades.py index bd8cb577f2..3db3cb8f35 100644 --- a/lms/djangoapps/courseware/grades.py +++ b/lms/djangoapps/courseware/grades.py @@ -89,8 +89,9 @@ def answer_distributions(course_key): raw_answers = state_dict.get("student_answers", {}) except ValueError: log.error( - "Answer Distribution: Could not parse module state for " + - "StudentModule id={}, course={}".format(module.id, course_key) + u"Answer Distribution: Could not parse module state for StudentModule id=%s, course=%s", + module.id, + course_key, ) continue diff --git a/lms/djangoapps/courseware/management/commands/regrade_partial.py b/lms/djangoapps/courseware/management/commands/regrade_partial.py index c24f84ddfc..1cbcd52b87 100644 --- a/lms/djangoapps/courseware/management/commands/regrade_partial.py +++ b/lms/djangoapps/courseware/management/commands/regrade_partial.py @@ -52,9 +52,13 @@ class Command(BaseCommand): module_state = module.state if module_state is None: # not likely, since we filter on it. But in general... - LOG.info("No state found for {type} module {id} for student {student} in course {course_id}" - .format(type=module.module_type, id=module.module_state_key, - student=module.student.username, course_id=module.course_id)) + LOG.info( + u"No state found for %(type)s module %(id)s for student %(student)s in course %(course_id)s", + module.module_type, + module.module_state_key, + module.student.username, + module.course_id, + ) return state_dict = json.loads(module_state) @@ -66,15 +70,31 @@ class Command(BaseCommand): if (not student_answers) or len(student_answers) == 0: # we should not have a grade here: if module.grade != 0: - LOG.error("No answer found but grade {grade} exists for {type} module {id} for student {student} " - "in course {course_id}".format(grade=module.grade, - type=module.module_type, id=module.module_state_key, - student=module.student.username, course_id=module.course_id)) + log_msg = ( + u"No answer found but grade %(grade)s exists for %(type)s module %(id)s for student %(student)s " + + u"in course %(course_id)s" + ) + + LOG.error(log_msg, { + "grade": module.grade, + "type": module.module_type, + "id": module.module_state_key, + "student": module.student.username, + "course_id": module.course_id, + }) else: - LOG.debug("No answer and no grade found for {type} module {id} for student {student} " - "in course {course_id}".format(grade=module.grade, - type=module.module_type, id=module.module_state_key, - student=module.student.username, course_id=module.course_id)) + log_msg = ( + u"No answer and no grade found for %(type)s module %(id)s for student %(student)s " + + u"in course %(course_id)s" + ) + + LOG.debug(log_msg, { + "grade": module.grade, + "type": module.module_type, + "id": module.module_state_key, + "student": module.student.username, + "course_id": module.course_id, + }) return # load into a CorrectMap, as done in LoncapaProblem.__init__(): @@ -90,24 +110,48 @@ class Command(BaseCommand): if module.grade == correct: # nothing to change - LOG.debug("Grade matches for {type} module {id} for student {student} in course {course_id}" - .format(type=module.module_type, id=module.module_state_key, - student=module.student.username, course_id=module.course_id)) + log_msg = u"Grade matches for %(type)s module %(id)s for student %(student)s in course %(course_id)s" + LOG.debug(log_msg, { + "type": module.module_type, + "id": module.module_state_key, + "student": module.student.username, + "course_id": module.course_id, + }) elif save_changes: # make the change - LOG.info("Grade changing from {0} to {1} for {type} module {id} for student {student} " - "in course {course_id}".format(module.grade, correct, - type=module.module_type, id=module.module_state_key, - student=module.student.username, course_id=module.course_id)) + log_msg = ( + u"Grade changing from %(grade)s to %(correct)s for %(type)s module " + + u"%(id)s for student %(student)s in course %(course_id)s" + ) + + LOG.debug(log_msg, { + "grade": module.grade, + "correct": correct, + "type": module.module_type, + "id": module.module_state_key, + "student": module.student.username, + "course_id": module.course_id, + }) + module.grade = correct module.save() self.num_changed += 1 else: # don't make the change, but log that the change would be made - LOG.info("Grade would change from {0} to {1} for {type} module {id} for student {student} " - "in course {course_id}".format(module.grade, correct, - type=module.module_type, id=module.module_state_key, - student=module.student.username, course_id=module.course_id)) + log_msg = ( + u"Grade would change from %(grade)s to %(correct)s for %(type)s module %(id)s for student " + + u"%(student)s in course %(course_id)s" + ) + + LOG.debug(log_msg, { + "grade": module.grade, + "correct": correct, + "type": module.module_type, + "id": module.module_state_key, + "student": module.student.username, + "course_id": module.course_id, + }) + self.num_changed += 1 def handle(self, **options): diff --git a/lms/djangoapps/courseware/management/commands/remove_input_state.py b/lms/djangoapps/courseware/management/commands/remove_input_state.py index e45d08e351..3e86d213a9 100644 --- a/lms/djangoapps/courseware/management/commands/remove_input_state.py +++ b/lms/djangoapps/courseware/management/commands/remove_input_state.py @@ -68,7 +68,7 @@ class Command(BaseCommand): try: module = StudentModule.objects.get(id=student_module_id) except StudentModule.DoesNotExist: - LOG.error("Unable to find student module with id = {0}: skipping... ".format(student_module_id)) + LOG.error(u"Unable to find student module with id = %s: skipping... ", student_module_id) continue self.remove_studentmodule_input_state(module, save_changes) diff --git a/lms/djangoapps/instructor/views/api.py b/lms/djangoapps/instructor/views/api.py index 8c97f1e899..73d544030e 100644 --- a/lms/djangoapps/instructor/views/api.py +++ b/lms/djangoapps/instructor/views/api.py @@ -353,12 +353,20 @@ def register_and_enroll_students(request, course_id): # pylint: disable=too-man 'username': username, 'email': email, 'response': warning_message}) log.warning('email {email} already exist'.format(email=email)) else: - log.info("user already exists with username '{username}' and email '{email}'".format(email=email, username=username)) + log.info( + u"user already exists with username '%s' and email '%s'", + email=email, + username=username, + ) # make sure user is enrolled in course if not CourseEnrollment.is_enrolled(user, course_id): CourseEnrollment.enroll(user, course_id) - log.info('user {username} enrolled in the course {course}'.format(username=username, course=course.id)) + log.info( + u'user %s enrolled in the course %s', + username=username, + course=course.id, + ) enroll_email(course_id=course_id, student_email=email, auto_enroll=True, email_students=True, email_params=email_params) else: # This email does not yet exist, so we need to create a new account diff --git a/lms/djangoapps/instructor/views/legacy.py b/lms/djangoapps/instructor/views/legacy.py index 4bb65e5849..09ecfe05d3 100644 --- a/lms/djangoapps/instructor/views/legacy.py +++ b/lms/djangoapps/instructor/views/legacy.py @@ -252,9 +252,11 @@ def instructor_dashboard(request, course_id): try: ddata.append([student.email, student.grades[aidx]]) except IndexError: - log.debug('No grade for assignment {idx} ({name}) for student {email}'.format( - idx=aidx, name=aname, email=student.email) - ) + log.debug(u'No grade for assignment %(idx)s (%(name)s) for student %(email)s', { + "idx": aidx, + "name": aname, + "email": student.email, + }) datatable['data'] = ddata datatable['title'] = _('Grades for assignment "{name}"').format(name=aname) @@ -749,7 +751,7 @@ def get_student_grade_summary_data(request, course, get_grades=True, get_raw_sco if get_grades: gradeset = student_grades(student, request, course, keep_raw_scores=get_raw_scores, use_offline=use_offline) - log.debug('student={0}, gradeset={1}'.format(student, gradeset)) + log.debug(u'student=%s, gradeset=%s', student, gradeset) with gtab.add_row(student.id) as add_grade: if get_raw_scores: # TODO (ichuang) encode Score as dict instead of as list, so score[0] -> score['earned'] diff --git a/lms/djangoapps/lms_migration/migrate.py b/lms/djangoapps/lms_migration/migrate.py index ee6f715fb6..eb33a14773 100644 --- a/lms/djangoapps/lms_migration/migrate.py +++ b/lms/djangoapps/lms_migration/migrate.py @@ -69,11 +69,11 @@ def manage_modulestores(request, reload_dir=None, commit_id=None): html += '

IP address: %s

' % ip html += '

User: %s

' % request.user html += '

My pid: %s

' % os.getpid() - log.debug('request from ip=%s, user=%s' % (ip, request.user)) + log.debug(u'request from ip=%s, user=%s', ip, request.user) if not (ip in ALLOWED_IPS or 'any' in ALLOWED_IPS): if request.user and request.user.is_staff: - log.debug('request allowed because user=%s is staff' % request.user) + log.debug(u'request allowed because user=%s is staff', request.user) else: html += 'Permission denied' html += "" @@ -185,7 +185,7 @@ def gitreload(request, reload_dir=None): if not (ip in ALLOWED_IPS or 'any' in ALLOWED_IPS): if request.user and request.user.is_staff: - log.debug('request allowed because user=%s is staff' % request.user) + log.debug(u'request allowed because user=%s is staff', request.user) else: html += 'Permission denied' html += "" diff --git a/lms/djangoapps/shoppingcart/models.py b/lms/djangoapps/shoppingcart/models.py index 8c6bda7860..29ed866986 100644 --- a/lms/djangoapps/shoppingcart/models.py +++ b/lms/djangoapps/shoppingcart/models.py @@ -927,7 +927,7 @@ class CouponRedemption(models.Model): coupon_redemption = cls.objects.filter(user=user, order=cart) if coupon_redemption: coupon_redemption.delete() - log.info('Coupon redemption entry removed for user {0} for order {1}'.format(user, cart.id)) + log.info(u'Coupon redemption entry removed for user %s for order %s', user, cart.id) @classmethod def get_discount_price(cls, percentage_discount, value): @@ -946,8 +946,11 @@ class CouponRedemption(models.Model): coupon_redemptions = cls.objects.filter(order=order, user=order.user) for coupon_redemption in coupon_redemptions: if coupon_redemption.coupon.code != coupon.code or coupon_redemption.coupon.id == coupon.id: - log.exception("Coupon redemption already exist for user '{0}' against order id '{1}'" - .format(order.user.username, order.id)) + log.exception( + u"Coupon redemption already exist for user '%s' against order id '%s'", + order.user.username, + order.id, + ) raise MultipleCouponsNotAllowedException for item in cart_items: @@ -959,8 +962,11 @@ class CouponRedemption(models.Model): item.list_price = item.unit_cost item.unit_cost = discount_price item.save() - log.info("Discount generated for user {0} against order id '{1}' " - .format(order.user.username, order.id)) + log.info( + u"Discount generated for user %s against order id '%s'", + order.user.username, + order.id, + ) is_redemption_applied = True return is_redemption_applied @@ -1018,8 +1024,12 @@ class PaidCourseRegistration(OrderItem): raise CourseDoesNotExistException if cls.contained_in_order(order, course_id): - log.warning("User {} tried to add PaidCourseRegistration for course {}, already in cart id {}" - .format(order.user.email, course_id, order.id)) + log.warning( + u"User %s tried to add PaidCourseRegistration for course %s, already in cart id %s", + order.user.email, + course_id, + order.id, + ) raise ItemAlreadyInCartException if CourseEnrollment.is_enrolled(user=order.user, course_key=course_id): @@ -1319,7 +1329,11 @@ class CertificateItem(OrderItem): try: target_cert = target_certs[0] except IndexError: - log.error("Matching CertificateItem not found while trying to refund. User %s, Course %s", course_enrollment.user, course_enrollment.course_id) + log.error( + u"Matching CertificateItem not found while trying to refund. User %s, Course %s", + course_enrollment.user, + course_enrollment.course_id, + ) return target_cert.status = 'refunded' target_cert.refund_requested_time = datetime.now(pytz.utc) diff --git a/lms/djangoapps/shoppingcart/views.py b/lms/djangoapps/shoppingcart/views.py index a19541d05c..00ce713cf2 100644 --- a/lms/djangoapps/shoppingcart/views.py +++ b/lms/djangoapps/shoppingcart/views.py @@ -200,8 +200,10 @@ def clear_cart(request): coupon_redemption = CouponRedemption.objects.filter(user=request.user, order=cart.id) if coupon_redemption: coupon_redemption.delete() - log.info('Coupon redemption entry removed for user {user} for order {order_id}'.format(user=request.user, - order_id=cart.id)) + log.info(u'Coupon redemption entry removed for user %(user)s for order %(order_id)s', { + "user": request.user, + "order_id": cart.id, + }) return HttpResponse('Cleared') @@ -224,7 +226,10 @@ def remove_item(request): if item.user == request.user: order_item_course_id = getattr(item, 'course_id') item.delete() - log.info('order item {item_id} removed for user {user}'.format(item_id=item_id, user=request.user)) + log.info(u'order item %(item_id)s removed for user %(user)s', { + "item_id": item_id, + "user": request.user, + }) remove_code_redemption(order_item_course_id, item_id, item, request.user) item.order.update_order_type() @@ -244,8 +249,11 @@ def remove_code_redemption(order_item_course_id, item_id, item, user): order=item.order_id ) coupon_redemption.delete() - log.info('Coupon "{code}" redemption entry removed for user "{user}" for order item "{item_id}"' - .format(code=coupon_redemption.coupon.code, user=user, item_id=item_id)) + log.info(u'Coupon "%(code)s" redemption entry removed for user "%(user)s" for order item "%(item_id)s"', { + "code": coupon_redemption.coupon.code, + "user": user, + "item_id": item_id, + }) except CouponRedemption.DoesNotExist: log.debug('Code redemption does not exist for order item id={item_id}.'.format(item_id=item_id)) diff --git a/lms/djangoapps/verify_student/views.py b/lms/djangoapps/verify_student/views.py index 2ac8704551..9b40f00488 100644 --- a/lms/djangoapps/verify_student/views.py +++ b/lms/djangoapps/verify_student/views.py @@ -929,7 +929,7 @@ def midcourse_reverify_dash(request): try: course_enrollment_pairs.append((modulestore().get_course(enrollment.course_id), enrollment)) except ItemNotFoundError: - log.error("User {0} enrolled in non-existent course {1}".format(user.username, enrollment.course_id)) + log.error(u"User %s enrolled in non-existent course %s", user.username, enrollment.course_id) statuses = ["approved", "pending", "must_reverify", "denied"]