fake_data_api.py 4.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
"""
A Fake Data API for testing purposes.
"""
import copy
import datetime

_DEFAULT_FAKE_MODE = {
    "slug": "honor",
    "name": "Honor Code Certificate",
    "min_price": 0,
    "suggested_prices": "",
    "currency": "usd",
    "expiration_datetime": None,
    "description": None
}

_ENROLLMENTS = []

_COURSES = []

21 22
_ENROLLMENT_ATTRIBUTES = []

Tasawer committed
23 24
_VERIFIED_MODE_EXPIRED = []

25

26
# pylint: disable=unused-argument
27 28 29 30 31 32 33 34 35 36
def get_course_enrollments(student_id):
    """Stubbed out Enrollment data request."""
    return _ENROLLMENTS


def get_course_enrollment(student_id, course_id):
    """Stubbed out Enrollment data request."""
    return _get_fake_enrollment(student_id, course_id)


37 38 39 40 41
def create_course_enrollment(student_id, course_id, mode='honor', is_active=True):
    """Stubbed out Enrollment creation request. """
    return add_enrollment(student_id, course_id, mode=mode, is_active=is_active)


42 43 44
def update_course_enrollment(student_id, course_id, mode=None, is_active=None):
    """Stubbed out Enrollment data request."""
    enrollment = _get_fake_enrollment(student_id, course_id)
45
    if enrollment and mode is not None:
46
        enrollment['mode'] = mode
47
    if enrollment and is_active is not None:
48 49 50 51
        enrollment['is_active'] = is_active
    return enrollment


52
def get_course_enrollment_info(course_id, include_expired=False):
53
    """Stubbed out Enrollment data request."""
Tasawer committed
54
    return _get_fake_course_info(course_id, include_expired)
55 56 57


def _get_fake_enrollment(student_id, course_id):
58
    """Get an enrollment from the enrollments array."""
59 60 61 62 63
    for enrollment in _ENROLLMENTS:
        if student_id == enrollment['student'] and course_id == enrollment['course']['course_id']:
            return enrollment


Tasawer committed
64
def _get_fake_course_info(course_id, include_expired=False):
65
    """Get a course from the courses array."""
Tasawer committed
66 67
    # if verified mode is expired and include expired is false
    # then remove the verified mode from the course.
68 69
    for course in _COURSES:
        if course_id == course['course_id']:
Tasawer committed
70 71
            if course_id in _VERIFIED_MODE_EXPIRED and not include_expired:
                course['course_modes'] = [mode for mode in course['course_modes'] if mode['slug'] != 'verified']
72 73 74 75
            return course


def add_enrollment(student_id, course_id, is_active=True, mode='honor'):
76
    """Append an enrollment to the enrollments array."""
77 78 79 80 81 82 83 84 85 86 87
    enrollment = {
        "created": datetime.datetime.now(),
        "mode": mode,
        "is_active": is_active,
        "course": _get_fake_course_info(course_id),
        "student": student_id
    }
    _ENROLLMENTS.append(enrollment)
    return enrollment


88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
# pylint: disable=unused-argument
def add_or_update_enrollment_attr(user_id, course_id, attributes):
    """Add or update enrollment attribute array"""
    for attribute in attributes:
        _ENROLLMENT_ATTRIBUTES.append({
            'namespace': attribute['namespace'],
            'name': attribute['name'],
            'value': attribute['value']
        })


# pylint: disable=unused-argument
def get_enrollment_attributes(user_id, course_id):
    """Retrieve enrollment attribute array"""
    return _ENROLLMENT_ATTRIBUTES


Tasawer committed
105 106 107 108 109
def set_expired_mode(course_id):
    """Set course verified mode as expired."""
    _VERIFIED_MODE_EXPIRED.append(course_id)


110
def add_course(course_id, enrollment_start=None, enrollment_end=None, invite_only=False, course_modes=None):
111
    """Append course to the courses array."""
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    course_info = {
        "course_id": course_id,
        "enrollment_end": enrollment_end,
        "course_modes": [],
        "enrollment_start": enrollment_start,
        "invite_only": invite_only,
    }
    if not course_modes:
        course_info['course_modes'].append(_DEFAULT_FAKE_MODE)
    else:
        for mode in course_modes:
            new_mode = copy.deepcopy(_DEFAULT_FAKE_MODE)
            new_mode['slug'] = mode
            course_info['course_modes'].append(new_mode)
    _COURSES.append(course_info)


def reset():
130 131
    """Set the enrollments and courses arrays to be empty."""
    global _COURSES  # pylint: disable=global-statement
132
    _COURSES = []
133
    global _ENROLLMENTS  # pylint: disable=global-statement
134
    _ENROLLMENTS = []
Tasawer committed
135 136
    global _VERIFIED_MODE_EXPIRED  # pylint: disable=global-statement
    _VERIFIED_MODE_EXPIRED = []