def get_group_info_for_cohort(cohort, use_cached=False): """ Get the ids of the group and partition to which this cohort has been linked as a tuple of (int, int). If the cohort has not been linked to any group/partition, both values in the tuple will be None. The partition group info is cached for the duration of a request. Pass use_cached=True to use the cached value instead of fetching from the database. """ cache = RequestCache("cohorts.get_group_info_for_cohort").data cache_key = str(cohort.id) if use_cached and cache_key in cache: return cache[cache_key] cache.pop(cache_key, None) try: partition_group = CourseUserGroupPartitionGroup.objects.get(course_user_group=cohort) return cache.setdefault(cache_key, (partition_group.group_id, partition_group.partition_id)) except CourseUserGroupPartitionGroup.DoesNotExist: pass return cache.setdefault(cache_key, (None, None))
def get_group_info_for_cohort(cohort, use_cached=False): """ Get the ids of the group and partition to which this cohort has been linked as a tuple of (int, int). If the cohort has not been linked to any group/partition, both values in the tuple will be None. The partition group info is cached for the duration of a request. Pass use_cached=True to use the cached value instead of fetching from the database. """ cache = RequestCache(u"cohorts.get_group_info_for_cohort").data cache_key = six.text_type(cohort.id) if use_cached and cache_key in cache: return cache[cache_key] cache.pop(cache_key, None) try: partition_group = CourseUserGroupPartitionGroup.objects.get(course_user_group=cohort) return cache.setdefault(cache_key, (partition_group.group_id, partition_group.partition_id)) except CourseUserGroupPartitionGroup.DoesNotExist: pass return cache.setdefault(cache_key, (None, None))
def get_cohort(user, course_key, assign=True, use_cached=False): """ Returns the user's cohort for the specified course. The cohort for the user is cached for the duration of a request. Pass use_cached=True to use the cached value instead of fetching from the database. Arguments: user: a Django User object. course_key: CourseKey assign (bool): if False then we don't assign a group to user use_cached (bool): Whether to use the cached value or fetch from database. Returns: A CourseUserGroup object if the course is cohorted and the User has a cohort, else None. Raises: ValueError if the CourseKey doesn't exist. """ if user is None or user.is_anonymous: return None cache = RequestCache(COHORT_CACHE_NAMESPACE).data cache_key = _cohort_cache_key(user.id, course_key) if use_cached and cache_key in cache: return cache[cache_key] cache.pop(cache_key, None) # First check whether the course is cohorted (users shouldn't be in a cohort # in non-cohorted courses, but settings can change after course starts) if not is_course_cohorted(course_key): return cache.setdefault(cache_key, None) # If course is cohorted, check if the user already has a cohort. try: membership = CohortMembership.objects.get( course_id=course_key, user_id=user.id, ) return cache.setdefault(cache_key, membership.course_user_group) except CohortMembership.DoesNotExist: # Didn't find the group. If we do not want to assign, return here. if not assign: # Do not cache the cohort here, because in the next call assign # may be True, and we will have to assign the user a cohort. return None # Otherwise assign the user a cohort. try: # If learner has been pre-registered in a cohort, get that cohort. Otherwise assign to a random cohort. course_user_group = None for assignment in UnregisteredLearnerCohortAssignments.objects.filter(email=user.email, course_id=course_key): course_user_group = assignment.course_user_group assignment.delete() break else: course_user_group = get_random_cohort(course_key) add_user_to_cohort(course_user_group, user) return course_user_group except ValueError: # user already in cohort return course_user_group except IntegrityError as integrity_error: # An IntegrityError is raised when multiple workers attempt to # create the same row in one of the cohort model entries: # CourseCohort, CohortMembership. log.info( "HANDLING_INTEGRITY_ERROR: IntegrityError encountered for course '%s' and user '%s': %s", course_key, user.id, str(integrity_error) ) return get_cohort(user, course_key, assign, use_cached)
def get_cohort(user, course_key, assign=True, use_cached=False): """ Returns the user's cohort for the specified course. The cohort for the user is cached for the duration of a request. Pass use_cached=True to use the cached value instead of fetching from the database. Arguments: user: a Django User object. course_key: CourseKey assign (bool): if False then we don't assign a group to user use_cached (bool): Whether to use the cached value or fetch from database. Returns: A CourseUserGroup object if the course is cohorted and the User has a cohort, else None. Raises: ValueError if the CourseKey doesn't exist. """ if user.is_anonymous: return None cache = RequestCache(COHORT_CACHE_NAMESPACE).data cache_key = _cohort_cache_key(user.id, course_key) if use_cached and cache_key in cache: return cache[cache_key] cache.pop(cache_key, None) # First check whether the course is cohorted (users shouldn't be in a cohort # in non-cohorted courses, but settings can change after course starts) if not is_course_cohorted(course_key): return cache.setdefault(cache_key, None) # If course is cohorted, check if the user already has a cohort. try: membership = CohortMembership.objects.get( course_id=course_key, user_id=user.id, ) return cache.setdefault(cache_key, membership.course_user_group) except CohortMembership.DoesNotExist: # Didn't find the group. If we do not want to assign, return here. if not assign: # Do not cache the cohort here, because in the next call assign # may be True, and we will have to assign the user a cohort. return None # Otherwise assign the user a cohort. try: # If learner has been pre-registered in a cohort, get that cohort. Otherwise assign to a random cohort. course_user_group = None for assignment in UnregisteredLearnerCohortAssignments.objects.filter(email=user.email, course_id=course_key): course_user_group = assignment.course_user_group assignment.delete() break else: course_user_group = get_random_cohort(course_key) add_user_to_cohort(course_user_group, user) return course_user_group except ValueError: # user already in cohort return course_user_group except IntegrityError as integrity_error: # An IntegrityError is raised when multiple workers attempt to # create the same row in one of the cohort model entries: # CourseCohort, CohortMembership. log.info( u"HANDLING_INTEGRITY_ERROR: IntegrityError encountered for course '%s' and user '%s': %s", course_key, user.id, six.text_type(integrity_error) ) return get_cohort(user, course_key, assign, use_cached)