Commit 21510a1e by kimth

Textbook TOC from S3

parent 01fafd34
from fs.errors import ResourceNotFoundError from fs.errors import ResourceNotFoundError
import time import time
import logging import logging
import requests
from lxml import etree from lxml import etree
from xmodule.util.decorators import lazyproperty from xmodule.util.decorators import lazyproperty
...@@ -15,19 +16,48 @@ class CourseDescriptor(SequenceDescriptor): ...@@ -15,19 +16,48 @@ class CourseDescriptor(SequenceDescriptor):
module_class = SequenceModule module_class = SequenceModule
class Textbook: class Textbook:
def __init__(self, title, table_of_contents_url): def __init__(self, title, book_url):
self.title = title self.title = title
self.table_of_contents_url = table_of_contents_url self.book_url = book_url
self.table_of_contents = self._get_toc_from_s3()
@classmethod @classmethod
def from_xml_object(cls, xml_object): def from_xml_object(cls, xml_object):
return cls(xml_object.get('title'), xml_object.get('table_of_contents_url')) return cls(xml_object.get('title'), xml_object.get('book_url'))
@property @property
def table_of_contents(self): def table_of_contents(self):
raw_table_of_contents = open(self.table_of_contents_url, 'r') # TODO: This will need to come from S3 '''
raw_table_of_contents = open(self.book_url, 'r') # TODO: This will need to come from S3
table_of_contents = etree.parse(raw_table_of_contents).getroot() table_of_contents = etree.parse(raw_table_of_contents).getroot()
return table_of_contents return table_of_contents
'''
return self.table_of_contents
def _get_toc_from_s3(self):
'''
Returns XML tree representation of the table of contents
'''
toc_url = self.book_url + 'toc.xml'
# Get the table of contents from S3
log.info("Retrieving textbook table of contents from %s" % toc_url)
try:
r = requests.get(toc_url)
except Exception as err:
msg = 'Error %s: Unable to retrieve textbook table of contents at %s' % (err, toc_url)
log.error(msg)
raise Exception(msg)
# TOC is XML. Parse it
try:
table_of_contents = etree.fromstring(r.text)
except Exception as err:
msg = 'Error %s: Unable to parse XML for textbook table of contents at %s' % (err, toc_url)
log.error(msg)
raise Exception(msg)
return table_of_contents
def __init__(self, system, definition=None, **kwargs): def __init__(self, system, definition=None, **kwargs):
......
...@@ -14,10 +14,8 @@ def index(request, course_id, book_index, page=0): ...@@ -14,10 +14,8 @@ def index(request, course_id, book_index, page=0):
textbook = course.textbooks[int(book_index)] textbook = course.textbooks[int(book_index)]
table_of_contents = textbook.table_of_contents table_of_contents = textbook.table_of_contents
book_url = settings.BOOK_URL
return render_to_response('staticbook.html', return render_to_response('staticbook.html',
{'page': int(page), 'course': course, 'book_url': book_url, {'page': int(page), 'course': course, 'book_url': textbook.book_url,
'table_of_contents': table_of_contents, 'table_of_contents': table_of_contents,
'staff_access': staff_access}) 'staff_access': staff_access})
......
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