Exemple #1
0
def _get_course(course_key, user):
    """
    Get the course descriptor, raising CourseNotFoundError if the course is not found or
    the user cannot access forums for the course, and DiscussionDisabledError if the
    discussion tab is disabled for the course.
    """
    try:
        course = get_course_with_access(user,
                                        'load',
                                        course_key,
                                        check_if_enrolled=True)
    except Http404:
        # Convert 404s into CourseNotFoundErrors.
        raise CourseNotFoundError("Course not found.")
    except CourseAccessRedirect:
        # Raise course not found if the user cannot access the course
        # since it doesn't make sense to redirect an API.
        raise CourseNotFoundError("Course not found.")
    if not any([
            tab.type == 'discussion' and tab.is_enabled(course, user)
            for tab in course.tabs
    ]):
        raise DiscussionDisabledError("Discussion is disabled for the course.")
    return course
Exemple #2
0
def _get_course(course_key, user):
    """
    Get the course descriptor, raising CourseNotFoundError if the course is not found or
    the user cannot access forums for the course, and DiscussionDisabledError if the
    discussion tab is disabled for the course.
    """
    try:
        course = get_course_with_access(user, 'load', course_key, check_if_enrolled=True)
    except (Http404, CourseAccessRedirect) as err:
        # Convert 404s into CourseNotFoundErrors.
        # Raise course not found if the user cannot access the course
        raise CourseNotFoundError("Course not found.") from err
    if not any(tab.type == 'discussion' and tab.is_enabled(course, user) for tab in course.tabs):
        raise DiscussionDisabledError("Discussion is disabled for the course.")
    return course
Exemple #3
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)
    site_course_org_filter = configuration_helpers.get_value('course_org_filter')

    if site_course_org_filter == course_key.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
Exemple #4
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)

    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        msg = u"Not user with username '{username}' found.".format(
            username=username)
        log.warning(msg)
        raise UserNotFoundError(msg)  # lint-amnesty, pylint: disable=raise-missing-from

    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(text_type(err))  # lint-amnesty, pylint: disable=raise-missing-from
    except EnrollmentClosedError as err:
        raise CourseEnrollmentClosedError(text_type(err))  # lint-amnesty, pylint: disable=raise-missing-from
    except CourseFullError as err:
        raise CourseEnrollmentFullError(text_type(err))  # lint-amnesty, pylint: disable=raise-missing-from
    except AlreadyEnrolledError as err:
        enrollment = get_course_enrollment(username, course_id)
        raise CourseEnrollmentExistsError(text_type(err), enrollment)  # lint-amnesty, pylint: disable=raise-missing-from
Exemple #5
0
def _get_course(course_key, user):
    """
    Get the course descriptor, raising CourseNotFoundError if the course is not found or
    the user cannot access forums for the course, and DiscussionDisabledError if the
    discussion tab is disabled for the course.
    """
    try:
        course = get_course_with_access(user,
                                        'load',
                                        course_key,
                                        check_if_enrolled=True)
    except Http404:
        raise CourseNotFoundError("Course not found.")
    if not any([
            tab.type == 'discussion' and tab.is_enabled(course, user)
            for tab in course.tabs
    ]):
        raise DiscussionDisabledError("Discussion is disabled for the course.")
    return course