Ejemplo n.º 1
0
def serialize_course_run_enrollments_with_course_staff_to_csv(enrollments):
    """
    Serialize course run enrollments (course_staff field included) into a CSV-formatted string.

    Arguments:
        enrollments: list[dict]

    Returns: str
    """
    return serialize_to_csv(
        enrollments,
        ('course_id', 'student_key', 'status', 'account_exists', 'course_staff'),
        include_headers=True,
    )
Ejemplo n.º 2
0
def serialize_program_enrollments_to_csv(enrollments):
    """
    Serialize program enrollments into a CSV-formatted string.

    Arguments:
        enrollments: list[dict]

    Returns: str
    """
    return serialize_to_csv(
        enrollments,
        ('student_key', 'status', 'account_exists'),
        include_headers=True,
    )
Ejemplo n.º 3
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,
    )
Ejemplo n.º 4
0
    def test_serialize_data(self, include_headers):

        # Assert that our test data includes at least one field that will NOT
        # be serialized, ensuring that `serialize_to_csv` can handle extra
        # fields gracefully.
        data_fields = set(self.test_data[0].keys())
        serialize_fields = set(self.field_names)
        self.assertTrue(serialize_fields.issubset(data_fields))
        self.assertFalse(data_fields.issubset(serialize_fields))

        result = serialize_to_csv(self.test_data, self.field_names,
                                  include_headers)
        if include_headers:
            self.assertEqual(self.expected_headers + self.expected_csv, result)
        else:
            self.assertEqual(self.expected_csv, result)
Ejemplo n.º 5
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
    )
Ejemplo n.º 6
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()
    for request in requests:
        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)

    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.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)._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,
                )._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)

    results_str = serialize_to_csv(course_responses, CourseEnrollmentResponseItem._fields, include_headers=True)
    post_job_success(job_id, results_str, "csv", text=code_str)