Example #1
0
    def test_course_run_not_fullfillable_enroll_period_ended(self):
        course_overview = self.create_course(start_from_now=-3,
                                             end_from_now=2,
                                             enrollment_start_from_now=-2,
                                             enrollment_end_from_now=-1)

        entitlement = CourseEntitlementFactory.create(mode=CourseMode.VERIFIED)

        assert not is_course_run_entitlement_fullfillable(
            course_overview.id, entitlement)
Example #2
0
    def test_course_run_fullfillable_user_enrolled(self):
        course_overview = self.create_course(start_from_now=-3,
                                             end_from_now=2,
                                             enrollment_start_from_now=-2,
                                             enrollment_end_from_now=1)

        entitlement = CourseEntitlementFactory.create(mode=CourseMode.VERIFIED)
        # Enroll User in the Course, but do not update the entitlement
        CourseEnrollmentFactory.create(user=entitlement.user,
                                       course_id=course_overview.id)

        assert is_course_run_entitlement_fullfillable(course_overview.id,
                                                      entitlement)
Example #3
0
def get_fulfillable_course_runs_for_entitlement(entitlement, course_runs):
    """
    Takes a list of course runs and returns only the course runs, sorted by start date, that:

    These are the only sessions that can be selected for an entitlement.
    """
    enrollable_sessions = []

    # Only show published course runs that can still be enrolled and upgraded
    search_time = datetime.datetime.now(UTC)
    for course_run in course_runs:
        course_id = CourseKey.from_string(course_run.get('key'))
        is_enrolled = CourseEnrollment.is_enrolled(entitlement.user, course_id)
        if is_course_run_entitlement_fullfillable(course_id, entitlement, search_time):
            if (is_enrolled and
                    entitlement.enrollment_course_run and
                    course_id == entitlement.enrollment_course_run.course_id):
                enrollable_sessions.append(course_run)
            elif not is_enrolled:
                enrollable_sessions.append(course_run)

    enrollable_sessions.sort(key=lambda session: session.get('start'))
    return enrollable_sessions
Example #4
0
    def create(self, request, uuid):
        """
        On POST this method will be called and will handle enrolling a user in the
        provided course_run_id from the data. This is called on a specific entitlement
        UUID so the course_run_id has to correspond to the Course that is assigned to
        the Entitlement.

        When this API is called for a user who is already enrolled in a run that User
        will be unenrolled from their current run and enrolled in the new run if it is
        available.
        """
        course_run_id = request.data.get('course_run_id', None)

        if not course_run_id:
            return Response(status=status.HTTP_400_BAD_REQUEST,
                            data='The Course Run ID was not provided.')

        # Verify that the user has an Entitlement for the provided Entitlement UUID.
        try:
            entitlement = CourseEntitlement.objects.get(uuid=uuid,
                                                        user=request.user,
                                                        expired_at=None)
        except CourseEntitlement.DoesNotExist:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data=
                'The Entitlement for this UUID does not exist or is Expired.')

        # Verify the course run ID is of the same Course as the Course entitlement.
        course_run_valid = self._verify_course_run_for_entitlement(
            entitlement, course_run_id)
        if not course_run_valid:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    'message':
                    'The Course Run ID is not a match for this Course Entitlement.'
                })

        try:
            course_run_key = CourseKey.from_string(course_run_id)
        except InvalidKeyError:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    'message':
                    'Invalid {course_id}'.format(course_id=course_run_id)
                })

        # Verify that the run is fullfillable
        if not is_course_run_entitlement_fullfillable(course_run_key,
                                                      entitlement):
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    'message':
                    'The User is unable to enroll in Course Run {course_id}, it is not available.'
                    .format(course_id=course_run_id)
                })

        # Determine if this is a Switch session or a simple enroll and handle both.
        if entitlement.enrollment_course_run is None:
            response = self._enroll_entitlement(entitlement=entitlement,
                                                course_run_key=course_run_key,
                                                user=request.user)
            if response:
                return response
        elif entitlement.enrollment_course_run.course_id != course_run_id:
            _unenroll_entitlement(
                course_entitlement=entitlement,
                course_run_key=entitlement.enrollment_course_run.course_id)
            response = self._enroll_entitlement(entitlement=entitlement,
                                                course_run_key=course_run_key,
                                                user=request.user)
            if response:
                return response

        return Response(status=status.HTTP_201_CREATED,
                        data={
                            'course_run_id': course_run_id,
                        })