Commit 759898b8 by Chris Dodge

wip

parent 7c581d06
......@@ -12,5 +12,6 @@ urlpatterns = patterns(
url(r'^(?P<course_id>[a-zA-Z0-9/_:]+)/modules/*$', 'modules_list'),
url(r'^(?P<course_id>[a-zA-Z0-9/_:]+)/groups/(?P<group_id>[0-9]+)$', 'courses_groups_detail'),
url(r'^(?P<course_id>[a-zA-Z0-9/_:]+)/groups/*$', 'courses_groups_list'),
url(r'^(?P<course_id>[a-zA-Z0-9/_:]+)/about$', 'course_about'),
url(r'^(?P<course_id>[a-zA-Z0-9/_:]+)$', 'courses_detail'),
)
......@@ -2,6 +2,8 @@
from django.contrib.auth.models import Group
from django.core.exceptions import ObjectDoesNotExist
from lxml import etree
from StringIO import StringIO
from rest_framework import status
from rest_framework.decorators import api_view, permission_classes
......@@ -12,6 +14,7 @@ from api_manager.models import CourseGroupRelationship
from xmodule.modulestore.django import modulestore
from xmodule.modulestore import Location, InvalidLocationError
from courseware.courses import get_course_about_section
def _generate_base_uri(request):
"""
......@@ -295,3 +298,68 @@ def courses_groups_detail(request, course_id, group_id):
except ObjectDoesNotExist:
pass
return Response({}, status=status.HTTP_204_NO_CONTENT)
def _parse_about_html(html):
"""
Helper method to break up the course about HTML into components
"""
result = {}
parser = etree.HTMLParser()
tree = etree.parse(StringIO(html), parser)
sections = tree.findall('/body/section')
result = []
for section in sections:
section_class = section.get('class')
if section_class:
section_data = {}
section_data['name'] = section_class
section_data['articles'] = []
articles = section.findall('article')
if articles:
for article in articles:
article_class = article.get('class')
if article_class:
article_data = {}
article_data['name'] = article_class
article_data['body'] = etree.tostring(article)
section_data['articles'].append(article_data)
else:
section_data['body'] = etree.tostring(section)
result.append(section_data)
return result
@api_view(['GET'])
@permission_classes((ApiKeyHeaderPermission,))
def course_about(request, course_id):
"""
GET retrieves the course overview module, which - in MongoDB - is stored with the following
naming convention {"_id.org":"i4x", "_id.course":<course_num>, "_id.category":"about", "_id.name":"overview"}
"""
store = modulestore()
response_data = {}
try:
course_module = store.get_course(course_id)
if not course_module:
return Response({}, status=status.HTTP_404_NOT_FOUND)
overview = get_course_about_section(course_module, 'overview')
short_description = get_course_about_section(course_module, 'short_description')
response_data['sections'] = _parse_about_html(overview)
response_data['overview_html'] = overview
response_data['short_description'] = short_description
except InvalidLocationError:
return Response({}, status=status.HTTP_404_NOT_FOUND)
return Response(response_data)
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