当前位置: 首页>>代码示例>>Python>>正文


Python RequestCache.pop方法代码示例

本文整理汇总了Python中edx_django_utils.cache.RequestCache.pop方法的典型用法代码示例。如果您正苦于以下问题:Python RequestCache.pop方法的具体用法?Python RequestCache.pop怎么用?Python RequestCache.pop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在edx_django_utils.cache.RequestCache的用法示例。


在下文中一共展示了RequestCache.pop方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_group_info_for_cohort

# 需要导入模块: from edx_django_utils.cache import RequestCache [as 别名]
# 或者: from edx_django_utils.cache.RequestCache import pop [as 别名]
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))
开发者ID:edx,项目名称:edx-platform,代码行数:29,代码来源:cohorts.py

示例2: get_cohort

# 需要导入模块: from edx_django_utils.cache import RequestCache [as 别名]
# 或者: from edx_django_utils.cache.RequestCache import pop [as 别名]
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)
开发者ID:edx,项目名称:edx-platform,代码行数:76,代码来源:cohorts.py


注:本文中的edx_django_utils.cache.RequestCache.pop方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。