api.py 2.85 KB
Newer Older
1 2 3 4
"""
Course API
"""

Sanford Student committed
5
from django.contrib.auth.models import User, AnonymousUser
6
from rest_framework.exceptions import PermissionDenied
7

8 9 10 11 12
from lms.djangoapps.courseware.courses import (
    get_courses,
    get_course_overview_with_access,
    get_permission_for_course_about,
)
13 14 15 16 17 18 19 20 21
from .permissions import can_view_courses_for_username


def get_effective_user(requesting_user, target_username):
    """
    Get the user we want to view information on behalf of.
    """
    if target_username == requesting_user.username:
        return requesting_user
Sanford Student committed
22 23
    elif target_username == '':
        return AnonymousUser()
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    elif can_view_courses_for_username(requesting_user, target_username):
        return User.objects.get(username=target_username)
    else:
        raise PermissionDenied()


def course_detail(request, username, course_key):
    """
    Return a single course identified by `course_key`.

    The course must be visible to the user identified by `username` and the
    logged-in user should have permission to view courses available to that
    user.

    Arguments:
        request (HTTPRequest):
            Used to identify the logged-in user and to instantiate the course
            module to retrieve the course about description
        username (string):
            The name of the user `requesting_user would like to be identified as.
        course_key (CourseKey): Identifies the course of interest

    Return value:
47
        `CourseOverview` object representing the requested course
48 49
    """
    user = get_effective_user(request.user, username)
50 51 52 53 54
    return get_course_overview_with_access(
        user,
        get_permission_for_course_about(),
        course_key,
    )
55 56


57
def list_courses(request, username, org=None, filter_=None):
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    """
    Return a list of available courses.

    The courses returned are all be visible to the user identified by
    `username` and the logged in user should have permission to view courses
    available to that user.

    Arguments:
        request (HTTPRequest):
            Used to identify the logged-in user and to instantiate the course
            module to retrieve the course about description
        username (string):
            The name of the user the logged-in user would like to be
            identified as

73 74 75 76 77
    Keyword Arguments:
        org (string):
            If specified, visible `CourseOverview` objects are filtered
            such that only those belonging to the organization with the provided
            org code (e.g., "HarvardX") are returned. Case-insensitive.
78 79 80
        filter_ (dict):
            If specified, visible `CourseOverview` objects are filtered
            by the given key-value pairs.
81 82

    Return value:
83
        List of `CourseOverview` objects representing the collection of courses.
84 85
    """
    user = get_effective_user(request.user, username)
86
    return get_courses(user, org=org, filter_=filter_)