Beispiel #1
0
def get_course_enrollments(user_id):
    """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:
        user_id (str): The name of the user to retrieve course enrollment information for.

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

    """
    qset = CourseEnrollment.objects.filter(user__username=user_id,
                                           is_active=True).order_by('created')

    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(
            (u"Course enrollments for user %s reference "
             u"courses that do not exist (this can occur if a course is deleted)."
             ),
            user_id,
        )

    return valid
Beispiel #2
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)
        self.assertFalse(result.exists())

        # Create 10 test users to enroll in the course
        users = []
        for i in xrange(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,
                                              unicode(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)
        self.assertTrue(result.exists())
        self.assertEqual(
            CourseEnrollmentSerializer(results, many=True).data,
            created_enrollments)
Beispiel #3
0
def update_course_enrollment(student_id, course_id, mode=None, is_active=None):
    """Modify a course enrollment for a student.

    Allows updates to a specific course enrollment.

    Args:
        student_id (str): The name of the student to retrieve course enrollment information for.
        course_id (str): The course to retrieve course enrollment information for.
        mode (str): (Optional) The mode for the new enrollment.
        is_active (boolean): (Optional) Determines if the enrollment is active.

    Returns:
        A serializable dictionary representing the modified course enrollment.

    """
    course_key = CourseKey.from_string(course_id)
    student = User.objects.get(username=student_id)
    if not CourseEnrollment.is_enrolled(student, course_key):
        enrollment = CourseEnrollment.enroll(student, course_key, check_access=True)
    else:
        enrollment = CourseEnrollment.objects.get(user=student, course_id=course_key)

    enrollment.update_enrollment(is_active=is_active, mode=mode)
    enrollment.save()
    return CourseEnrollmentSerializer(enrollment).data  # pylint: disable=no-member
Beispiel #4
0
def get_course_enrollments(user_id=None, **kwargs):
    """
    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:
        user_id (str): The name of the user to retrieve course enrollment information for.
    Returns:
        A serializable list of dictionaries of all aggregated enrollment data for a user.
    """
    qset = CourseEnrollment.objects.filter(is_active=True, **kwargs)
    if user_id is not None:
        qset = qset.filter(user__username=user_id)
    qset = qset.order_by('created')
    return CourseEnrollmentSerializer(qset).data  # pylint: disable=no-member
Beispiel #5
0
def get_course_enrollments(student_id):
    """Retrieve a list representing all aggregated data for a student's course enrollments.

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

    Args:
        student_id (str): The name of the student to retrieve course enrollment information for.

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

    """
    qset = CourseEnrollment.objects.filter(
        user__username=student_id, is_active=True
    ).order_by('created')
    return CourseEnrollmentSerializer(qset).data  # pylint: disable=no-member
Beispiel #6
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  # pylint: disable=no-member
    except CourseEnrollment.DoesNotExist:
        return None
Beispiel #7
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
Beispiel #8
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  # pylint: disable=no-member