Commit 6c6ba54e by Diana Huang

Create Course Modes django app

parent 57a80635
"""
Add and create new modes for running courses on this particular LMS
"""
from django.db import models
from collections import namedtuple
from django.utils.translation import ugettext as _
Mode = namedtuple('Mode', ['slug', 'name', 'min_price', 'suggested_prices'])
class CourseMode(models.Model):
"""
We would like to offer a course in a variety of modes.
"""
# the course that this mode is attached to
course_id = models.CharField(max_length=255, db_index=True)
# the reference to this mode that can be used by Enrollments to generate
# similar behavior for the same slug across courses
mode_slug = models.CharField(max_length=100)
# The 'pretty' name that can be translated and displayed
mode_display_name = models.CharField(max_length=255)
# minimum price in USD that we would like to charge for this mode of the course
min_price = models.IntegerField(default=0)
# the suggested prices for this mode
suggested_prices = models.CommaSeparatedIntegerField(max_length=255, blank=True, default='')
DEFAULT_MODE = Mode('honor', _('Honor Code Certificate'), 0, '')
@classmethod
def modes_for_course(cls, course_id):
"""
Returns a list of the modes for a given course id
If no modes have been set in the table, returns the default mode
"""
found_course_modes = cls.objects.filter(course_id=course_id)
modes = ([Mode(mode.mode_slug, mode.mode_display_name, mode.min_price, mode.suggested_prices)
for mode in found_course_modes])
if not modes:
modes = [cls.DEFAULT_MODE]
return modes
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
from course_modes.models import CourseMode, Mode
class CourseModeModelTest(TestCase):
"""
Tests for the CourseMode model
"""
def setUp(self):
self.course_id = 'TestCourse'
CourseMode.objects.all().delete()
def create_mode(self, mode_slug, mode_name, min_price=0, suggested_prices=''):
"""
Create a new course mode
"""
CourseMode.objects.get_or_create(
course_id=self.course_id,
mode_display_name=mode_name,
mode_slug=mode_slug,
min_price=min_price,
suggested_prices=suggested_prices
)
def test_modes_for_course_empty(self):
"""
If we can't find any modes, we should get back the default mode
"""
# shouldn't be able to find a corresponding course
modes = CourseMode.modes_for_course(self.course_id)
self.assertEqual([CourseMode.DEFAULT_MODE], modes)
def test_nodes_for_course_single(self):
"""
Find the modes for a course with only one mode
"""
self.create_mode('verified', 'Verified Certificate')
modes = CourseMode.modes_for_course(self.course_id)
self.assertEqual([Mode(u'verified', u'Verified Certificate', 0, '')], modes)
def test_modes_for_course_multiple(self):
"""
Finding the modes when there's multiple modes
"""
mode1 = Mode(u'honor', u'Honor Code Certificate', 0, '')
mode2 = Mode(u'verified', u'Verified Certificate', 0, '')
set_modes = [mode1, mode2]
for mode in set_modes:
self.create_mode(mode.slug, mode.name, mode.min_price, mode.suggested_prices)
modes = CourseMode.modes_for_course(self.course_id)
self.assertEqual(modes, set_modes)
......@@ -771,6 +771,9 @@ INSTALLED_APPS = (
# Notification preferences setting
'notification_prefs',
# Different Course Modes
'course_modes'
)
######################### MARKETING SITE ###############################
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment