def move_to_verified_cohort(sender, instance, **kwargs): # pylint: disable=unused-argument """ If the learner has changed modes, update assigned cohort iff the course is using the Automatic Verified Track Cohorting MVP feature. """ from lms.djangoapps.courseware.courses import get_course_by_id course_key = instance.course_id verified_cohort_enabled = VerifiedTrackCohortedCourse.is_verified_track_cohort_enabled( course_key) verified_cohort_name = VerifiedTrackCohortedCourse.verified_cohort_name_for_course( course_key) if verified_cohort_enabled and (instance.mode != instance._old_mode): # pylint: disable=protected-access if not is_course_cohorted(course_key): log.error( u"Automatic verified cohorting enabled for course '%s', but course is not cohorted.", course_key) else: course = get_course_by_id(course_key) existing_manual_cohorts = get_course_cohorts( course, assignment_type=CourseCohort.MANUAL) if any(cohort.name == verified_cohort_name for cohort in existing_manual_cohorts): # Get a random cohort to use as the default cohort (for audit learners). # Note that calling this method will create a "Default Group" random cohort if no random # cohort yet exist. random_cohort = get_random_cohort(course_key) args = { 'course_id': six.text_type(course_key), 'user_id': instance.user.id, 'verified_cohort_name': verified_cohort_name, 'default_cohort_name': random_cohort.name } log.info( u"Queuing automatic cohorting for user '%s' in course '%s' " u"due to change in enrollment mode from '%s' to '%s'.", instance.user.id, course_key, instance._old_mode, instance.mode # pylint: disable=protected-access ) # Do the update with a 3-second delay in hopes that the CourseEnrollment transaction has been # completed before the celery task runs. We want a reasonably short delay in case the learner # immediately goes to the courseware. sync_cohort_with_mode.apply_async(kwargs=args, countdown=3) # In case the transaction actually was not committed before the celery task runs, # run it again after 5 minutes. If the first completed successfully, this task will be a no-op. sync_cohort_with_mode.apply_async(kwargs=args, countdown=300) else: log.error( u"Automatic verified cohorting enabled for course '%s', " u"but verified cohort named '%s' does not exist.", course_key, verified_cohort_name, )
def move_to_verified_cohort(sender, instance, **kwargs): # pylint: disable=unused-argument """ If the learner has changed modes, update assigned cohort iff the course is using the Automatic Verified Track Cohorting MVP feature. """ course_key = instance.course_id verified_cohort_enabled = VerifiedTrackCohortedCourse.is_verified_track_cohort_enabled(course_key) verified_cohort_name = VerifiedTrackCohortedCourse.verified_cohort_name_for_course(course_key) if verified_cohort_enabled and (instance.mode != instance._old_mode): # pylint: disable=protected-access if not is_course_cohorted(course_key): log.error("Automatic verified cohorting enabled for course '%s', but course is not cohorted.", course_key) else: course = get_course_by_id(course_key) existing_manual_cohorts = get_course_cohorts(course, CourseCohort.MANUAL) if any(cohort.name == verified_cohort_name for cohort in existing_manual_cohorts): # Get a random cohort to use as the default cohort (for audit learners). # Note that calling this method will create a "Default Group" random cohort if no random # cohort yet exist. random_cohort = get_random_cohort(course_key) args = { 'course_id': unicode(course_key), 'user_id': instance.user.id, 'verified_cohort_name': verified_cohort_name, 'default_cohort_name': random_cohort.name } log.info( "Queuing automatic cohorting for user '%s' in course '%s' " "due to change in enrollment mode from '%s' to '%s'.", instance.user.id, course_key, instance._old_mode, instance.mode # pylint: disable=protected-access ) # Do the update with a 3-second delay in hopes that the CourseEnrollment transaction has been # completed before the celery task runs. We want a reasonably short delay in case the learner # immediately goes to the courseware. sync_cohort_with_mode.apply_async(kwargs=args, countdown=3) # In case the transaction actually was not committed before the celery task runs, # run it again after 5 minutes. If the first completed successfully, this task will be a no-op. sync_cohort_with_mode.apply_async(kwargs=args, countdown=300) else: log.error( "Automatic verified cohorting enabled for course '%s', " "but verified cohort named '%s' does not exist.", course_key, verified_cohort_name, )