Пример #1
0
    def test_get_user_enrollments(self, course_modes, enrollment_mode):
        self._create_course_modes(course_modes)

        # Try to get enrollments before they exist.
        result = data.get_user_enrollments(self.course.id)
        assert not result.exists()

        # Create 10 test users to enroll in the course
        users = []
        for i in range(10):
            users.append(
                UserFactory.create(username=self.USERNAME + str(i),
                                   email=self.EMAIL + str(i),
                                   password=self.PASSWORD + str(i)))

        # Create the original enrollments.
        created_enrollments = []
        for user in users:
            created_enrollments.append(
                data.create_course_enrollment(user.username,
                                              six.text_type(self.course.id),
                                              enrollment_mode, True))

        # Compare the created enrollments with the results
        # from the get user enrollments request.
        results = data.get_user_enrollments(self.course.id)
        assert result.exists()
        assert CourseEnrollmentSerializer(
            results, many=True).data == created_enrollments
Пример #2
0
def get_course_enrollment(username, course_id):
    """Retrieve an object representing all aggregated data for a user's course enrollment.

    Get the course enrollment information for a specific user and course.

    Args:
        username (str): The name of the user to retrieve course enrollment information for.
        course_id (str): The course to retrieve course enrollment information for.

    Returns:
        A serializable dictionary representing the course enrollment.

    """
    course_key = CourseKey.from_string(course_id)
    try:
        enrollment = CourseEnrollment.objects.get(user__username=username,
                                                  course_id=course_key)
        return CourseEnrollmentSerializer(enrollment).data
    except CourseEnrollment.DoesNotExist:
        return None
Пример #3
0
def get_course_enrollments(username, include_inactive=False):
    """Retrieve a list representing all aggregated data for a user's course enrollments.

    Construct a representation of all course enrollment data for a specific user.

    Args:
        username: The name of the user to retrieve course enrollment information for.
        include_inactive (bool): Determines whether inactive enrollments will be included


    Returns:
        A serializable list of dictionaries of all aggregated enrollment data for a user.

    """
    qset = CourseEnrollment.objects.filter(
        user__username=username, ).order_by('created')

    if not include_inactive:
        qset = qset.filter(is_active=True)

    enrollments = CourseEnrollmentSerializer(qset, many=True).data

    # Find deleted courses and filter them out of the results
    deleted = []
    valid = []
    for enrollment in enrollments:
        if enrollment.get("course_details") is not None:
            valid.append(enrollment)
        else:
            deleted.append(enrollment)

    if deleted:
        log.warning(
            ("Course enrollments for user %s reference "
             "courses that do not exist (this can occur if a course is deleted)."
             ),
            username,
        )

    return valid
Пример #4
0
def serialize_enrollments(enrollments):
    """
    Take CourseEnrollment objects and return them in a serialized list.
    """
    return CourseEnrollmentSerializer(enrollments, many=True).data
Пример #5
0
def _update_enrollment(enrollment, is_active=None, mode=None):
    enrollment.update_enrollment(is_active=is_active, mode=mode)
    enrollment.save()
    return CourseEnrollmentSerializer(enrollment).data