Example #1
0
def _generate_grades(course_key, enrollments):
    """
    Load enrolled user grades for a program-course,
    using bulk fetching for efficiency.

    Arguments:
        course_key (CourseKey)
        enrollments (list[ProgramCourseEnrollment])

    Yields: BaseProgramCourseGrade
    """
    users = [enrollment.program_enrollment.user for enrollment in enrollments]
    prefetch_course_grades(course_key, users)
    try:
        grades_iter = CourseGradeFactory().iter(users, course_key=course_key)
        for enrollment, grade_tuple in zip(enrollments, grades_iter):
            user, course_grade, exception = grade_tuple
            if course_grade:
                yield ProgramCourseGradeOk(enrollment, course_grade)
            else:
                error_template = 'Failed to load course grade for user ID {} in {}: {}'
                error_string = error_template.format(
                    user.id, course_key,
                    text_type(exception) if exception else 'Unknown error')
                logger.error(error_string)
                yield ProgramCourseGradeError(enrollment, exception)
    finally:
        clear_prefetched_course_grades(course_key)
Example #2
0
def bulk_course_grade_context(course_key, users):
    """
    Prefetches grades for the given users in the given course
    within a context, storing in a RequestCache and deleting
    on context exit.
    """
    prefetch_course_grades(course_key, users)
    try:
        yield
    finally:
        clear_prefetched_course_grades(course_key)
Example #3
0
def bulk_course_grade_context(course_key, users):
    """
    Prefetches grades for the given users in the given course
    within a context, storing in a RequestCache and deleting
    on context exit.
    """
    prefetch_course_grades(course_key, users)
    try:
        yield
    finally:
        clear_prefetched_course_grades(course_key)