Commit a6130507 by Nimisha Asthagiri

Mobile Video Summary API: add filter for get_children

parent 4b9434a3
...@@ -402,7 +402,7 @@ class XModuleMixin(XBlockMixin): ...@@ -402,7 +402,7 @@ class XModuleMixin(XBlockMixin):
else: else:
return [self.display_name_with_default] return [self.display_name_with_default]
def get_children(self): def get_children(self, usage_key_filter=lambda location: True):
"""Returns a list of XBlock instances for the children of """Returns a list of XBlock instances for the children of
this module""" this module"""
...@@ -412,6 +412,9 @@ class XModuleMixin(XBlockMixin): ...@@ -412,6 +412,9 @@ class XModuleMixin(XBlockMixin):
if getattr(self, '_child_instances', None) is None: if getattr(self, '_child_instances', None) is None:
self._child_instances = [] # pylint: disable=attribute-defined-outside-init self._child_instances = [] # pylint: disable=attribute-defined-outside-init
for child_loc in self.children: for child_loc in self.children:
# Skip if it doesn't satisfy the filter function
if not usage_key_filter(child_loc):
continue
try: try:
child = self.runtime.get_block(child_loc) child = self.runtime.get_block(child_loc)
if child is None: if child is None:
......
...@@ -3,6 +3,7 @@ Serializer for video outline ...@@ -3,6 +3,7 @@ Serializer for video outline
""" """
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from xmodule.modulestore.mongo.base import BLOCK_TYPES_WITH_CHILDREN
from courseware.access import has_access from courseware.access import has_access
from edxval.api import ( from edxval.api import (
...@@ -14,10 +15,10 @@ class BlockOutline(object): ...@@ -14,10 +15,10 @@ class BlockOutline(object):
""" """
Serializes course videos, pulling data from VAL and the video modules. Serializes course videos, pulling data from VAL and the video modules.
""" """
def __init__(self, course_id, start_block, categories_to_outliner, request): def __init__(self, course_id, start_block, block_types, request):
"""Create a BlockOutline using `start_block` as a starting point.""" """Create a BlockOutline using `start_block` as a starting point."""
self.start_block = start_block self.start_block = start_block
self.categories_to_outliner = categories_to_outliner self.block_types = block_types
self.course_id = course_id self.course_id = course_id
self.request = request # needed for making full URLS self.request = request # needed for making full URLS
self.local_cache = {} self.local_cache = {}
...@@ -143,11 +144,11 @@ class BlockOutline(object): ...@@ -143,11 +144,11 @@ class BlockOutline(object):
# from the table-of-contents. # from the table-of-contents.
continue continue
if curr_block.category in self.categories_to_outliner: if curr_block.location.block_type in self.block_types:
if not has_access(user, 'load', curr_block, course_key=self.course_id): if not has_access(user, 'load', curr_block, course_key=self.course_id):
continue continue
summary_fn = self.categories_to_outliner[curr_block.category] summary_fn = self.block_types[curr_block.category]
block_path = list(path(curr_block)) block_path = list(path(curr_block))
unit_url, section_url = find_urls(curr_block) unit_url, section_url = find_urls(curr_block)
...@@ -159,8 +160,17 @@ class BlockOutline(object): ...@@ -159,8 +160,17 @@ class BlockOutline(object):
"summary": summary_fn(self.course_id, curr_block, self.request, self.local_cache) "summary": summary_fn(self.course_id, curr_block, self.request, self.local_cache)
} }
def parent_or_requested_block_type(usage_key):
"""
Returns whether the usage_key's block_type is one of self.block_types or a parent type.
"""
return (
usage_key.block_type in self.block_types or
usage_key.block_type in BLOCK_TYPES_WITH_CHILDREN
)
if curr_block.has_children: if curr_block.has_children:
for block in reversed(curr_block.get_children()): for block in reversed(curr_block.get_children(usage_key_filter=parent_or_requested_block_type)):
stack.append(block) stack.append(block)
child_to_parent[block] = curr_block child_to_parent[block] = curr_block
......
...@@ -20,42 +20,42 @@ class TestVideoAPITestCase(MobileAPITestCase): ...@@ -20,42 +20,42 @@ class TestVideoAPITestCase(MobileAPITestCase):
def setUp(self): def setUp(self):
super(TestVideoAPITestCase, self).setUp() super(TestVideoAPITestCase, self).setUp()
self.section = ItemFactory.create( self.section = ItemFactory.create(
parent_location=self.course.location, parent=self.course,
category="chapter", category="chapter",
display_name=u"test factory section omega \u03a9", display_name=u"test factory section omega \u03a9",
) )
self.sub_section = ItemFactory.create( self.sub_section = ItemFactory.create(
parent_location=self.section.location, parent=self.section,
category="sequential", category="sequential",
display_name=u"test subsection omega \u03a9", display_name=u"test subsection omega \u03a9",
) )
self.unit = ItemFactory.create( self.unit = ItemFactory.create(
parent_location=self.sub_section.location, parent=self.sub_section,
category="vertical", category="vertical",
metadata={'graded': True, 'format': 'Homework'}, metadata={'graded': True, 'format': 'Homework'},
display_name=u"test unit omega \u03a9", display_name=u"test unit omega \u03a9",
) )
self.other_unit = ItemFactory.create( self.other_unit = ItemFactory.create(
parent_location=self.sub_section.location, parent=self.sub_section,
category="vertical", category="vertical",
metadata={'graded': True, 'format': 'Homework'}, metadata={'graded': True, 'format': 'Homework'},
display_name=u"test unit omega 2 \u03a9", display_name=u"test unit omega 2 \u03a9",
) )
self.nameless_unit = ItemFactory.create( self.nameless_unit = ItemFactory.create(
parent_location=self.sub_section.location, parent=self.sub_section,
category="vertical", category="vertical",
metadata={'graded': True, 'format': 'Homework'}, metadata={'graded': True, 'format': 'Homework'},
display_name=None, display_name=None,
) )
self.split_unit = ItemFactory.create( self.split_unit = ItemFactory.create(
parent_location=self.sub_section.location, parent=self.sub_section,
category="vertical", category="vertical",
display_name=u"split test vertical\u03a9", display_name=u"split test vertical\u03a9",
) )
self.split_test = ItemFactory.create( self.split_test = ItemFactory.create(
parent_location=self.split_unit.location, parent=self.split_unit,
category="split_test", category="split_test",
display_name=u"split test unit" display_name=u"split test unit"
) )
...@@ -120,7 +120,7 @@ class TestVideoAPITestCase(MobileAPITestCase): ...@@ -120,7 +120,7 @@ class TestVideoAPITestCase(MobileAPITestCase):
subid, subid,
self.course) self.course)
return ItemFactory.create( return ItemFactory.create(
parent_location=self.unit.location, parent=self.unit,
category="video", category="video",
edx_video_id=self.edx_video_id, edx_video_id=self.edx_video_id,
display_name=u"test video omega \u03a9", display_name=u"test video omega \u03a9",
...@@ -156,27 +156,27 @@ class TestNonStandardCourseStructure(MobileAPITestCase): ...@@ -156,27 +156,27 @@ class TestNonStandardCourseStructure(MobileAPITestCase):
def setUp(self): def setUp(self):
super(TestNonStandardCourseStructure, self).setUp() super(TestNonStandardCourseStructure, self).setUp()
self.chapter_under_course = ItemFactory.create( self.chapter_under_course = ItemFactory.create(
parent_location=self.course.location, parent=self.course,
category="chapter", category="chapter",
display_name=u"test factory chapter under course omega \u03a9", display_name=u"test factory chapter under course omega \u03a9",
) )
self.section_under_course = ItemFactory.create( self.section_under_course = ItemFactory.create(
parent_location=self.course.location, parent=self.course,
category="sequential", category="sequential",
display_name=u"test factory section under course omega \u03a9", display_name=u"test factory section under course omega \u03a9",
) )
self.section_under_chapter = ItemFactory.create( self.section_under_chapter = ItemFactory.create(
parent_location=self.chapter_under_course.location, parent=self.chapter_under_course,
category="sequential", category="sequential",
display_name=u"test factory section under chapter omega \u03a9", display_name=u"test factory section under chapter omega \u03a9",
) )
self.vertical_under_course = ItemFactory.create( self.vertical_under_course = ItemFactory.create(
parent_location=self.course.location, parent=self.course,
category="vertical", category="vertical",
display_name=u"test factory vertical under course omega \u03a9", display_name=u"test factory vertical under course omega \u03a9",
) )
self.vertical_under_section = ItemFactory.create( self.vertical_under_section = ItemFactory.create(
parent_location=self.section_under_chapter.location, parent=self.section_under_chapter,
category="vertical", category="vertical",
display_name=u"test factory vertical under section omega \u03a9", display_name=u"test factory vertical under section omega \u03a9",
) )
...@@ -187,7 +187,7 @@ class TestNonStandardCourseStructure(MobileAPITestCase): ...@@ -187,7 +187,7 @@ class TestNonStandardCourseStructure(MobileAPITestCase):
""" """
self.login_and_enroll() self.login_and_enroll()
ItemFactory.create( ItemFactory.create(
parent_location=self.course.location, parent=self.course,
category="video", category="video",
display_name=u"test factory video omega \u03a9", display_name=u"test factory video omega \u03a9",
) )
...@@ -206,7 +206,7 @@ class TestNonStandardCourseStructure(MobileAPITestCase): ...@@ -206,7 +206,7 @@ class TestNonStandardCourseStructure(MobileAPITestCase):
""" """
self.login_and_enroll() self.login_and_enroll()
ItemFactory.create( ItemFactory.create(
parent_location=self.vertical_under_course.location, parent=self.vertical_under_course,
category="video", category="video",
display_name=u"test factory video omega \u03a9", display_name=u"test factory video omega \u03a9",
) )
...@@ -234,7 +234,7 @@ class TestNonStandardCourseStructure(MobileAPITestCase): ...@@ -234,7 +234,7 @@ class TestNonStandardCourseStructure(MobileAPITestCase):
self.login_and_enroll() self.login_and_enroll()
ItemFactory.create( ItemFactory.create(
parent_location=self.chapter_under_course.location, parent=self.chapter_under_course,
category="video", category="video",
display_name=u"test factory video omega \u03a9", display_name=u"test factory video omega \u03a9",
) )
...@@ -262,7 +262,7 @@ class TestNonStandardCourseStructure(MobileAPITestCase): ...@@ -262,7 +262,7 @@ class TestNonStandardCourseStructure(MobileAPITestCase):
""" """
self.login_and_enroll() self.login_and_enroll()
ItemFactory.create( ItemFactory.create(
parent_location=self.section_under_course.location, parent=self.section_under_course,
category="video", category="video",
display_name=u"test factory video omega \u03a9", display_name=u"test factory video omega \u03a9",
) )
...@@ -291,7 +291,7 @@ class TestNonStandardCourseStructure(MobileAPITestCase): ...@@ -291,7 +291,7 @@ class TestNonStandardCourseStructure(MobileAPITestCase):
self.login_and_enroll() self.login_and_enroll()
ItemFactory.create( ItemFactory.create(
parent_location=self.section_under_chapter.location, parent=self.section_under_chapter,
category="video", category="video",
display_name=u"meow factory video omega \u03a9", display_name=u"meow factory video omega \u03a9",
) )
...@@ -323,7 +323,7 @@ class TestNonStandardCourseStructure(MobileAPITestCase): ...@@ -323,7 +323,7 @@ class TestNonStandardCourseStructure(MobileAPITestCase):
""" """
self.login_and_enroll() self.login_and_enroll()
ItemFactory.create( ItemFactory.create(
parent_location=self.vertical_under_section.location, parent=self.vertical_under_section,
category="video", category="video",
display_name=u"test factory video omega \u03a9", display_name=u"test factory video omega \u03a9",
) )
...@@ -366,19 +366,19 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro ...@@ -366,19 +366,19 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro
self.login_and_enroll() self.login_and_enroll()
self._create_video_with_subs() self._create_video_with_subs()
ItemFactory.create( ItemFactory.create(
parent_location=self.other_unit.location, parent=self.other_unit,
category="video", category="video",
display_name=u"test video omega 2 \u03a9", display_name=u"test video omega 2 \u03a9",
html5_sources=[self.html5_video_url] html5_sources=[self.html5_video_url]
) )
ItemFactory.create( ItemFactory.create(
parent_location=self.other_unit.location, parent=self.other_unit,
category="video", category="video",
display_name=u"test video omega 3 \u03a9", display_name=u"test video omega 3 \u03a9",
source=self.html5_video_url source=self.html5_video_url
) )
ItemFactory.create( ItemFactory.create(
parent_location=self.unit.location, parent=self.unit,
category="video", category="video",
edx_video_id=self.edx_video_id, edx_video_id=self.edx_video_id,
display_name=u"test draft video omega \u03a9", display_name=u"test draft video omega \u03a9",
...@@ -405,7 +405,7 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro ...@@ -405,7 +405,7 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro
def test_with_nameless_unit(self): def test_with_nameless_unit(self):
self.login_and_enroll() self.login_and_enroll()
ItemFactory.create( ItemFactory.create(
parent_location=self.nameless_unit.location, parent=self.nameless_unit,
category="video", category="video",
edx_video_id=self.edx_video_id, edx_video_id=self.edx_video_id,
display_name=u"test draft video omega 2 \u03a9" display_name=u"test draft video omega 2 \u03a9"
...@@ -423,7 +423,7 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro ...@@ -423,7 +423,7 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro
""" """
self.login_and_enroll() self.login_and_enroll()
ItemFactory.create( ItemFactory.create(
parent_location=self.sub_section.location, parent=self.sub_section,
category="video", category="video",
edx_video_id=self.edx_video_id, edx_video_id=self.edx_video_id,
display_name=u"video in the sub section" display_name=u"video in the sub section"
...@@ -446,12 +446,12 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro ...@@ -446,12 +446,12 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro
self.login_and_enroll() self.login_and_enroll()
ItemFactory.create( ItemFactory.create(
parent_location=self.split_test.location, parent=self.split_test,
category="video", category="video",
display_name=u"split test video a", display_name=u"split test video a",
) )
ItemFactory.create( ItemFactory.create(
parent_location=self.split_test.location, parent=self.split_test,
category="video", category="video",
display_name=u"split test video b", display_name=u"split test video b",
) )
...@@ -465,26 +465,26 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro ...@@ -465,26 +465,26 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro
def test_with_hidden_blocks(self): def test_with_hidden_blocks(self):
self.login_and_enroll() self.login_and_enroll()
hidden_subsection = ItemFactory.create( hidden_subsection = ItemFactory.create(
parent_location=self.section.location, parent=self.section,
category="sequential", category="sequential",
hide_from_toc=True, hide_from_toc=True,
) )
unit_within_hidden_subsection = ItemFactory.create( unit_within_hidden_subsection = ItemFactory.create(
parent_location=hidden_subsection.location, parent=hidden_subsection,
category="vertical", category="vertical",
) )
hidden_unit = ItemFactory.create( hidden_unit = ItemFactory.create(
parent_location=self.sub_section.location, parent=self.sub_section,
category="vertical", category="vertical",
hide_from_toc=True, hide_from_toc=True,
) )
ItemFactory.create( ItemFactory.create(
parent_location=unit_within_hidden_subsection.location, parent=unit_within_hidden_subsection,
category="video", category="video",
edx_video_id=self.edx_video_id, edx_video_id=self.edx_video_id,
) )
ItemFactory.create( ItemFactory.create(
parent_location=hidden_unit.location, parent=hidden_unit,
category="video", category="video",
edx_video_id=self.edx_video_id, edx_video_id=self.edx_video_id,
) )
...@@ -494,7 +494,7 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro ...@@ -494,7 +494,7 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro
def test_language(self): def test_language(self):
self.login_and_enroll() self.login_and_enroll()
video = ItemFactory.create( video = ItemFactory.create(
parent_location=self.nameless_unit.location, parent=self.nameless_unit,
category="video", category="video",
edx_video_id=self.edx_video_id, edx_video_id=self.edx_video_id,
display_name=u"test draft video omega 2 \u03a9" display_name=u"test draft video omega 2 \u03a9"
...@@ -523,7 +523,7 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro ...@@ -523,7 +523,7 @@ class TestVideoSummaryList(TestVideoAPITestCase, MobileAuthTestMixin, MobileEnro
def test_transcripts(self): def test_transcripts(self):
self.login_and_enroll() self.login_and_enroll()
video = ItemFactory.create( video = ItemFactory.create(
parent_location=self.nameless_unit.location, parent=self.nameless_unit,
category="video", category="video",
edx_video_id=self.edx_video_id, edx_video_id=self.edx_video_id,
display_name=u"test draft video omega 2 \u03a9" display_name=u"test draft video omega 2 \u03a9"
......
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