common.py 6.28 KB
Newer Older
Don Mitchell committed
1 2
# pylint: disable=C0111
# pylint: disable=W0621
3

4 5
from __future__ import absolute_import

6
from lettuce import world, step
7 8
from django.contrib.auth.models import User
from student.models import CourseEnrollment
9
from xmodule.modulestore import Location
10
from xmodule.modulestore.django import modulestore
11 12 13
from xmodule.course_module import CourseDescriptor
from courseware.courses import get_course_by_id
from xmodule import seq_module, vertical_module
14 15 16 17

from logging import getLogger
logger = getLogger(__name__)

Will Daly committed
18

19
@step(u'The course "([^"]*)" exists$')
20
def create_course(_step, course):
21 22 23 24

    # First clear the modulestore so we don't try to recreate
    # the same course twice
    # This also ensures that the necessary templates are loaded
25
    world.clear_courses()
26 27 28 29

    # Create the course
    # We always use the same org and display name,
    # but vary the course identifier (e.g. 600x or 191x)
30
    world.scenario_dict['COURSE'] = world.CourseFactory.create(org='edx',
31
                                        number=course,
32
                                        display_name='Test Course')
33 34

    # Add a section to the course to contain problems
35 36
    world.scenario_dict['SECTION'] = world.ItemFactory.create(parent_location=world.scenario_dict['COURSE'].location,
                                       display_name='Test Section')
37

38 39 40 41
    world.ItemFactory.create(
        parent_location=world.scenario_dict['SECTION'].location,
        category='sequential',
        display_name='Test Section')
42

Will Daly committed
43

44
@step(u'I am registered for the course "([^"]*)"$')
45 46 47 48 49
def i_am_registered_for_the_course(step, course):
    # Create the course
    create_course(step, course)

    # Create the user
50
    world.create_user('robot', 'test')
51
    u = User.objects.get(username='robot')
52 53

    # If the user is not already enrolled, enroll the user.
Jay Zoldak committed
54
    # TODO: change to factory
55
    CourseEnrollment.enroll(u, course_id(course))
56

57
    world.log_in(username='robot', password='test')
Calen Pennington committed
58

Will Daly committed
59

60
@step(u'The course "([^"]*)" has extra tab "([^"]*)"$')
61 62 63 64 65
def add_tab_to_course(_step, course, extra_tab_name):
    world.ItemFactory.create(
        parent_location=course_location(course),
        category="static_tab",
        display_name=str(extra_tab_name))
66

Calen Pennington committed
67

68 69 70 71 72 73 74
@step(u'I am in a course$')
def go_into_course(step):
    step.given('I am registered for the course "6.002x"')
    step.given('And I am logged in')
    step.given('And I click on View Courseware')


75
def course_id(course_num):
76 77
    return "%s/%s/%s" % (world.scenario_dict['COURSE'].org, course_num,
                         world.scenario_dict['COURSE'].display_name.replace(" ", "_"))
78

Will Daly committed
79

80 81
def course_location(course_num):
    return Location(loc_or_tag="i4x",
82
                    org=world.scenario_dict['COURSE'].org,
83 84
                    course=course_num,
                    category='course',
85
                    name=world.scenario_dict['COURSE'].display_name.replace(" ", "_"))
86

Will Daly committed
87

88 89
def section_location(course_num):
    return Location(loc_or_tag="i4x",
90
                    org=world.scenario_dict['COURSE'].org,
91 92
                    course=course_num,
                    category='sequential',
93
                    name=world.scenario_dict['SECTION'].display_name.replace(" ", "_"))
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177


def get_courses():
    '''
    Returns dict of lists of courses available, keyed by course.org (ie university).
    Courses are sorted by course.number.
    '''
    courses = [c for c in modulestore().get_courses()
               if isinstance(c, CourseDescriptor)]
    courses = sorted(courses, key=lambda course: course.number)
    return courses


def get_courseware_with_tabs(course_id):
    """
    Given a course_id (string), return a courseware array of dictionaries for the
    top three levels of navigation. Same as get_courseware() except include
    the tabs on the right hand main navigation page.

    This hides the appropriate courseware as defined by the hide_from_toc field:
    chapter.lms.hide_from_toc

    Example:

    [{
        'chapter_name': 'Overview',
        'sections': [{
            'clickable_tab_count': 0,
            'section_name': 'Welcome',
            'tab_classes': []
        }, {
            'clickable_tab_count': 1,
            'section_name': 'System Usage Sequence',
            'tab_classes': ['VerticalDescriptor']
        }, {
            'clickable_tab_count': 0,
            'section_name': 'Lab0: Using the tools',
            'tab_classes': ['HtmlDescriptor', 'HtmlDescriptor', 'CapaDescriptor']
        }, {
            'clickable_tab_count': 0,
            'section_name': 'Circuit Sandbox',
            'tab_classes': []
        }]
    }, {
        'chapter_name': 'Week 1',
        'sections': [{
            'clickable_tab_count': 4,
            'section_name': 'Administrivia and Circuit Elements',
            'tab_classes': ['VerticalDescriptor', 'VerticalDescriptor', 'VerticalDescriptor', 'VerticalDescriptor']
        }, {
            'clickable_tab_count': 0,
            'section_name': 'Basic Circuit Analysis',
            'tab_classes': ['CapaDescriptor', 'CapaDescriptor', 'CapaDescriptor']
        }, {
            'clickable_tab_count': 0,
            'section_name': 'Resistor Divider',
            'tab_classes': []
        }, {
            'clickable_tab_count': 0,
            'section_name': 'Week 1 Tutorials',
            'tab_classes': []
        }]
    }, {
        'chapter_name': 'Midterm Exam',
        'sections': [{
            'clickable_tab_count': 2,
            'section_name': 'Midterm Exam',
            'tab_classes': ['VerticalDescriptor', 'VerticalDescriptor']
        }]
    }]
    """

    course = get_course_by_id(course_id)
    chapters = [chapter for chapter in course.get_children() if not chapter.lms.hide_from_toc]
    courseware = [{'chapter_name': c.display_name_with_default,
                   'sections': [{'section_name': s.display_name_with_default,
                                'clickable_tab_count': len(s.get_children()) if (type(s) == seq_module.SequenceDescriptor) else 0,
                                'tabs': [{'children_count': len(t.get_children()) if (type(t) == vertical_module.VerticalDescriptor) else 0,
                                         'class': t.__class__.__name__}
                                         for t in s.get_children()]}
                                for s in c.get_children() if not s.lms.hide_from_toc]}
                  for c in chapters]

    return courseware