def get_cohort(user, course_key, assign=True, use_cached=False):
 request_cache = RequestCache.get_request_cache()
 cache_key = u'cohorts.get_cohort.{}.{}'.format(user.id, course_key)
 if (use_cached and (cache_key in request_cache.data)):
  return request_cache.data[cache_key]
 request_cache.data.pop(cache_key, None)
 course_cohort_settings = get_course_cohort_settings(course_key)
 if (not course_cohort_settings.is_cohorted):
  return request_cache.data.setdefault(cache_key, None)
 try:
  membership = CohortMembership.objects.get(course_id=course_key, user_id=user.id)
  return request_cache.data.setdefault(cache_key, membership.course_user_group)
 except CohortMembership.DoesNotExist:
  if (not assign):
   return None
 membership = CohortMembership.objects.create(user=user, course_user_group=get_random_cohort(course_key))
 return request_cache.data.setdefault(cache_key, membership.course_user_group)

def get_cohort ( user , course_key , assign = True , use_cached = False ) : request_cache = RequestCache . get_request_cache ( ) cache_key = u'cohorts.get_cohort.{}.{}' . format ( user . id , course_key ) if ( use_cached and ( cache_key in request_cache . data ) ) : return request_cache . data [ cache_key ] request_cache . data . pop ( cache_key , None ) course_cohort_settings = get_course_cohort_settings ( course_key ) if ( not course_cohort_settings . is_cohorted ) : return request_cache . data . setdefault ( cache_key , None ) try : membership = CohortMembership . objects . get ( course_id = course_key , user_id = user . id ) return request_cache . data . setdefault ( cache_key , membership . course_user_group ) except CohortMembership . DoesNotExist : if ( not assign ) : return None membership = CohortMembership . objects . create ( user = user , course_user_group = get_random_cohort ( course_key ) ) return request_cache . data . setdefault ( cache_key , membership . course_user_group )
returns the users cohort for the specified course .