From 0d8237680c0a01d216a659b532687f614b861707 Mon Sep 17 00:00:00 2001 From: Amir Qayyum Khan Date: Wed, 13 Apr 2016 21:36:18 +0500 Subject: [PATCH] Fixed exception related to course not found for ccx --- .../0003_add_master_course_staff_in_ccx.py | 54 +++++++++++++------ 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/lms/djangoapps/ccx/migrations/0003_add_master_course_staff_in_ccx.py b/lms/djangoapps/ccx/migrations/0003_add_master_course_staff_in_ccx.py index 0c3a5770c7..977a69b5b8 100644 --- a/lms/djangoapps/ccx/migrations/0003_add_master_course_staff_in_ccx.py +++ b/lms/djangoapps/ccx/migrations/0003_add_master_course_staff_in_ccx.py @@ -1,15 +1,20 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +import logging + from ccx_keys.locator import CCXLocator from courseware.courses import get_course_by_id from django.db import migrations +from django.http import Http404 from lms.djangoapps.ccx.utils import ( add_master_course_staff_to_ccx, remove_master_course_staff_from_ccx, ) +log = logging.getLogger("edx.ccx") + def add_master_course_staff_to_ccx_for_existing_ccx(apps, schema_editor): """ @@ -23,16 +28,24 @@ def add_master_course_staff_to_ccx_for_existing_ccx(apps, schema_editor): CustomCourseForEdX = apps.get_model("ccx", "CustomCourseForEdX") list_ccx = CustomCourseForEdX.objects.all() for ccx in list_ccx: - if ccx.course_id.deprecated: - # prevent migration for deprecated course ids. + if not ccx.course_id or ccx.course_id.deprecated: + # prevent migration for deprecated course ids or invalid ids. continue ccx_locator = CCXLocator.from_course_locator(ccx.course_id, unicode(ccx.id)) - add_master_course_staff_to_ccx( - get_course_by_id(ccx.course_id), - ccx_locator, - ccx.display_name, - send_email=False - ) + try: + course = get_course_by_id(ccx.course_id) + add_master_course_staff_to_ccx( + course, + ccx_locator, + ccx.display_name, + send_email=False + ) + except Http404: + log.warning( + "Unable to add instructors and staff of master course %s to ccx %s.", + ccx.course_id, + ccx_locator + ) def remove_master_course_staff_from_ccx_for_existing_ccx(apps, schema_editor): @@ -47,17 +60,24 @@ def remove_master_course_staff_from_ccx_for_existing_ccx(apps, schema_editor): CustomCourseForEdX = apps.get_model("ccx", "CustomCourseForEdX") list_ccx = CustomCourseForEdX.objects.all() for ccx in list_ccx: - if ccx.course_id.deprecated: - # prevent migration for deprecated course ids. + if not ccx.course_id or ccx.course_id.deprecated: + # prevent migration for deprecated course ids or invalid ids. continue ccx_locator = CCXLocator.from_course_locator(ccx.course_id, unicode(ccx.id)) - remove_master_course_staff_from_ccx( - get_course_by_id(ccx.course_id), - ccx_locator, - ccx.display_name, - send_email=False - ) - + try: + course = get_course_by_id(ccx.course_id) + remove_master_course_staff_from_ccx( + course, + ccx_locator, + ccx.display_name, + send_email=False + ) + except Http404: + log.warning( + "Unable to remove instructors and staff of master course %s from ccx %s.", + ccx.course_id, + ccx_locator + ) class Migration(migrations.Migration):