From 4984f9d804d211f55e1658cda66ab20681e916f6 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Mon, 23 Jul 2012 16:22:39 -0400 Subject: [PATCH 1/2] Use django-pipeline to read course static files. Requires the replacement of {COURSE_STATIC_URL} with just /static/ in about/overview.html files. --- .../templates/static_content.html | 3 --- lms/djangoapps/courseware/courses.py | 26 +++++++++---------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/common/djangoapps/pipeline_mako/templates/static_content.html b/common/djangoapps/pipeline_mako/templates/static_content.html index 225e3e0542..1737153260 100644 --- a/common/djangoapps/pipeline_mako/templates/static_content.html +++ b/common/djangoapps/pipeline_mako/templates/static_content.html @@ -1,7 +1,6 @@ <%! from staticfiles.storage import staticfiles_storage from pipeline_mako import compressed_css, compressed_js -from static_replace import replace_urls %> <%def name='url(file)'>${staticfiles_storage.url(file)} @@ -24,5 +23,3 @@ from static_replace import replace_urls % endfor %endif - -<%def name='replace_urls(text)'>${replace_urls(text)} diff --git a/lms/djangoapps/courseware/courses.py b/lms/djangoapps/courseware/courses.py index f92efbe4eb..1eb15f5d85 100644 --- a/lms/djangoapps/courseware/courses.py +++ b/lms/djangoapps/courseware/courses.py @@ -9,18 +9,21 @@ from django.http import Http404 from xmodule.course_module import CourseDescriptor from xmodule.modulestore.django import modulestore from xmodule.modulestore.exceptions import ItemNotFoundError +from static_replace import replace_urls +from staticfiles.storage import staticfiles_storage log = logging.getLogger(__name__) + def check_course(course_id, course_must_be_open=True, course_required=True): """ Given a course_id, this returns the course object. By default, if the course is not found or the course is not open yet, this method will raise a 404. - + If course_must_be_open is False, the course will be returned without a 404 even if it is not open. - + If course_required is False, a course_id of None is acceptable. The course returned will be None. Even if the course is not required, if a course_id is given that does not exist a 404 will be raised. @@ -32,22 +35,17 @@ def check_course(course_id, course_must_be_open=True, course_required=True): course = modulestore().get_item(course_loc) except (KeyError, ItemNotFoundError): raise Http404("Course not found.") - + if course_must_be_open and not course.has_started(): raise Http404("This course has not yet started.") - + return course -### These methods look like they should be on the course_module object itself, but they rely -### on the lms. Maybe they should be added dynamically to the class? - -def course_static_url(course): - return settings.STATIC_URL + "/" + course.metadata['data_dir'] + "/" - def course_image_url(course): - return course_static_url(course) + "images/course_image.jpg" - + return staticfiles_storage.url(course.metadata['data_dir'] + "/images/course_image.jpg") + + def get_course_about_section(course, section_key): """ This returns the snippet of html to be rendered on the course about page, given the key for the section. @@ -78,7 +76,7 @@ def get_course_about_section(course, section_key): 'effort', 'end_date', 'prerequisites']: try: with course.system.resources_fs.open(path("about") / section_key + ".html") as htmlFile: - return htmlFile.read().decode('utf-8').format(COURSE_STATIC_URL = course_static_url(course) ) + return replace_urls(htmlFile.read().decode('utf-8'), course.metadata['data_dir']) except ResourceNotFoundError: log.warning("Missing about section {key} in course {url}".format(key=section_key, url=course.location.url())) return None @@ -114,4 +112,4 @@ def get_course_info_section(course, section_key): raise KeyError("Invalid about key " + str(section_key)) - \ No newline at end of file + From b8b133d30c3e83836eda800787725be3a3ff965d Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Mon, 23 Jul 2012 16:31:52 -0400 Subject: [PATCH 2/2] Replace /static urls in all course snippets --- lms/djangoapps/courseware/courses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lms/djangoapps/courseware/courses.py b/lms/djangoapps/courseware/courses.py index 1eb15f5d85..c65612772b 100644 --- a/lms/djangoapps/courseware/courses.py +++ b/lms/djangoapps/courseware/courses.py @@ -105,7 +105,7 @@ def get_course_info_section(course, section_key): if section_key in ['handouts', 'guest_handouts', 'updates', 'guest_updates']: try: with course.system.resources_fs.open(path("info") / section_key + ".html") as htmlFile: - return htmlFile.read().decode('utf-8') + return replace_urls(htmlFile.read().decode('utf-8'), course.metadata['data_dir']) except ResourceNotFoundError: log.exception("Missing info section {key} in course {url}".format(key=section_key, url=course.location.url())) return "! Info section missing !"