Commit 13edb0b0 by sandroroux Committed by Tyler Hallada

Updated course_has_highlights and get_week_highlights. Methods ensure that a…

Updated course_has_highlights and get_week_highlights. Methods ensure that a course has highlights_enabled_for_messaging

Responded to Nimisha's review feedback.
parent ea488f84
import logging
from courseware.module_render import get_module_for_descriptor
from courseware.model_data import FieldDataCache
from openedx.core.djangoapps.schedules.config import COURSE_UPDATE_WAFFLE_FLAG
......@@ -6,6 +8,8 @@ from request_cache import get_request_or_stub
from xmodule.modulestore.django import modulestore
log = logging.getLogger(__name__)
def course_has_highlights(course_key):
"""
......@@ -13,16 +17,26 @@ def course_has_highlights(course_key):
This ignores access checks, since highlights may be lurking in currently
inaccessible content.
"""
if not COURSE_UPDATE_WAFFLE_FLAG.is_enabled(course_key):
try:
course = _get_course_with_highlights(course_key)
except CourseUpdateDoesNotExist:
return False
course = modulestore().get_course(course_key, depth=1)
return any(
else:
course_has_highlights = any(
section.highlights
for section in course.get_children()
if not section.hide_from_toc
)
if not course_has_highlights:
log.error(
"Course team enabled highlights and provided no highlights."
)
return course_has_highlights
def get_week_highlights(user, course_key, week_num):
"""
......@@ -31,17 +45,32 @@ def get_week_highlights(user, course_key, week_num):
Raises CourseUpdateDoesNotExist if highlights do not exist for
the requested week_num.
"""
course_descriptor = _get_course_with_highlights(course_key)
course_module = _get_course_module(course_descriptor, user)
sections_with_highlights = _get_sections_with_highlights(course_module)
highlights = _get_highlights_for_week(
sections_with_highlights,
week_num,
course_key,
)
return highlights
def _get_course_with_highlights(course_key):
if not COURSE_UPDATE_WAFFLE_FLAG.is_enabled(course_key):
raise CourseUpdateDoesNotExist(
"%s does not have Course Updates enabled.",
course_key
"%s Course Update Messages waffle flag is disabled.",
course_key,
)
course_descriptor = _get_course_descriptor(course_key)
course_module = _get_course_module(course_descriptor, user)
sections_with_highlights = _get_sections_with_highlights(course_module)
highlights = _get_highlights_for_week(sections_with_highlights, week_num, course_key)
return highlights
if not course_descriptor.highlights_enabled_for_messaging:
raise CourseUpdateDoesNotExist(
"%s Course Update Messages are disabled.",
course_key,
)
return course_descriptor
def _get_course_descriptor(course_key):
......
......@@ -21,7 +21,9 @@ class TestContentHighlights(ModuleStoreTestCase):
self._setup_user()
def _setup_course(self):
self.course = CourseFactory.create()
self.course = CourseFactory.create(
highlights_enabled_for_messaging=True
)
self.course_key = self.course.id
def _setup_user(self):
......@@ -67,6 +69,23 @@ class TestContentHighlights(ModuleStoreTestCase):
)
@override_waffle_flag(COURSE_UPDATE_WAFFLE_FLAG, True)
def test_highlights_disabled_for_messaging(self):
highlights = [u'A test highlight.']
with self.store.bulk_operations(self.course_key):
self._create_chapter(highlights=highlights)
self.course.highlights_enabled_for_messaging = False
self.store.update_item(self.course, self.user.id)
self.assertFalse(course_has_highlights(self.course_key))
with self.assertRaises(CourseUpdateDoesNotExist):
get_week_highlights(
self.user,
self.course_key,
week_num=1,
)
@override_waffle_flag(COURSE_UPDATE_WAFFLE_FLAG, True)
def test_course_with_no_highlights(self):
with self.store.bulk_operations(self.course_key):
self._create_chapter(display_name=u"Week 1")
......
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