예제 #1
0
def update_course_enrollment(username, course_id, mode=None, is_active=None):
    """Modify a course enrollment for a user.

    Allows updates to a specific course enrollment.

    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.
        mode (str): (Optional) If specified, modify the mode for this 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)

    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        msg = u"Not user with username '{username}' found.".format(username=username)
        log.warn(msg)
        raise UserNotFoundError(msg)

    try:
        enrollment = CourseEnrollment.objects.get(user=user, course_id=course_key)
        return _update_enrollment(enrollment, is_active=is_active, mode=mode)
    except CourseEnrollment.DoesNotExist:
        return None
예제 #2
0
def create_course_enrollment(username, course_id, mode, is_active):
    """Create a new course enrollment for the given user.

    Creates a new course enrollment for the specified user username.

    Args:
        username (str): The name of the user to create a new course enrollment for.
        course_id (str): The course to create the course enrollment 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 new course enrollment.

    Raises:
        CourseNotFoundError
        CourseEnrollmentFullError
        EnrollmentClosedError
        CourseEnrollmentExistsError

    """
    course_key = CourseKey.from_string(course_id)
    current_course_org = course_id.split(':')[1].split('+')[0]
    site_course_org_filter = configuration_helpers.get_value(
        'course_org_filter')
    if site_course_org_filter == current_course_org or not site_course_org_filter:
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            msg = u"Not user with username '{username}' found.".format(
                username=username)
            log.warn(msg)
            raise UserNotFoundError(msg)

        try:
            enrollment = CourseEnrollment.enroll(user,
                                                 course_key,
                                                 check_access=True)
            return _update_enrollment(enrollment,
                                      is_active=is_active,
                                      mode=mode)
        except NonExistentCourseError as err:
            raise CourseNotFoundError(err.message)
        except EnrollmentClosedError as err:
            raise CourseEnrollmentClosedError(err.message)
        except CourseFullError as err:
            raise CourseEnrollmentFullError(err.message)
        except AlreadyEnrolledError as err:
            enrollment = get_course_enrollment(username, course_id)
            raise CourseEnrollmentExistsError(err.message, enrollment)
    else:
        return NonExistentCourseError
예제 #3
0
def _get_user(user_id):
    """Retrieve user with provided user_id

    Args:
        user_id(str): username of the user for which object is to retrieve

    Returns: obj
    """
    try:
        return User.objects.get(username=user_id)
    except User.DoesNotExist:
        msg = u"Not user with username '{username}' found.".format(username=user_id)
        log.warn(msg)
        raise UserNotFoundError(msg)
예제 #4
0
파일: data.py 프로젝트: lumsx/edx-platform
def create_course_enrollment(username, course_id, mode, is_active):
    """Create a new course enrollment for the given user.

    Creates a new course enrollment for the specified user username.

    Args:
        username (str): The name of the user to create a new course enrollment for.
        course_id (str): The course to create the course enrollment 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 new course enrollment.

    Raises:
        CourseNotFoundError
        CourseEnrollmentFullError
        EnrollmentClosedError
        CourseEnrollmentExistsError

    """
    course_key = CourseKey.from_string(course_id)

    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        msg = u"Not user with username '{username}' found.".format(
            username=username)
        log.warn(msg)
        raise UserNotFoundError(msg)

    try:
        enrollment = CourseEnrollment.enroll(user,
                                             course_key,
                                             check_access=True)
        handle_course_enrollment(course_id, user, 'enroll')
        return _update_enrollment(enrollment, is_active=is_active, mode=mode)
    except NonExistentCourseError as err:
        raise CourseNotFoundError(text_type(err))
    except EnrollmentClosedError as err:
        raise CourseEnrollmentClosedError(text_type(err))
    except CourseFullError as err:
        raise CourseEnrollmentFullError(text_type(err))
    except AlreadyEnrolledError as err:
        enrollment = get_course_enrollment(username, course_id)
        raise CourseEnrollmentExistsError(text_type(err), enrollment)
예제 #5
0
def add_enrollment(user_id,
                   course_id,
                   mode=None,
                   is_active=True,
                   enrollment_attributes=None,
                   user=None,
                   is_ecommerce_request=False):
    """Enrolls a user in a course.

    Enrolls a user in a course. If the mode is not specified, this will default to `CourseMode.DEFAULT_MODE_SLUG`.

    Arguments:
        user_id (str): The user to enroll.
        course_id (str): The course to enroll the user in.
        mode (str): Optional argument for the type of enrollment to create. Ex. 'audit', 'honor', 'verified',
            'professional'. If not specified, this defaults to the default course mode.
        is_active (boolean): Optional argument for making the new enrollment inactive. If not specified, is_active
            defaults to True.
        enrollment_attributes (list): Attributes to be set the enrollment.

    Returns:
        A serializable dictionary of the new course enrollment.

    Example:
        >>> add_enrollment("Bob", "edX/DemoX/2014T2", mode="audit")
        {
            "created": "2014-10-20T20:18:00Z",
            "mode": "audit",
            "is_active": True,
            "user": "******",
            "course_details": {
                "course_id": "edX/DemoX/2014T2",
                "course_name": "edX Demonstration Course",
                "enrollment_end": "2014-12-20T20:18:00Z",
                "enrollment_start": "2014-10-15T20:18:00Z",
                "course_start": "2015-02-03T00:00:00Z",
                "course_end": "2015-05-06T00:00:00Z",
                "course_modes": [
                    {
                        "slug": "audit",
                        "name": "Audit",
                        "min_price": 0,
                        "suggested_prices": "",
                        "currency": "usd",
                        "expiration_datetime": null,
                        "description": null,
                        "sku": null,
                        "bulk_sku": null
                    }
                ],
                "invite_only": False
            }
        }
    """
    if mode is None:
        mode = _default_course_mode(course_id)
    validate_course_mode(course_id, mode, is_active=is_active)

    if settings.FEATURES.get(
            'ENABLE_MEMBERSHIP_INTEGRATION') and is_ecommerce_request:
        from membership.models import VIPCourseEnrollment
        course_key = CourseKey.from_string(course_id)
        VIPCourseEnrollment.objects.filter(
            user=user, course_id=course_key).update(is_active=False)

    with transaction.atomic():
        enrollment = _data_api().create_course_enrollment(
            user_id, course_id, mode, is_active)

        if enrollment_attributes is not None:
            set_enrollment_attributes(user_id, course_id,
                                      enrollment_attributes)

        if settings.FEATURES.get('ENABLE_MEMBERSHIP_INTEGRATION',
                                 False) and not is_ecommerce_request:
            from membership.models import VIPCourseEnrollment
            from student.models import User

            if user is None:
                try:
                    user = User.objects.get(username=user_id)
                except User.DoesNotExist:
                    msg = u"Not user with username '{username}' found.".format(
                        username=user_id)
                    log.warn(msg)
                    raise UserNotFoundError(msg)

            course_key = CourseKey.from_string(course_id)
            can_vip_enroll = VIPCourseEnrollment.can_vip_enroll(
                user, course_key)
            if mode in ('professional', 'no-id-professional', 'verified'):
                if not can_vip_enroll:
                    msg = u"Sorry, your VIP has expired."
                    error_data = {
                        'mode': mode,
                        'can_vip_enroll': can_vip_enroll
                    }
                    raise errors.CourseModeNotFoundError(msg, error_data)
                else:
                    VIPCourseEnrollment.enroll(user, course_key)

    return enrollment