Changes after Andy's Review
This commit is contained in:
committed by
Usman Khalid
parent
3ce494f5c5
commit
70efd28483
@@ -456,9 +456,12 @@ def set_course_cohort_settings(course_key, **kwargs):
|
||||
Raises:
|
||||
ValueError if course_key is invalid.
|
||||
"""
|
||||
fields = {'is_cohorted': bool, 'always_cohort_inline_discussions': bool, 'cohorted_discussions': list}
|
||||
course_cohort_settings = get_course_cohort_settings(course_key)
|
||||
for field in ('is_cohorted', 'always_cohort_inline_discussions', 'cohorted_discussions'):
|
||||
for field, field_type in fields.items():
|
||||
if field in kwargs:
|
||||
if not isinstance(kwargs[field], field_type):
|
||||
raise ValueError("Incorrect field type for `{}`. Type must be `{}`".format(field, field_type.__name__))
|
||||
setattr(course_cohort_settings, field, kwargs[field])
|
||||
course_cohort_settings.save()
|
||||
return course_cohort_settings
|
||||
|
||||
@@ -93,12 +93,12 @@ class CourseCohortsSettings(models.Model):
|
||||
|
||||
@property
|
||||
def cohorted_discussions(self):
|
||||
"""Jsonfiy the cohorted_discussions"""
|
||||
"""Jsonify the cohorted_discussions"""
|
||||
return json.loads(self._cohorted_discussions)
|
||||
|
||||
@cohorted_discussions.setter
|
||||
def cohorted_discussions(self, value):
|
||||
"""UnJsonfiy the cohorted_discussions"""
|
||||
"""Un-Jsonify the cohorted_discussions"""
|
||||
self._cohorted_discussions = json.dumps(value)
|
||||
|
||||
|
||||
|
||||
@@ -716,6 +716,29 @@ class TestCohorts(ModuleStoreTestCase):
|
||||
self.assertEqual(course_cohort_settings.cohorted_discussions, ['topic a id', 'topic b id'])
|
||||
self.assertFalse(course_cohort_settings.always_cohort_inline_discussions)
|
||||
|
||||
def test_update_course_cohort_settings_with_invalid_data_type(self):
|
||||
"""
|
||||
Test that cohorts.set_course_cohort_settings raises exception if fields have incorrect data type.
|
||||
"""
|
||||
course = modulestore().get_course(self.toy_course_key)
|
||||
CourseCohortSettingsFactory(course_id=course.id)
|
||||
|
||||
exception_msg_tpl = "Incorrect field type for `{}`. Type must be `{}`"
|
||||
fields = [
|
||||
{'name': 'is_cohorted', 'type': bool},
|
||||
{'name': 'always_cohort_inline_discussions', 'type': bool},
|
||||
{'name': 'cohorted_discussions', 'type': list}
|
||||
]
|
||||
|
||||
for field in fields:
|
||||
with self.assertRaises(ValueError) as value_error:
|
||||
cohorts.set_course_cohort_settings(course.id, **{field['name']: ''})
|
||||
|
||||
self.assertEqual(
|
||||
value_error.exception.message,
|
||||
exception_msg_tpl.format(field['name'], field['type'].__name__)
|
||||
)
|
||||
|
||||
|
||||
class TestCohortsAndPartitionGroups(ModuleStoreTestCase):
|
||||
"""
|
||||
|
||||
@@ -3,7 +3,6 @@ Tests for course group views
|
||||
"""
|
||||
# pylint: disable=attribute-defined-outside-init
|
||||
# pylint: disable=no-member
|
||||
from collections import namedtuple
|
||||
import json
|
||||
|
||||
from collections import namedtuple
|
||||
@@ -15,7 +14,6 @@ from student.models import CourseEnrollment
|
||||
from student.tests.factories import UserFactory
|
||||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
|
||||
from xmodule.modulestore.tests.factories import CourseFactory
|
||||
from xmodule.modulestore.django import modulestore
|
||||
from opaque_keys.edx.locations import SlashSeparatedCourseKey
|
||||
|
||||
from ..models import CourseUserGroup, CourseCohort
|
||||
@@ -180,13 +178,26 @@ class CourseCohortSettingsHandlerTestCase(CohortViewsTestCase):
|
||||
"""
|
||||
config_course_cohorts(self.course, [], cohorted=True)
|
||||
|
||||
# Get the cohorts from the course. This will run the migrations.
|
||||
# And due to migrations CourseCohortsSettings object will be created.
|
||||
self.get_handler(self.course)
|
||||
|
||||
response = self.put_handler(self.course, expected_response_code=400, handler=course_cohort_settings_handler)
|
||||
self.assertEqual("Bad Request", response.get("error"))
|
||||
|
||||
def test_update_settings_with_invalid_field_data_type(self):
|
||||
"""
|
||||
Verify that course_cohort_settings_handler return HTTP 400 if field data type is incorrect.
|
||||
"""
|
||||
config_course_cohorts(self.course, [], cohorted=True)
|
||||
|
||||
response = self.put_handler(
|
||||
self.course,
|
||||
data={'is_cohorted': ''},
|
||||
expected_response_code=400,
|
||||
handler=course_cohort_settings_handler
|
||||
)
|
||||
self.assertEqual(
|
||||
"Incorrect field type for `{}`. Type must be `{}`".format('is_cohorted', bool.__name__),
|
||||
response.get("error")
|
||||
)
|
||||
|
||||
|
||||
class CohortHandlerTestCase(CohortViewsTestCase):
|
||||
"""
|
||||
|
||||
@@ -112,7 +112,13 @@ def course_cohort_settings_handler(request, course_key_string):
|
||||
if is_cohorted is None:
|
||||
# Note: error message not translated because it is not exposed to the user (UI prevents this state).
|
||||
return JsonResponse({"error": "Bad Request"}, 400)
|
||||
cohort_settings = cohorts.set_course_cohort_settings(course_key, is_cohorted=is_cohorted)
|
||||
|
||||
try:
|
||||
cohort_settings = cohorts.set_course_cohort_settings(course_key, is_cohorted=is_cohorted)
|
||||
except ValueError as err:
|
||||
# Note: error message not translated because it is not exposed to the user (UI prevents this state).
|
||||
return JsonResponse({"error": unicode(err)}, 400)
|
||||
|
||||
return JsonResponse(_get_course_cohort_settings_representation(cohort_settings))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user