Пример #1
0
def serialize_course_run_enrollments_to_csv(enrollments):
    """
    Serialize course run enrollments into a CSV-formatted string.

    Arguments:
        enrollments: list[dict]

    Returns: str
    """
    return serialize_to_csv(
        enrollments,
        ('course_id', 'student_key', 'status', 'account_exists'),
        include_headers=True,
    )
Пример #2
0
def serialize_course_run_grades_to_csv(grades):
    """
    Serialize grades into a CSV-formatted string.

    Arguments:
        grades (list[dict]):
            List of grade responses

    Returns: str
    """
    return serialize_to_csv(
        grades,
        ('student_key', 'letter_grade', 'percent', 'passed', 'error'),
        include_headers=True,
    )
Пример #3
0
def serialize_enrollment_results_to_csv(enrollment_results):
    """
    Serialize enrollment results into a CSV-formatted string.

    Arguments:
        enrollment_results (dict[str: str]):
            Mapping from student keys to enrollment statuses.

    Returns: str
    """
    enrollment_results_list = [{
        "student_key": student_key,
        "status": status,
    } for student_key, status in enrollment_results.items()]
    return serialize_to_csv(enrollment_results_list, ('student_key', 'status'),
                            include_headers=True)
Пример #4
0
def serialize_program_enrollments_to_csv(enrollments,
                                         include_username_email=False):
    """
    Serialize program enrollments into a CSV-formatted string.

    Arguments:
        enrollments: list[dict]

    Returns: str
    """
    if include_username_email:
        field_names = ('student_key', 'status', 'account_exists', 'username',
                       'email')
    else:
        field_names = ('student_key', 'status', 'account_exists')

    return serialize_to_csv(
        enrollments,
        field_names,
        include_headers=True,
    )
Пример #5
0
def write_course_run_enrollments(self, job_id, user_id, json_filepath,
                                 program_key):
    """
    A user task that reads course run enrollment requests from json_filepath,
    writes them to the LMS, and stores a CSV-formatted results file.
    """
    program = get_program(job_id, program_key)
    if not program:
        return

    requests = _load_enrollment_requests(job_id, program_key, json_filepath)
    if requests is None:
        return

    requests_by_course_key = OrderedDict()
    student_to_course_staff = OrderedDict()
    include_course_staff = False
    for request in requests:
        if 'course_staff' in request:
            include_course_staff = True
        requested_course_key = request.pop('course_id')
        if requested_course_key not in requests_by_course_key:
            requests_by_course_key[requested_course_key] = []
        requests_by_course_key[requested_course_key].append(request)

        requested_student_key = request.get('student_key')
        requested_course_staff = request.get('course_staff')
        student_to_course_staff[requested_student_key] = requested_course_staff

    successes = []
    failures = []
    course_responses = []

    for requested_course_key, course_requests in requests_by_course_key.items(
    ):
        # we don't know if the requested key was an external key or internal key,
        # so convert it before making requests to the LMS.
        internal_course_key = program.details.get_course_key(
            requested_course_key)

        if internal_course_key:
            successes_in_course, failures_in_course, status_by_student_key = lms.write_course_run_enrollments(
                'PUT', program.discovery_uuid, internal_course_key,
                course_requests)
            course_responses.extend([
                CourseEnrollmentResponseItem(
                    requested_course_key, student_key, status,
                    student_to_course_staff[student_key])._asdict()
                for student_key, status in status_by_student_key.items()
            ])
            successes.append(successes_in_course)
            failures.append(failures_in_course)
        else:
            course_responses.extend([
                CourseEnrollmentResponseItem(
                    requested_course_key, request.get('student_key'),
                    ENROLLMENT_ERROR_COURSE_NOT_FOUND, student_to_course_staff[
                        request.get('student_key')])._asdict()
                for request in course_requests
            ])
            failures.append(True)

    if any(successes) and any(failures):
        code_str = str(EnrollmentWriteStatus.MULTI_STATUS.value)
    elif any(successes):
        code_str = str(EnrollmentWriteStatus.OK.value)
    elif any(failures):
        code_str = str(EnrollmentWriteStatus.UNPROCESSABLE_ENTITY.value)
    else:
        # This only happens if no enrollments are given.
        code_str = str(EnrollmentWriteStatus.NO_CONTENT.value)

    course_enrollment_response_format = list(
        CourseEnrollmentResponseItem._fields)
    if not include_course_staff:
        course_enrollment_response_format.remove('course_staff')

    results_str = serialize_to_csv(course_responses,
                                   course_enrollment_response_format,
                                   include_headers=True)
    post_job_success(job_id, results_str, "csv", text=code_str)