Commit 091d6aeb by Nimisha Asthagiri

Mobile API: Modularize VideoOutline code

parent 40dd4f1f
...@@ -30,16 +30,58 @@ class BlockOutline(object): ...@@ -30,16 +30,58 @@ class BlockOutline(object):
self.local_cache['course_videos'] = {} self.local_cache['course_videos'] = {}
def __iter__(self): def __iter__(self):
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
)
child_to_parent = {} child_to_parent = {}
stack = [self.start_block] stack = [self.start_block]
while stack:
curr_block = stack.pop()
if curr_block.hide_from_toc:
# For now, if the 'hide_from_toc' setting is set on the block, do not traverse down
# the hierarchy. The reason being is that these blocks may not have human-readable names
# to display on the mobile clients.
# Eventually, we'll need to figure out how we want these blocks to be displayed on the
# mobile clients. As, they are still accessible in the browser, just not navigatable
# from the table-of-contents.
continue
if curr_block.location.block_type in self.block_types:
if not has_access(self.request.user, 'load', curr_block, course_key=self.course_id):
continue
summary_fn = self.block_types[curr_block.category]
block_path = list(path(curr_block, child_to_parent, self.start_block))
unit_url, section_url = find_urls(self.course_id, curr_block, child_to_parent, self.request)
yield {
"path": block_path,
"named_path": [b["name"] for b in block_path],
"unit_url": unit_url,
"section_url": section_url,
"summary": summary_fn(self.course_id, curr_block, self.request, self.local_cache)
}
# path should be optional if curr_block.has_children:
def path(block): children = curr_block.get_children(usage_key_filter=parent_or_requested_block_type)
for block in reversed(children):
stack.append(block)
child_to_parent[block] = curr_block
def path(block, child_to_parent, start_block):
"""path for block""" """path for block"""
block_path = [] block_path = []
while block in child_to_parent: while block in child_to_parent:
block = child_to_parent[block] block = child_to_parent[block]
if block is not self.start_block: if block is not start_block:
block_path.append({ block_path.append({
# to be consistent with other edx-platform clients, return the defaulted display name # to be consistent with other edx-platform clients, return the defaulted display name
'name': block.display_name_with_default, 'name': block.display_name_with_default,
...@@ -48,7 +90,8 @@ class BlockOutline(object): ...@@ -48,7 +90,8 @@ class BlockOutline(object):
}) })
return reversed(block_path) return reversed(block_path)
def find_urls(block):
def find_urls(course_id, block, child_to_parent, request):
""" """
Find the section and unit urls for a block. Find the section and unit urls for a block.
...@@ -70,7 +113,6 @@ class BlockOutline(object): ...@@ -70,7 +113,6 @@ class BlockOutline(object):
section = block_list[2] if block_count > 2 else None section = block_list[2] if block_count > 2 else None
position = None position = None
#position is found traversing the section block
if block_count > 3: if block_count > 3:
position = 1 position = 1
for block in section.children: for block in section.children:
...@@ -78,101 +120,25 @@ class BlockOutline(object): ...@@ -78,101 +120,25 @@ class BlockOutline(object):
break break
position += 1 position += 1
kwargs = {'course_id': unicode(course_id)}
if chapter_id is None: if chapter_id is None:
no_chapter_url = reverse( no_chapter_url = reverse("courseware", kwargs=kwargs, request=request)
"courseware",
kwargs=dict(
course_id=unicode(self.course_id),
),
request=self.request
)
return no_chapter_url, no_chapter_url return no_chapter_url, no_chapter_url
elif section is None:
no_section_url = reverse(
"courseware_chapter",
kwargs=dict(
course_id=unicode(self.course_id),
chapter=chapter_id
),
request=self.request
)
return no_section_url, no_section_url
elif position is None:
no_position_url = reverse(
"courseware_section",
kwargs=dict(
course_id=unicode(self.course_id),
chapter=chapter_id,
section=section.url_name
),
request=self.request
)
return no_position_url, no_position_url
else:
section_url = reverse(
"courseware_section",
kwargs=dict(
course_id=unicode(self.course_id),
chapter=chapter_id,
section=section.url_name
),
request=self.request
)
unit_url = reverse(
"courseware_position",
kwargs=dict(
course_id=unicode(self.course_id),
chapter=chapter_id,
section=section.url_name,
position=position
),
request=self.request
)
return unit_url, section_url
user = self.request.user
while stack:
curr_block = stack.pop()
if curr_block.hide_from_toc:
# For now, if the 'hide_from_toc' setting is set on the block, do not traverse down
# the hierarchy. The reason being is that these blocks may not have human-readable names
# to display on the mobile clients.
# Eventually, we'll need to figure out how we want these blocks to be displayed on the
# mobile clients. As, they are still accessible in the browser, just not navigatable
# from the table-of-contents.
continue
if curr_block.location.block_type in self.block_types:
if not has_access(user, 'load', curr_block, course_key=self.course_id):
continue
summary_fn = self.block_types[curr_block.category]
block_path = list(path(curr_block))
unit_url, section_url = find_urls(curr_block)
yield { kwargs['chapter'] = chapter_id
"path": block_path, if section is None:
"named_path": [b["name"] for b in block_path], no_section_url = reverse("courseware_chapter", kwargs=kwargs, request=request)
"unit_url": unit_url, return no_section_url, no_section_url
"section_url": section_url,
"summary": summary_fn(self.course_id, curr_block, self.request, self.local_cache)
}
def parent_or_requested_block_type(usage_key): kwargs['section'] = section.url_name
""" if position is None:
Returns whether the usage_key's block_type is one of self.block_types or a parent type. no_position_url = reverse("courseware_section", kwargs=kwargs, request=request)
""" return no_position_url, no_position_url
return (
usage_key.block_type in self.block_types or
usage_key.block_type in BLOCK_TYPES_WITH_CHILDREN
)
if curr_block.has_children: section_url = reverse("courseware_section", kwargs=kwargs, request=request)
for block in reversed(curr_block.get_children(usage_key_filter=parent_or_requested_block_type)): kwargs['position'] = position
stack.append(block) unit_url = reverse("courseware_position", kwargs=kwargs, request=request)
child_to_parent[block] = curr_block return unit_url, section_url
def video_summary(course, course_id, video_descriptor, request, local_cache): def video_summary(course, course_id, video_descriptor, request, local_cache):
......
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