From 987d7bcc298226c69f4a8df1c938ce90a4784597 Mon Sep 17 00:00:00 2001 From: kimth Date: Mon, 20 Aug 2012 12:40:34 -0400 Subject: [PATCH 1/3] book_url doesn't come from settings --- lms/djangoapps/staticbook/views.py | 5 ++++- lms/templates/staticbook.html | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lms/djangoapps/staticbook/views.py b/lms/djangoapps/staticbook/views.py index aaafb60dd8..c43bed5ae6 100644 --- a/lms/djangoapps/staticbook/views.py +++ b/lms/djangoapps/staticbook/views.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.contrib.auth.decorators import login_required from mitxmako.shortcuts import render_to_response @@ -13,8 +14,10 @@ def index(request, course_id, book_index, page=0): textbook = course.textbooks[int(book_index)] table_of_contents = textbook.table_of_contents + book_url = settings.BOOK_URL + return render_to_response('staticbook.html', - {'page': int(page), 'course': course, + {'page': int(page), 'course': course, 'book_url': book_url, 'table_of_contents': table_of_contents, 'staff_access': staff_access}) diff --git a/lms/templates/staticbook.html b/lms/templates/staticbook.html index 2a0a6f03bb..8677186d23 100644 --- a/lms/templates/staticbook.html +++ b/lms/templates/staticbook.html @@ -32,7 +32,7 @@ function goto_page(n) { if(n<10) { prefix="00"; } - $("#bookpage").attr("src","${ settings.BOOK_URL }p"+prefix+n+".png"); + $("#bookpage").attr("src","${ book_url }p"+prefix+n+".png"); $.cookie("book_page", n, {'expires':3650, 'path':'/'}); }; @@ -113,7 +113,7 @@ $("#open_close_accordion a").click(function(){ - + From c084745290244584235ec2f8e5f366f5ea82f72c Mon Sep 17 00:00:00 2001 From: kimth Date: Mon, 20 Aug 2012 13:32:13 -0400 Subject: [PATCH 2/3] Textbook TOC from S3 --- common/lib/xmodule/xmodule/course_module.py | 38 ++++++++++++++++++--- lms/djangoapps/staticbook/views.py | 4 +-- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/common/lib/xmodule/xmodule/course_module.py b/common/lib/xmodule/xmodule/course_module.py index 4729de905d..b647cf77ed 100644 --- a/common/lib/xmodule/xmodule/course_module.py +++ b/common/lib/xmodule/xmodule/course_module.py @@ -1,6 +1,7 @@ from fs.errors import ResourceNotFoundError import time import logging +import requests from lxml import etree from xmodule.util.decorators import lazyproperty @@ -15,19 +16,48 @@ class CourseDescriptor(SequenceDescriptor): module_class = SequenceModule class Textbook: - def __init__(self, title, table_of_contents_url): + def __init__(self, title, book_url): 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 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 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() 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): diff --git a/lms/djangoapps/staticbook/views.py b/lms/djangoapps/staticbook/views.py index c43bed5ae6..d68117dd8a 100644 --- a/lms/djangoapps/staticbook/views.py +++ b/lms/djangoapps/staticbook/views.py @@ -14,10 +14,8 @@ def index(request, course_id, book_index, page=0): textbook = course.textbooks[int(book_index)] table_of_contents = textbook.table_of_contents - book_url = settings.BOOK_URL - 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, 'staff_access': staff_access}) From 9852a581f0b23c88ce491ad97bbed90cb760d4b4 Mon Sep 17 00:00:00 2001 From: kimth Date: Mon, 20 Aug 2012 13:43:55 -0400 Subject: [PATCH 3/3] Adjust comments --- common/lib/xmodule/xmodule/course_module.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/common/lib/xmodule/xmodule/course_module.py b/common/lib/xmodule/xmodule/course_module.py index b647cf77ed..dedf4c86ec 100644 --- a/common/lib/xmodule/xmodule/course_module.py +++ b/common/lib/xmodule/xmodule/course_module.py @@ -27,15 +27,12 @@ class CourseDescriptor(SequenceDescriptor): @property def table_of_contents(self): - ''' - 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() - return table_of_contents - ''' return self.table_of_contents def _get_toc_from_s3(self): ''' + Accesses the textbook's table of contents (default name "toc.xml") at the URL self.book_url + Returns XML tree representation of the table of contents ''' toc_url = self.book_url + 'toc.xml'