diff --git a/lms/djangoapps/courseware/courses.py b/lms/djangoapps/courseware/courses.py index 625453aad0..8634ba895d 100644 --- a/lms/djangoapps/courseware/courses.py +++ b/lms/djangoapps/courseware/courses.py @@ -51,6 +51,7 @@ def get_course_by_id(course_id, depth=0): except InvalidLocationError: raise Http404("Invalid location") + def get_course_with_access(user, course_id, action, depth=0): """ Given a course_id, look up the corresponding course descriptor, @@ -85,24 +86,24 @@ def course_image_url(course): if course.static_asset_path or modulestore().get_modulestore_type(course.location.course_id) == XML_MODULESTORE_TYPE: return '/static/' + (course.static_asset_path or getattr(course, 'data_dir', '')) + "/images/course_image.jpg" else: - loc = course.location._replace(tag='c4x', category='asset', name=course.course_image) + loc = course.location.replace(tag='c4x', category='asset', name=course.course_image) _path = StaticContent.get_url_path_from_location(loc) return _path -def find_file(fs, dirs, filename): +def find_file(filesystem, dirs, filename): """ Looks for a filename in a list of dirs on a filesystem, in the specified order. - fs: an OSFS filesystem + filesystem: an OSFS filesystem dirs: a list of path objects filename: a string Returns d / filename if found in dir d, else raises ResourceNotFoundError. """ - for d in dirs: - filepath = path(d) / filename - if fs.exists(filepath): + for directory in dirs: + filepath = path(directory) / filename + if filesystem.exists(filepath): return filepath raise ResourceNotFoundError("Could not find {0}".format(filename)) @@ -146,7 +147,7 @@ def get_course_about_section(course, section_key): request = get_request_for_thread() - loc = course.location._replace(category='about', name=section_key) + loc = course.location.replace(category='about', name=section_key) # Use an empty cache field_data_cache = FieldDataCache([], course.id, request.user) @@ -182,7 +183,6 @@ def get_course_about_section(course, section_key): raise KeyError("Invalid about key " + str(section_key)) - def get_course_info_section(request, course, section_key): """ This returns the snippet of html to be rendered on the course info page, @@ -194,8 +194,6 @@ def get_course_info_section(request, course, section_key): - updates - guest_updates """ - - loc = Location(course.location.tag, course.location.org, course.location.course, 'course_info', section_key) # Use an empty cache @@ -237,13 +235,13 @@ def get_course_syllabus_section(course, section_key): if section_key in ['syllabus', 'guest_syllabus']: try: - fs = course.system.resources_fs + filesys = course.system.resources_fs # first look for a run-specific version dirs = [path("syllabus") / course.url_name, path("syllabus")] - filepath = find_file(fs, dirs, section_key + ".html") - with fs.open(filepath) as htmlFile: + filepath = find_file(filesys, dirs, section_key + ".html") + with filesys.open(filepath) as html_file: return replace_static_urls( - htmlFile.read().decode('utf-8'), + html_file.read().decode('utf-8'), getattr(course, 'data_dir', None), course_id=course.location.course_id, static_asset_path=course.static_asset_path, diff --git a/lms/djangoapps/courseware/tests/test_courses.py b/lms/djangoapps/courseware/tests/test_courses.py index ee05a483a5..71c314a48b 100644 --- a/lms/djangoapps/courseware/tests/test_courses.py +++ b/lms/djangoapps/courseware/tests/test_courses.py @@ -1,4 +1,7 @@ # -*- coding: utf-8 -*- +""" +Tests for course access +""" import mock from django.test import TestCase @@ -9,11 +12,12 @@ from xmodule.modulestore.django import get_default_store_name_for_current_reques CMS_BASE_TEST = 'testcms' + class CoursesTest(TestCase): def test_get_course_by_id_invalid_chars(self): """ Test that `get_course_by_id` throws a 404, rather than - an exception, when faced with unexpected characters + an exception, when faced with unexpected characters (such as unicode characters, and symbols such as = and ' ') """ with self.assertRaises(Http404): @@ -30,13 +34,12 @@ class CoursesTest(TestCase): self.assertEqual("//{}/".format(CMS_BASE_TEST), get_cms_course_link_by_id("too/too/many/slashes")) self.assertEqual("//{}/org/num/course/name".format(CMS_BASE_TEST), get_cms_course_link_by_id('org/num/name')) - @mock.patch('xmodule.modulestore.django.get_current_request_hostname', mock.Mock(return_value='preview.localhost')) - @override_settings(HOSTNAME_MODULESTORE_DEFAULT_MAPPINGS={'preview\.': 'draft'}) - def test_default_modulestore_preview_mapping(self): + @override_settings(HOSTNAME_MODULESTORE_DEFAULT_MAPPINGS={r'preview\.': 'draft'}) + def test_default_modulestore_preview_mapping(self): self.assertEqual(get_default_store_name_for_current_request(), 'draft') @mock.patch('xmodule.modulestore.django.get_current_request_hostname', mock.Mock(return_value='localhost')) - @override_settings(HOSTNAME_MODULESTORE_DEFAULT_MAPPINGS={'preview\.': 'draft'}) + @override_settings(HOSTNAME_MODULESTORE_DEFAULT_MAPPINGS={r'preview\.': 'draft'}) def test_default_modulestore_published_mapping(self): self.assertEqual(get_default_store_name_for_current_request(), 'default')